magento flat和eav表连接的不同

对于flat表,也就是普通的表,例如订单之类的sales_flat_order,这类型的连接,Collection连接

 1     $table = Mage::getSingleton('core/resource')->getTableName('catalog_product_store');  
 2     $storetable = Mage::getSingleton('core/resource')->getTableName('core_store');  
 3                 $collection ->getSelect()->joinLeft(  
 4                             array('pstable' => $table),//表名称  
 5                             'e.entity_id=pstable.product_id',//表连接条件  
 6                             array('pstable.store_id as storeid')//选择出的字段  
 7                 );  
 8                 $collection ->getSelect()->joinLeft(  
 9                             array('storetable' => $storetable),//表名称  
10                             'pstable.store_id=storetable.store_id',//表连接条件  
11                             array('storetable.name as storename')//选择出的字段  
12                 );  

上述连接其实也适用于EAV模型的表,只是在grid过滤的情况下会出错。上述语句中

 1 storetable.name as storename  

其实是防止grid表中重复的索引,比如某一个表的索引列叫  'index' => 'name'。而这个时候选出的字段就需要起个另外的别名,通过尝试以后发现 上述 代码 是可以执行成功的,可以得到想要的结果。

对于eav模型的表连接,下述左连接来示例

 1     $collection ->joinField('storeid',  
 2                         'catalog_product_store',  
 3                         'store_id',  
 4                         'product_id=entity_id',  
 5                         null,  
 6                         'left');  
 7                $collection ->joinField('storename',  
 8                         'core_store',  
 9                         'name',  
10                         'store_id=storeid',  
11                         null,  
12                         'left');  

上述的连接其实是进行了两次连接,是有特点的,第一次连接的storeid用来作为第二次关联的条件。这种eav专属的连接,grid是不会过滤出错的。

原文地址:https://www.cnblogs.com/zhengyanbin2016/p/5737326.html