Details
Description
When using the result of a:
test.php
$q = Doctrine::createQuery()
->from('User u')
->leftJoin('Role r');
$users = $q->execute();
now if we want to iterate and know if exists the according role for each user
test.php
foreach ($users as $user)
{
$role_name = $user->relatedExists('Role') ? $user->getRole()->getName() : '';
}
the exception in the relatedExists method will be thrown:
test.php
public function relatedExists($name) { $newReference = false; if ( ! $this->hasReference($name)) { $newReference = true; } $reference = $this->$name; if ( ! $reference instanceof Doctrine_Record) { throw new Doctrine_Record_Exception( 'You can only call relatedExists() on a relationship that '. 'returns an instance of Doctrine_Record' ); } $exists = $reference->exists(); if ($newReference) { $this->clearRelated($name); } return $exists; }
because the according role record does not exist for some users ( a user just could have one record).
it could be better this way:
test.php
public function relatedExists($name) { $newReference = false; if ( ! $this->hasReference($name)) { $newReference = true; } $reference = $this->$name; if ( ! $reference && ! $this->getTable()->hasRelation($name)) { throw new Doctrine_Record_Exception( 'You can only call relatedExists() on a relationship that '. 'returns an instance of Doctrine_Record' ); } $exists = $reference ? $reference->exists() : false; if ($newReference) { $this->clearRelated($name); } return $exists; }
With this change, this method can be used to check newly created user objects or some retrieved from the db (left join with role) smoothly.
Hi, I was able to copy and paste your change out of the comment, but when I apply it and run the tests. It seems to break something, which I expected. Your changes allows collections through now and it calls a method that doesn't exist on Doctrine_Collection. Give your change a try and run the tests, maybe you can provide a more complete patch and we can include it for 1.2.2 release. Thanks, Jon