tags:

views:

21

answers:

1

Hi,

this code example from doctrine 1.2 manual gives me one phonenumber and areacode for the user:

$q = Doctrine_Query::create()
->from('User u')
->leftJoin('u.Phonenumbers p')
->where('u.id = ?', 1);

$user = $q->fetchOne();

echo $user->Phonenumbers[0]['phonenumber'];
echo $user->Phonenumbers[0]['areacode'];

Is it possible to get the specific phonenumber with areacode = 123 without looping Phonenumbers[]?

A: 

try this:

$q = Doctrine_Query::create()
->from('User u')
->leftJoin('u.Phonenumbers p')
->where('u.id = ? AND p.areacode = ?', 1,$areacode);

$user = $q->fetchOne();

echo $user->Phonenumbers[0]['phonenumber'];
echo $user->Phonenumbers[0]['areacode'];
ITroubs
Thanks, this would possibly work, but at the time i fetch the user object in my code i don't know the areacode.
marco
why not moving the fetch to the right place where the areacode is already known?
ITroubs
I cannot do that. The $user is used on all my pages, so i fetch it in a very early stage in a base class. Depending on the selected page i would have to write n various where statements...
marco
I have rechecked my code and it is possible to use ITroubs code with a small correction. The correct format for the where statement is: ->where('u.id = ?', $id)->andWhere('p.areacode = ?', $areacode)
marco
as far as i know it is possible to do it both ways mine and yours ;-)
ITroubs