使用magento命令连接自定义表 [英] joining custom tables using magento commands

查看:80
本文介绍了使用magento命令连接自定义表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用magento的命令连接两个自定义表.搜索后,我遇到了此通用块代码

I have been trying to join two custom table using magento's commands. After searching i came across this block of generic code

$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 
'main_table.foreign_id = table_alias.primary_key',
 array('table_alias.*'), 
'schema_name_if_different');

以此为模板,我尝试将表连接在一起,但仅返回诸如incorrect table nametable doesn't exist之类的错误或其他错误.

Following this as template I have tried to join my tables together but have only returned errors such as incorrect table name or table doesn't exist or some other error.

只要清理一下,有人可以在我的理解下纠正我

Just to clear things up can someone please correct me on my understanding

$collection = Mage::getModel('module/model_name')->getCollection();

获取模型的实例.在该模型中的表是保存所需数据的表(在本示例中,我将表称为p)

Gets an instance of your model. Within that model is the table that holds the required data (for this example I shall call the table p)

$collection->getSelect()

从表p中选择数据

->join()

需要三个参数才能将两个表连接在一起

Requires three parameters to join two table together

PARAM1

array('table_alias'=>$this->getTable('module/table_name'))

您赋予表格的别名" =>您要添加到集合中的表格(已在模型文件夹中设置)"

'the alised name you give the table' => 'the table you want to add to the collection (this has been set up in the model folder)'

PARAM2

'main_table.foreign_id = table_alias.primary_key'

我不明白这一点(虽然看起来很简单)

This bit i don't get (it seems straight forward though)

我的主表(p)没有外部ID(它具有主键-也是它的外部ID)吗?

my main table (p) doesn't have a foreign id (it has it's primary key - is that also its foreign id)?

必须等于您在param1中给定的别名

has to be equal to the alised name you gave in param1

PARAM3

'main_table.foreign_id = table_alias.primary_key'

从别名中获取全部

我的理解哪里出错了?

推荐答案

请在下面的sql join语句中查看,我在我的项目中正在使用它,并且它运行良好.

Please have a look in below sql join statement, I am using it in my project and it is working perfectly.

语法

$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()->getTablePrefix().'table_name_for_join.join_table_field', array('field_name_you_want_to_fetch_from_db'));

工作查询示例

$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id', array('value'));

希望这对您有用!!

这篇关于使用magento命令连接自定义表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆