在 cakephp 中使用包含在具有 HABTM 关系的查找中
我在实体和国家之间有 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_id
、Entity
.ticker2
、Entity
.hash
、CountriesEntity
.id< /代码>,
CountriesEntity
.country_id
、CountriesEntity
.entity_id
、CountriesEntity
.default_country
FROMentities
ASEntity
JOINcountries_entities
ASCountriesEntity
ON (CountriesEntity
.country_id
= 78 ANDCountriesEntity
.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 SELECTCountry
.id
,Country
.name
,Country
.hash
FROMcountries
ASCountry
WHERECountry
.id
= 78 LIMIT 1 1 1 1 29 SELECTEntity
.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
FROMentities
ASEntity
JOINcountries_entities
ASCountriesEntity
ON
(CountriesEntity
.country_id
= 78 ANDCountriesEntity
.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在
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'))
.