简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM