dql query builder Doctrine 1.2
Is there easier way to build querys in doctrine then this. At this point there is only one parameter, but some case there could be like username, tagname, etc. Some of those could be null or empty. I just need simple StringBuilder implementation for those. I have try to do DQL query with LEFT JOIN, but I have no idea how to do DQL 开发者_C百科querys?
public function getTagsByApiKey($apikey='', $limit = 20){
$whereArray = array();
$whereClauseArray = array();
if($apikey != ''){
array_push($whereClauseArray, ' f.apikey = :apikey ');
$whereArray[':apikey'] = $apikey;
}
$whereClause = '';
for ($i=0; $i < sizeof($whereClauseArray); $i++) {
if($i>0){
$whereClause .= ' AND ';
}
$whereClause .= $whereClauseArray[$i];
}
$q = Doctrine_Query::create()
->from('Tag t')
->leftJoin('t.Feedback f')
->where($whereClause, $whereArray)
->orderBy('t.count ASC')
->limit($limit);
return $q->execute();
}
With Doctrine 2, you can write DQL in a SQL manner (SELECT * FROM table t....
).
In Doctrine 1.x, what you could do is built the query in different stages.
This is just an easy example without sense so you see what I mean:
$q = Doctrine_Query::create()
->from('Tag t')
->leftJoin('t.Feedback f');
$array = array("user" => "frank", "tag" => "music");
foreach($array as $key => $value) {
$q = $q->andWhere("t.$key = ?", $value);
}
$q = $q->orderBy('t.count ASC')
->limit($limit);
return $q->execute();
精彩评论