Patrick
Patrick

Reputation: 335

Symfony and DQL syntax

I apologize for the beginner question, but I'm struggling with what seems should be a simple query. I can get it to work perfectly in MySQL, but can't get it to work in my symfony application. Essentially, this is all I'm trying to do:

SELECT * 
FROM benefits b
WHERE b.company_id = X

X = 
SELECT id
FROM company c
WHERE c.user_id = ($this->getUser()->getGuardUser()->getId())

I've tried many join statements but still can't get it to work with the result I want, such as:

public function executeIndex(sfWebRequest $request)
{
$this->benefitss = Doctrine_Core::getTable('benefits')
->createQuery('b')
->where('user_id = ?', '$this->getUser()->getGuardUser()->getId()')
->leftJoin('b.Company c')
->andWhere('c.user_id = ?', '$this->getUser()->getGuardUser()->getId()')
->execute();      
}

Here's my condensed schema:

Benefits
 columns
  id
  user_id
  company_id
  name
 relations
  User {class: sfGuardUser}
  Company 

Company
 columns
  id
  user_id
  name
 relations
  User {class: sfGuardUser}

sfGuardUser
 columns
  id

also of note: the user is not assigned a company_id b/c not all users will have companies according to my user definitions.

Any help for this wretched beginner would be much appreciated. :)

Update 8/27/11:

This gets the results I want:

$this->benefitss = Doctrine_Core::getTable('benefits')
->createQuery('b')
->leftJoin('b.Company c')
->Where('c.user_id = ?', '1')
->execute();      

Where the '1' needs to be the current user's id. But when I change the where clause to:

->Where('c.user_id = ?', '$this->getUser()->getGuardUser()->getId()')

I get no results.

Any ideas?

Upvotes: 0

Views: 323

Answers (2)

Patrick
Patrick

Reputation: 335

I got it figured. Here's how (I'm obviously a beginner):

    $this->benefitss = Doctrine_Core::getTable('benefits')
    ->createQuery('b')
    ->leftJoin('b.Company c')
    ->Where('c.user_id = ?', $this->getUser()->getGuardUser()->getId())
    ->execute();      

Upvotes: 0

plandolt
plandolt

Reputation: 1931

Don't put $this->getUser()->getGuardUser()->getId() in ''.

Upvotes: 2

Related Questions