7

I'm wondering if there is anyway to use doctrine's magic method to look for null values. For instance:

Doctrine::getTable('myClass')->findByDeletedAt(null);

Essentially, I want to return all records that are not deleted. I've tried the above, but it doesn't seem to work.

Any ideas?

j0k
  • 22,600
  • 28
  • 79
  • 90
Sean
  • 91
  • 1
  • 1
  • 3

4 Answers4

13

Trying this gives me the error:

Catchable fatal error: Argument 1 passed to Doctrine\ORM\EntityRepository::findBy() must be an array, string given

So this works for me:

$repository->findBy(array('date_field' => null));
j0k
  • 22,600
  • 28
  • 79
  • 90
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
2

Actually, I think I was trying to be to fancy. It can be done like this:

Doctrine::getTable('myClass')->findBy('deleted_at',null);
Shef
  • 44,808
  • 15
  • 79
  • 90
Sean
  • 91
  • 1
  • 1
  • 3
1

I know this is old, but provided solutions are not correct for Doctrine 1.2.x

When you use functions like:

Doctrine::getTable('YourClass')->findByDeleted_at(null); //he want to get NOT DELETED rows!

Doctrine throws an error that: "You must specify the value to findBy", because null is threaten as no passed value;

->findBy('deleted_at', null) throws an exception too.

->findBy('deleted_at','NULL') is either not correct becouse it is translated to SQL: ... WHERE deleted_at = 'NULL';

SQL syntax for such query should look like:

... WHERE deleted_at IS NULL;

So the solution is to use ->findBySql() method:

Doctrine::getTable('YourClass')->findBySql('deleted_at IS NULL', array());

* third argument could be a Hydration_Mode.

Hope that will help someone...

Tomasz Rozmus
  • 1,646
  • 13
  • 21
0

I know this is old but your 1st attempt was incorrect. For a field 'deleted_at' you should have been doing this:

Doctrine::getTable('YourClass')->findByDeleted_at($val);

Notice I only capitalised the 1st letter of the field 'deleted_at'.

regomodo
  • 644
  • 3
  • 9
  • 18