[英]Using a contain in a find with HABTM relationship in cakephp
I have a HABTM relationship between Entities and Countries. 我在实体和国家之间建立了HABTM关系。 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; 27包含1064:您的SQL语法有误; 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
. 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在行1 0 28 SELECTCountry
'contain'附近使用。id
,Country
.id
,Country
。name
,Country
.name
,Country
。hash
FROMcountries
ASCountry
WHERECountry
. 从countries
Country
Country
hash
countries
Country
Country
。id
= 78 LIMIT 1 1 1 1 29 SELECTEntity
.id
= 78 LIMIT 1 1 1 1 29 SELECTEntity
。id
,Entity
.id
,Entity
。ticker
,Entity
.ticker
,Entity
。shares_outstanding
,Entity
.shares_outstanding
,Entity
。begin_fiscal_calendar
,Entity
.begin_fiscal_calendar
,Entity
。last_reported_quarter
,Entity
.last_reported_quarter
,Entity
。next_report_date
,Entity
.next_report_date
,Entity
。obsolete_groups
,Entity
.obsolete_groups
,Entity
。actual_fiscal_year_end
,Entity
.actual_fiscal_year_end
,Entity
。full_name
,Entity
.full_name
,Entity
。depricated
,Entity
.depricated
,Entity
。obsolete_country_id
,Entity
.obsolete_country_id
,Entity
。ticker2
,Entity
.ticker2
,Entity
。hash
,CountriesEntity
.hash
,CountriesEntity
。id
,CountriesEntity
.id
,CountriesEntity
。country_id
,CountriesEntity
.country_id
,CountriesEntity
。entity_id
,CountriesEntity
.entity_id
,CountriesEntity
。default_country
FROMentities
ASEntity
JOINcountries_entities
ASCountriesEntity
ON (CountriesEntity
.country_id
= 78 ANDCountriesEntity
.entity_id
=Entity
.id
) 32 32 1default_country
FROMentities
ASEntity
JOINcountries_entities
ASCountriesEntity
ON(CountriesEntity
。country_id
= 78和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. 作为备份计划,我可以编写代码以筛选$ blap中的结果并提取所需的值,但我认为在find命令本身中将有某种过滤方法。
if you are calling find on Country
model it is the one you should contain, like this: $this->Entity->Country->contain()
. 如果要在Country
模型上调用find,则应包含该模型,例如: $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'))
. 尝试这样调用: $this->Entity->Country->contain(array('fields' => 'ticker'))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.