2012-02-14 26 views
1

我有一個公司模型這是關於一個MonthlyReturn模型。一家公司可能會有許多月度回報。我試圖獲得一系列月度回報爲每月的所有公司。使用查找以使用基於相關模型的條件返回值

我使用的代碼如下:

$this->Company->find('all', array('contain' => array(
    'MonthlyReturn' => array(
     'conditions' => array('MonthlyReturn.month' => "2012-01-01") 
    ) 
))); 

本公司產品型號:

public $hasMany = array(
     'Employee' => array(
      'className' => 'Employee', 
      'foreignKey' => 'company_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ), 
     'MonthlyReturn' => array(
      'className' => 'MonthlyReturn', 
      'foreignKey' => 'company_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ) 
    ); 

    public $hasOne = 'Umuser'; 

} 

此代碼返回所有的公司,而不是那些月回報。 'MonthlyReturn.month'字段將始終採用上述格式,即年和月將會更改,但始終是該月份的第一個月份。

任何意見,將不勝感激。

+0

我認爲它很難給出一個完美的答案,但不知道公司的datamodel,但我會試一試;) – alexdd55 2012-02-14 14:31:42

+1

包容行爲ior也可用? – alexdd55 2012-02-17 08:52:56

+0

對不起@ alexdd55,我不明白你的問題?你能否詳細說明一下。 – 2012-02-17 10:51:01

回答

0

你的代碼說,你問一個具體的日期,而不是一個月。

試試這個:

$this->Company->find('all', array('contain' => array(
'MonthlyReturn' => array(
    'conditions' => array('MonthlyReturn.month LIKE' => "2012-01%") 
)))); 
+0

該公司的型號: – 2012-02-14 14:34:28

+0

我已經更新我的初始職位,因爲你會看到日期將以特定的格式。 – 2012-02-14 14:41:14

0

我不知道是否有更好的辦法,但你可以在兩個呼叫這樣做:

$company_ids = $this->MonthlyReturn->query("SELECT DISTINCT company_id FROM monthy_returns WHERE month = '2012-01-01';") 

$this->Company->find('all', array(
    'conditions' => array('Company.id' => $company_ids[0]), 
    'contain' => array('MonthlyReturn' => array(
     'conditions' => array('MonthlyReturn.month' => "2012-01-01") 
    ) 
))); 

這就是這個想法,但一些調試可能需要