在 cakephp 中使用包含在具有 HABTM 关系的查找中

发布于 2025-01-08 10:03:16 字数 2173 浏览 5 评论 0原文

我在实体和国家之间有 HABTM 关系。以下查找返回我想要的数据,但我想将其缩减为仅返回实体中的股票字段:

$blap = $this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78)));

所以我添加了一个像这样的包含:

$blap = $this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78),'contain'=>'Entity.ticker));

但这没有效果。

我尝试在实体模型上调用包含,然后像这样调用查找:

$this->Entity->contain('ticker');
$blap = $this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78)));

但这生成了以下错误消息

27 包含 1064:您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 第 1 0 28 行“contain”附近使用的语法 SELECT Country.id, 国家.名称国家.哈希 FROM 国家 AS 国家< /代码> 哪里 国家/地区.id = 78 LIMIT 1 1 1 1 1 29 选择实体.id, 实体.股票代码实体.shares_outstanding实体.begin_fiscal_calendar实体.last_reported_quarter实体next_report_date实体obsolete_groups实体.actual_fiscal_year_end实体.full_name实体.已停用实体.obsolete_country_idEntity.ticker2Entity.hashCountriesEntity.id< /代码>, CountriesEntity.country_idCountriesEntity.entity_idCountriesEntity.default_country FROM entities AS Entity JOIN countries_entities AS CountriesEntity ON (CountriesEntity.country_id = 78 AND CountriesEntity.entity_id = 实体.id) 32 32 1

作为备份计划,我可以编写代码来筛选 $blap 中的结果并提取我想要的值,但我认为有这将是在 find 命令本身中进行过滤的某种方式。

I have a HABTM relationship between Entities and Countries. The following find returns the data I want but I want to whittle it down to returning just the ticker field from entities:

$blap = $this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78)));

So I added a contain like this:

$blap = $this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78),'contain'=>'Entity.ticker));

but that had no effect.

I tried calling contain on the Entity model before calling the find like this:

$this->Entity->contain('ticker');
$blap = $this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78)));

But that generated the following error message

27 contain 1064: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'contain' at line 1 0 28 SELECT Country.id,
Country.name, Country.hash FROM countries AS Country WHERE
Country.id = 78 LIMIT 1 1 1 1 29 SELECT Entity.id,
Entity.ticker, Entity.shares_outstanding,
Entity.begin_fiscal_calendar, Entity.last_reported_quarter,
Entity.next_report_date, Entity.obsolete_groups,
Entity.actual_fiscal_year_end, Entity.full_name,
Entity.depricated, Entity.obsolete_country_id,
Entity.ticker2, Entity.hash, CountriesEntity.id,
CountriesEntity.country_id, CountriesEntity.entity_id,
CountriesEntity.default_country FROM entities AS Entity JOIN
countries_entities AS CountriesEntity ON
(CountriesEntity.country_id = 78 AND CountriesEntity.entity_id
= Entity.id) 32 32 1

As a backup plan I can write code to sift through the results in $blap and pull out the values I want but I thought that there would be some way to filter within the find command itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我最亲爱的 2025-01-15 10:03:16

如果您在 Country 模型上调用 find,那么您应该包含该模型,如下所示:$this->Entity->Country->contain()

此外,您不能将字段直接放在包含调用上。
尝试像这样调用:$this->Entity->Country->contain(array('fields' => 'ticker'))

if you are calling find on Country model it is the one you should contain, like this: $this->Entity->Country->contain().

Also, you can't put fields directly on the contain call.
Try calling like this: $this->Entity->Country->contain(array('fields' => 'ticker')).

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