Tataller
Tataller

Reputation: 25

instruction IF in Doctrine query in Symfony?

How can i make something:

  public function executeSearch(sfWebRequest $request)
  {
    $test = 1;

    $this->shows = Doctrine_Core::getTable('Show')
      ->createQuery('a')
      ->where('a.name = ?', 'Tom')
   if ($test == 1) {   ->andWhere('a.last_name = ?', 'Jerry') }
      ->execute();
  }

this doesn't work. How can i instruction if in Doctrine query?

OR:

  public function executeSearch(sfWebRequest $request)
  { 
    if(isset($test)){
    $test = 'Jerry';
    }
  //now if isset $test then andWhere search only with Jerry, if not isset andWhere search ALL

    $this->shows = Doctrine_Core::getTable('Show')
      ->createQuery('a')
      ->where('a.name = ?', 'Tom')
      ->andWhere('a.last_name = ?', $test) }
      ->execute();
  }

Upvotes: 1

Views: 848

Answers (1)

Dziamid
Dziamid

Reputation: 11581

$q = Doctrine_Core::getTable('Show')
  ->createQuery('a')
  ->where('a.name = ?', 'Tom');

if ($test == 1) 
{   
  $q->andWhere('a.last_name = ?', 'Jerry');
}

$this->shows = $q->execute();

Upvotes: 5

Related Questions