选择没有“e.”或“main_table”的字段 - Magento
这个问题源于我在这个论坛上的其他问题之一。我创建了一个自定义模块并覆盖了“Review”模型的核心文件。它允许我将数据保存到我的自定义字段“电子邮件”中。但是当我尝试检索该字段的值时,它根本无法识别我的字段。
这是我的查询,
Mage::getModel('review/review')->getCollection()
->addFieldToFilter('status_id' , '1')
->addFieldToFilter('email', $emailId)
->addFieldToSelect('email');
这就是我回显时得到的结果,
SELECT `main_table`.`email` AS `review_detail`, `detail`.`detail_id`, `detail`.`title`, `detail`.`detail`, `detail`.`nickname`, `detail`.`customer_id` FROM `review` AS `main_table` INNER JOIN `review_detail` AS `detail` ON main_table.review_id = detail.review_id WHERE (status_id = '1') AND (email = '[email protected]')
它显然试图从 main_table
中选择电子邮件,这是 review
表而不是 review_detail
代码>表。有没有办法选择没有 main_table
前缀的 email
字段,或者只是更改查询,以便从 review_detail
中选择 email
> 表?
请帮帮我。我遇到过与joins
相关的帖子,但不确定这是否是我的情况下的选项之一。提前致谢。
This question follows from one of my other questions on this forum. I have created a custom module and have overridden 'Review' model's core files. It allows me to save data to my custom field 'email' fine. But when I try to retrieve the values of this field, it doesn't recognize my field at all.
This is my query,
Mage::getModel('review/review')->getCollection()
->addFieldToFilter('status_id' , '1')
->addFieldToFilter('email', $emailId)
->addFieldToSelect('email');
And this is what I get when I echo it,
SELECT `main_table`.`email` AS `review_detail`, `detail`.`detail_id`, `detail`.`title`, `detail`.`detail`, `detail`.`nickname`, `detail`.`customer_id` FROM `review` AS `main_table` INNER JOIN `review_detail` AS `detail` ON main_table.review_id = detail.review_id WHERE (status_id = '1') AND (email = '[email protected]')
Its obviously trying to select email from main_table
which is review
table instead of review_detail
table. Is there a way to select email
field without main_table
prefix or just change the query such that it selects email
from review_detail
table?
Please help me out. I have come across posts related to joins
but not sure if that is one of the options in my case or not. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道问题已经很老了,但我也有同样的问题,所以对于将来的所有人来说,这就是最简单的方法,无需使用纯 sql:
它变成:
detail.email AS email< /代码>
I know the Problem is old, but I just had the same, so for all you people in the future, this is how you do it the easiest way, without using plain sql:
It becomes:
detail.email AS email
由于您没有在
addFieldToSelect
方法中为email
字段指定表别名,系统默认为main_table.email
如果您有看一下查询,它使用表别名
detail
连接到review_detail
。您应该能够通过指定
`detail`.email
而不仅仅是从
。review_detail
表中选择(和过滤)email
>电子邮件As you have not specified a table alias for the
email
field in theaddFieldToSelect
method, the system is defaulting it tomain_table.email
If you have a look at the query, it's joining onto the
review_detail
with a table alias ofdetail
.You should be able to select (and filter) on the
email
from thereview_detail
table by specifying`detail`.email
instead of justemail
.实际上您没有使用表别名,这就是为什么您从主表收到电子邮件。
您将使用 'detail' .email = '[电子邮件受保护]'
Actually you are not using table alias name thats why, you are getting email from main table.
You will use 'detail' .email = '[email protected]'
试试这个方法:
Try this way:
现在让它运行的快速方法是我直接在代码中传递 SQL 查询。我知道这不是正确的方法,但我尝试过的任何事情肯定没有帮助。这是我的代码:
我知道这不是编写 magento 代码的正确方法,但为了让它快速运行,我别无选择。我会继续寻找合适的解决方案,并在得到解决方案后进行修改。如果其他人能想到用 Magento 方式写同样的东西,请告诉我。
感谢迄今为止大家的帮助。
Well the quick way to get it running for now is that I have directly passed a SQL query in the code. I know that's not the right way to do it but any thing I tried was certainly not helping. This is my code:
I know this is not the right way to write a magento code but to get it working quickly, I had no other option. I would keep looking for a proper solution and would amend it once I get it. If anyone else could think of writing this same thing but the Magento way please let me know.
Thanks for your help so far every one.