开发者

How to execute query with subqueries on a table and get a Rowset object as a result in Zend?

I'm currently struggling on how to execute my query on a Table object in Zend and get a Rowset in return. Reason I need particularly THIS is because I'm modifying a code for existing project and I don't have much flexibility.开发者_JAVA技巧

Query:

    SELECT * 
      FROM  `tblname` ud
     WHERE ud.user_id = some_id
       AND
       (
            (ud.reputation_level > 1)
            OR
            (
                (SELECT COUNT( * )
                  FROM  `tblname` t
                 WHERE t.user_id = ud.user_id
                   AND t.category_id <=> ud.category_id
                   AND t.city_id <=> ud.city_id
                ) = 1
            )
        )

Is there a way to describe this query using Select object?

Previous SQL solution was very simple and consisted of one WHERE clause:

$where = $this->getAdapter()->quoteInto("user_id = ?",$user_id);
return $this->fetchAll($where);

I need to produce same type of the result (so that it could be processed by existing code) but for more complicated query.

Things I've tried

$db = Zend_Db_Table::getDefaultAdapter();
return $db->query($sql)->fetchAll();
----------------  OR  ----------------------
return $this->fetchAll($select);
----------------  OR  ----------------------
return $this->_db->query($sql)->fetchAll(); 

But they either produce arrays instead of objects or fail with Cardinality violation message.

I would appreciate any help on how to handle SQL text queries in Zend.


$dbAdapter = Zend_Db_Table::getDefaultAdapter();

//change the fetch mode becouse you don't like the array
$dbAdapter->setFetchMode(Zend_Db::FETCH_OBJ);

$sql = "you're long sql here";
$result = $dbAdapter->fetchAll($sql);
Zend_Debug::dump($result);
exit;

For a list of all fetch modes go to Zend_Db_Adapter

To write you're query using Zend_Db_Select instead of manual string , look at Zend_Db_Slect

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜