2012-02-21 68 views
0

我在實體和國家之間有HABTM關係。下面發現返回我想要的數據,但我想以削減下來,從實體只返回股票領域:在cakephp中使用包含與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行的'contains'附近使用。0 28 SELECT Countryid, CountrynameCountryhash FROM countries AS Country WHERE Countryid = 78限制1 1 1 1 29選擇Entityid, EntitytickerEntityshares_outstanding, Entitybegin_fiscal_calendar,Entitylast_reported_quarterEntitynext_report_date,Entityobsolete_groups, Entityactual_fiscal_year_endEntityfull_name, EntitydepricatedEntityobsolete_country_idEntityticker2EntityhashCountriesEntityid, CountriesEntitycountry_id,CountriesEntityentity_id, CountriesEntitydefault_country FROM entities AS Entity JOIN countries_entities AS CountriesEntity ON (CountriesEntitycountry_id = 78和CountriesEntityentity_id = Entityid)32 32 1

作爲備份計劃我可以編寫代碼通過的結果進行篩選在$ blap中,並提取出我想要的值,但我認爲有一些方法可以在find命令本身內進行過濾。

回答

0

如果您打電話尋找Country型號,則應該包含此型號,如下所示:$this->Entity->Country->contain()

此外,您不能直接將字段放在包含調用上。 試試像這樣調用:$this->Entity->Country->contain(array('fields' => 'ticker'))