选择没有“e.”或“main_table”的字段 - Magento

发布于 2024-12-28 07:22:04 字数 1108 浏览 0 评论 0原文

这个问题源于我在这个论坛上的其他问题之一。我创建了一个自定义模块并覆盖了“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怀里藏娇 2025-01-04 07:22:04

我知道问题已经很老了,但我也有同样的问题,所以对于将来的所有人来说,这就是最简单的方法,无需使用纯 sql:

Mage::getModel('review/review')
->getCollection()
->addExpressionFieldToSelect('email','detail.email');

它变成: 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:

Mage::getModel('review/review')
->getCollection()
->addExpressionFieldToSelect('email','detail.email');

It becomes: detail.email AS email

眼藏柔 2025-01-04 07:22:04

由于您没有在 addFieldToSelect 方法中为 email 字段指定表别名,系统默认为 main_table.email

如果您有看一下查询,它使用表别名 detail 连接到 review_detail

INNER JOIN `review_detail` AS `detail`

您应该能够通过指定 `detail`.email 而不仅仅是 review_detail 表中选择(和过滤)email >电子邮件

As you have not specified a table alias for the email field in the addFieldToSelect method, the system is defaulting it to main_table.email

If you have a look at the query, it's joining onto the review_detail with a table alias of detail.

INNER JOIN `review_detail` AS `detail`

You should be able to select (and filter) on the email from the review_detail table by specifying `detail`.email instead of just email.

怪异←思 2025-01-04 07:22:04

实际上您没有使用表别名,这就是为什么您从主表收到电子邮件。
您将使用 '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]'

始终不够爱げ你 2025-01-04 07:22:04

试试这个方法:

    Mage::getModel('review/review')->getCollection()
    ->addFieldToFilter('status_id' , '1')
    ->addFilter('my_cool_filter',
        $this->getConnection()->quoteInto('detail.email=?', $emailId),
        'string'
    );

Try this way:

    Mage::getModel('review/review')->getCollection()
    ->addFieldToFilter('status_id' , '1')
    ->addFilter('my_cool_filter',
        $this->getConnection()->quoteInto('detail.email=?', $emailId),
        'string'
    );
幻想少年梦 2025-01-04 07:22:04

现在让它运行的快速方法是我直接在代码中传递 SQL 查询。我知道这不是正确的方法,但我尝试过的任何事情肯定没有帮助。这是我的代码:

  $resource = Mage::getSingleton('core/resource');
  $readConnection = $resource->getConnection('core_read');
  $query = "SELECT a.review_id, b.email FROM  review AS a INNER JOIN review_detail AS b WHERE a.review_id = b.review_id AND a.status_id = 1 AND b.email = '" . $emailId. "'";  
  $results = $readConnection->fetchAll($query);

我知道这不是编写 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:

  $resource = Mage::getSingleton('core/resource');
  $readConnection = $resource->getConnection('core_read');
  $query = "SELECT a.review_id, b.email FROM  review AS a INNER JOIN review_detail AS b WHERE a.review_id = b.review_id AND a.status_id = 1 AND b.email = '" . $emailId. "'";  
  $results = $readConnection->fetchAll($query);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文