4

I have a Doctrine query in my model say Model_1Table.class.php that left joins with Model_2. Model_2 relates to Model_1 on a many-to-one relation. (it really is a one-to-one relation and not a many-to-one but the schema got designed so)

My query is:

$this->select('m1.*, m2.primaryKeyOfModel2')->leftJoin('m1.model2 m2')->where('m1.record = ?', some_value);

Inspecting SF Debugger logs, it retrieves the information as it should, retaining all table records from Model_1 that satisfy the where clause and retaining all values of Model_2 along with null values for those that do not exist.

The pain begins when I want to access this information, and conditionally render information dependent upon the presence/absence of model_2 records in my table. which I do like this:

<?php foreach($Model1Records as $Model1Record) :
          if($Model1Record->getModel2()) :
              foreach($Model1Record->getModel2() as $Model2Record):
                        //do something 
               end foreach ; 
          else: //basically checking for if(!($Model1Record->getModel2()))
              // do something else
           endif;
    endforeach ; ?>

Now based on this it should really work, but I think what is happening is that for the null values of model2 it fails. how do I infer that, well because if I remove the if-else statements in the above record and simply access the collection of Model2Record with a foreach loop, the available rows in Model2Record are retrieved, but then offcourse it does not access the null values. Essentially, the information I get from this is what I would typically get from an inner join.

To frame a question, how do I access records of the Model1 that have Model2 records as null values ?

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
user1020069
  • 1,540
  • 7
  • 24
  • 41

1 Answers1

3

Not sure what the default return is for a null relation between model1 and model2 via doctrine, but obviously it's not returning a false, otherwise you would be hitting the else condition, try 'instanceof' instead, for example:

<?php foreach($Model1Records as $Model1Record) :

      if($Model1Record->getModel2() instanceof Model2) :

You could also try casting the Model1 object to an array:

<?php foreach($Model1Records as $Model1Record) :

    <?php $model1 = $Model1Record->toArray();  ?>

      if(isset($model1['Model2'])) :

-- Update --

If it's returning a Doctrine_Collection instance, you should be able to do:

<?php foreach($Model1Records as $Model1Record) :

      if($Model1Record->getModel2()->count() > 0) :
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • its returning a Doctrine Collection ; which is why the else condition was never executing; so for those with null values, empty collections are being rendered... – user1020069 Aug 19 '12 at 03:20
  • instanceof Model2 will fail because it is a one-to-many relation and it returns a collection of records – user1020069 Aug 19 '12 at 03:37
  • +1 either `$collection->count()` (or `count($collection)`) should work - the gotcha is that an empty Doctrine_Collection is still an object, so casting to boolean results in `true`, whereas an array cast to boolean results in `false`. – John Carter Aug 25 '12 at 00:49