0

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);

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!

developer10
  • 1,450
  • 2
  • 15
  • 31

2 Answers2

1

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);
AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • This seems as it might work. It meets all conditions to work, ie. semicolon is at the end so the IF clause wont interfere with the query itself but rather insert (or not) that extra line (where clause when necessary), just like i did before with CI Active record. I'll give it a try and let you know if it worked. Thanks! – developer10 Nov 07 '10 at 09:34
  • Just to confirm I managed to use the code in this fashion and it works! SOLVED – developer10 Nov 08 '10 at 21:47
0

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.

Alpesh
  • 5,307
  • 2
  • 19
  • 19
  • Well, the thing is i'm actually making a rewrite from CI's Active record into Doctrine so i already have working code written in Active Record. But thanks anyway!! – developer10 Nov 07 '10 at 09:32