【问题标题】:Using a contain in a find with HABTM relationship in cakephp在 cakephp 中使用包含 HABTM 关系的查找
【发布时间】:2012-02-21 14:31:08
【问题描述】:

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

$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));

但这没有任何效果。

在调用 find 之前,我尝试在实体模型上调用 contains:

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

但这会产生以下错误消息

27 包含 1064:您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 1 行的“包含”附近使用的语法 0 28 SELECT Country.id, Country.name, Country.hash FROM countries AS Country WHERE Country.id = 78 限制 1 1 1 1 29 选择 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 加入 countries_entities AS CountriesEntity 开启 (CountriesEntity.country_id = 78 和CountriesEntity.entity_id = Entity.id) 32 32 1

作为备用计划,我可以编写代码来筛选 $blap 中的结果并提取我想要的值,但我认为会有一些方法可以在 find 命令本身中进行过滤。

【问题讨论】:

    标签: php cakephp cakephp-1.3


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多