将普通的SQL查询转换为Zend_Db?

时间:2013-01-14 04:41:54

标签: database zend-framework

我无法将以下查询转换为Zend_Db:

SELECT `mfaq`.* FROM `m_faq` AS `mfaq`
WHERE (mfaq.delete_flg <> 'D' OR mfaq.delete_flg IS NULL)
AND ((mfaq.title like '%$title%')
OR (mfaq.title like '%$title%')
AND (mfaq.title like '%$title%') 
OR (mfaq.title like '%$title%'))
ORDER BY `create_date` DESC 

一些帮助???

1 个答案:

答案 0 :(得分:1)

首先,您需要创建dbtable模型:

class Application_Model_DbTable_Mfaq extends Zend_Db_Table_Abstract
{
    protected $_name = 'm_faq';
}

然后按如下方式使用:

$table = new Application_Model_DbTable_Mfaq();

$select = $table->getAdapter()->select()
            ->from(array('mfaq'=>$table->info(Zend_Db_Table::NAME)))
            ->where("( mfaq.delete_flg <> 'D'")
            ->orWhere("mfaq.delete_flg IS NULL )")
            ->where("( mfaq.title like ?", "%$title%")
            ->orWhere("mfaq.title like ? ", "%$title%")
            ->where(" mfaq.title like ?", "%$title%")
            ->orWhere("mfaq.title like ? )", "%$title%")
            ->order("create_date DESC");

// echo $select; // shows your sql
$results = $select->query()->fetchAll();

我希望你考虑到AND的优先级高于OR。