I have a situation where I want to assemble a Doctrine select query based on weather certain params are empty or not For example, if there was a $slug variable that was optional, i'd want something like this:
function get_bio($slug = '')
{
$q = Doctrine_Query::create()
->from('Bio b');
if (!empty($slug))
{
$q .= $q->where('b.slug = ?', $slug);
}
}
I know thats not the correct syntax, but how would I assemble something like that?
You are treating the query object as a string rather than an object. Try this instead:
Note the call to
$q->where()operates directly on the query object$q, so you do not have to assign the return value to anything (it returns a reference to the query object itself so that you can chain method calls).Note also that if you are planning on adding multiple
whereclauses, you will probably want to useandWhere()instead ofwhere().