Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Magento连接不同的表_Php_Mysql_Magento_Left Join - Fatal编程技术网

Php Magento连接不同的表

Php Magento连接不同的表,php,mysql,magento,left-join,Php,Mysql,Magento,Left Join,我的Grid.php文件中有以下代码: function _prepareCollection () { $collection = Mage::getResourceModel($this->_getCollectionClass()); $collection->getSelect()->joinLeft( array('sfog' => 'sales_flat_order_grid'), 'main_table.entity_id = sfog.en

我的Grid.php文件中有以下代码:

function _prepareCollection () {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(
    array('sfog' => 'sales_flat_order_grid'),
    'main_table.entity_id = sfog.entity_id',
    array('sfog.shipping_name','sfog.billing_name')
);
$collection->getSelect()->joinLeft(
    array('sfo'=>'sales_flat_order'),
    'sfo.entity_id=main_table.entity_id',
    array(
        'sfo.customer_email',
        'sfo.weight',
        'sfo.discount_description',
        'sfo.increment_id',
        'sfo.store_id',
        'sfo.created_at',
        'sfo.status',
        'sfo.base_grand_total',
        'sfo.grand_total'
    )
);
我也想添加表sales\u order\u项目,但如果添加此表,则会出现以下错误:

具有相同id“119”的物料(Mage\ U销售\型号\订单)已存在


不管怎样?

这是因为自动递增值,它被用作
id
,它是唯一的来识别记录

假设您正在加入
soi
意思是
销售订单项目
,按
soi.item\u id
分组

$collection->getSelect()->group('soi.item_id');

显然,加入sales_order_项目会导致每个订单返回多行(这并不奇怪),Magento正试图将行数据加载到专门针对订单的集合中,该集合包含一个检查,以确保每个订单只发生一次。该检查正在产生您看到的错误

要解决此问题,您可以:

  • 将collection类更改为“sales/order\u item\u collection”,它允许每个订单项有一行
-或-

  • 不与sales\u order\u项目联接,而是通过
    getItemsCollection()

您必须将sales\u flat\u order\u item表的entity\u id列设置为集合的entity\u id列。您还必须重命名主表类的entity\u id列,以避免
entity\u id
列冲突

$collection->getSelect()->joinLeft(
   array('sfoi' => 'sales_flat_order_item'),
   'sfoi.order_id=sfo.entity_id', 
   array(
      'entity_id' => 'sfoi.item_id',  // Re-assign entity_id column
      'main_table_entity_id' => 'main_table.entity_id'  // Rename original entity_id column
   )
)

也许你可以用几句话解释一下你到底想从数据库中选择什么,这样我们就可以了解选择应该做什么。实际上我想显示“SKU、运输成本、小计、客户群”在Grid.php文件的sales页面上,将db结构放在@Ahsan上,您说您也想添加表sales\u order\u项-您能展示代码吗-您是如何尝试添加它的?今天遇到了这样的问题:如果Magento没有返回“main\u table\u entity\u id”=>“main\u table.entity\u id”字段,设置以下内容:'main_table_entity_id'=>'CONCAT(main_table.entity_id)'