【问题标题】:Getting data from 2 tables in magento从magento中的2个表中获取数据
【发布时间】:2014-12-30 12:24:42
【问题描述】:

我在 magento 中创建了 2 个文本以及 2 个不同的表。第一个扩展将数据存储在表 1 中,而第二个扩展将数据存储在表 2 中。现在我想通过 LeftJoin 在第一个扩展中显示数据。它显示第一个表中没有左连接的数据,但不显示两个表中的左连接数据。
block.php 中的这段代码

public function methodblock()
 {
    $collection = Mage::getModel('test/test')->getCollection();

    $returnCollection = $collection->getSelect()
    ->joinLeft('magento_answer', 'id_pfay_test=question_id', 
    array('*'), null , 'left');


     return $returnCollection;
 }

在布局方面。显示数据.phtml

<?php 
$collection =  $this->testmethodblock(); 
foreach($collection as $rows {
    echo $rows ->getData('name');
}

【问题讨论】:

  • 您遇到了什么错误?看看这个答案是否有帮助:stackoverflow.com/a/19401573/1076075
  • 我没有显示任何错误。当我回显 $returnCollection.它给出了这个查询。选择 main_table.*, magento_answer.* FROM magento_pfay_test AS main_table LEFT JOIN magento_answer ON id_pfay_test=question_id 。此查询在 mysql db 上正常运行。
  • 尝试打印查询并检查问题出在哪里。在返回函数之前写 echo $collection->getSelect();exit;

标签: php mysql magento magento-1.9.1


【解决方案1】:

我得到了答案。我使用适合我的自定义查询。

$resource = Mage::getSingleton('core/resource');
        $readConnection = $resource->getConnection('core_read');
        $qTable   = $resource->getTableName('pfay_test');
        $aTable   = $resource->getTableName('answer/answer');
        $query = 'SELECT * FROM  '.$qTable.'  q  left join '.$aTable.' a ON  a.question_id=q.id_pfay_test';
        $results = $readConnection->fetchAll($query); 
         return $results;

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多