php - 从magento中的2个表获取数据

标签 php mysql magento magento-1.9.1

我在 magento 中创建了 2 个扩展以及 2 个不同的表。第一个扩展将数据存储在表 1 中,而第二个扩展将数据存储在表 2 中。现在我想通过 LeftJoin 在第一个扩展中显示数据。它显示第一个表中没有 leftjoin 的数据,但不显示两个表中带有 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;
 }

在布局方面。 dislplaydata.phtml

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

最佳答案

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

$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;

关于php - 从magento中的2个表获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705443/

相关文章:

php - PHP通过xhr获取数据

php - 需要有关 OAuthException 代码 2500 的帮助

php - ajax发布成功后如何设置隐藏字段的值?

python - 使用 Python 和 SQL 在 SQL INSERT & "commit"之后未显示新行

mysql - 监视 MySQL 中未使用的索引

xml - 使用 local.xml 更改或重新排列 Magento 登录和注销(顶部链接)位置

Magento 覆盖 getPrice()

Magento:保存管理设置时出现 "configForm is not defined"错误

php - 用于代码重用的特征与抽象类

php - 无法在mysql中添加子查询别名