简体   繁体   English

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

[英]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 SELECT Country 'contain'附近使用。 id , Country . idCountry name , Country . nameCountry hash FROM countries AS Country WHERE Country . countries Country Country hash countries Country Country id = 78 LIMIT 1 1 1 1 29 SELECT Entity . id = 78 LIMIT 1 1 1 1 29 SELECT Entity id , Entity . idEntity ticker , Entity . tickerEntity shares_outstanding , Entity . shares_outstandingEntity begin_fiscal_calendar , Entity . begin_fiscal_calendarEntity last_reported_quarter , Entity . last_reported_quarterEntity next_report_date , Entity . next_report_dateEntity obsolete_groups , Entity . obsolete_groupsEntity actual_fiscal_year_end , Entity . actual_fiscal_year_endEntity full_name , Entity . full_nameEntity depricated , Entity . depricatedEntity obsolete_country_id , Entity . obsolete_country_idEntity ticker2 , Entity . ticker2Entity hash , CountriesEntity . hashCountriesEntity id , CountriesEntity . idCountriesEntity country_id , CountriesEntity . country_idCountriesEntity entity_id , CountriesEntity . entity_idCountriesEntity 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 default_country FROM entities AS Entity JOIN countries_entities AS CountriesEntity ON( CountriesEntitycountry_id = 78和CountriesEntityentity_id = Entityid )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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM