开发者

Doctrine: How can I have a where clause included only when necessary in a query?

I want to use a single query to retrieve:

  • items of any categories (no filter applied);
  • only items of a single category (limited to a particular category);
  • 开发者_如何学JAVA

For that purpose I should be able to write a Doctrine query which would include a where clause only when certain condition is met (eg. part of URL existing), otherwise, where clause is not included in the query.

Of course, i tried with using the If statement, but since doctrine query is chained, the error is thrown.

So i guess the solution might be some (to me unknown) way of writing doctrine queries in an unchained form (by not having each row started with "->" and also having each row of a query ending with semicolon ";")

That way the usage of IF statement would be possible i guess.

Or, maybe there's already some extremely simple solution to this matter?

Thanks for your reply!


I am unfamiliar with Codeigniter but can't you write something like this?

$q = Doctrine_Query::create()
    ->from('items');

if ($cat)
    $q->where('category = ?', $cat);


In your model pass the condition for where as a parameter in a function. In below example i am assuming the function name to be filter_query() and passing where condition as a parameter.

function filter_query($condition=''){

$this->db->select('*');
$this->db->from('TABLE NAME');
   if($condition != ''){
    $this->db->where('condition',$condition);
   }
}

In above example i have used Codeigniter's Active Record Class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜