0
 $q = Mage::getModel('qa/post')->getCollection()->addProductFilter(1)->printLogQuery(true);

    public function addProductFilter($productId)
    {
        $this->addFieldToFilter('qa_post_relation.product_id', $productId);
        return $this;
    }

返回错误:

a:5:{i:0;s:94:"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'qa_post_relation.product_id' in 'where clause'";i:1;s:2016:"#0 C:\xampp\htdocs\ahw\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#1 C:\xampp\htdocs\ahw\lib\Zend\Db\Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 C:\xampp\htdocs\ahw\lib\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array)
#3 C:\xampp\htdocs\ahw\lib\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT `main_ta...', Array)
#4 C:\xampp\htdocs\ahw\lib\Varien\Db\Adapter\Pdo\Mysql.php(419): Zend_Db_Adapter_Pdo_Abstract->query('SELECT `main_ta...', Array)
#5 C:\xampp\htdocs\ahw\lib\Zend\Db\Adapter\Abstract.php(734): Varien_Db_Adapter_Pdo_Mysql->query('SELECT `main_ta...', Array)
#6 C:\xampp\htdocs\ahw\lib\Varien\Data\Collection\Db.php(734): Zend_Db_Adapter_Abstract->fetchAll('SELECT `main_ta...', Array)
#7 C:\xampp\htdocs\ahw\app\code\core\Mage\Core\Model\Resource\Db\Collection\Abstract.php(521): Varien_Data_Collection_Db->_fetchAll('SELECT `main_ta...', Array)
#8 C:\xampp\htdocs\ahw\lib\Varien\Data\Collection\Db.php(566): Mage_Core_Model_Resource_Db_Collection_Abstract->getData()
#9 C:\xampp\htdocs\ahw\lib\Varien\Data\Collection.php(741): Varien_Data_Collection_Db->load()
#10 C:\xampp\htdocs\ahw\app\code\local\AJW\Qa\controllers\IndexController.php(25): Varien_Data_Collection->getIterator()
#11 C:\xampp\htdocs\ahw\app\code\core\Mage\Core\Controller\Varien\Action.php(419): AJW_Qa_IndexController->indexAction()
#12 C:\xampp\htdocs\ahw\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#13 C:\xampp\htdocs\ahw\app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#14 C:\xampp\htdocs\ahw\app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#15 C:\xampp\htdocs\ahw\app\Mage.php(683): Mage_Core_Model_App->run(Array)
#16 C:\xampp\htdocs\ahw\index.php(87): Mage::run('', 'store')
#17 {main}";s:3:"url";s:5:"/qas/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:7:"default";}

查询:SELECT main_table.* FROM qa_postsAS main_tableWHERE (qa_post_relation.product_id = '1') 在“where 子句”中失败未知列“qa_post_relation.product_id”

然而:

选择 * 从qa_posts_relation 哪里product_id=1

返回一个结果 我很难过殴打我的妻子 (jk) 之类的东西。

4

1 回答 1

0

我想通了,它缺少“关系”和主表之间的连接。

public function joinRel()
    {
        $this->setFlag('relation', true);
        $this->getSelect()->joinLeft(
            array('relation' => $this->getTable('qa/relation')),
            'main_table.postid=relation.post_id'
        );
        return $this;
    }
于 2013-07-18T01:13:31.417 回答