简体   繁体   English

dql查询构建器Doctrine 1.2

[英]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. 此时只有一个参数,但在某些情况下可能会出现诸如用户名,标记名等。其中一些可能为null或为空。 I just need simple StringBuilder implementation for those. 我只需要简单的StringBuilder实现即可。 I have try to do DQL query with LEFT JOIN, but I have no idea how to do DQL querys? 我已经尝试使用LEFT JOIN进行DQL查询,但是我不知道如何进行DQL查询?

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.... ). 使用Doctrine 2,您可以以SQL方式编写DQL( SELECT * FROM table t.... )。

In Doctrine 1.x, what you could do is built the query in different stages. 在Doctrine 1.x中,您可以做的是在不同阶段构建查询。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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