我做了find('all')
一個相當複雜的條件:CakePHP的:如何當有多個相關記錄指定返回的字段
array(
'conditions' => array(
(int) 0 => 'Attempt.test_id = 8',
(int) 1 => 'Attempt.score > 60',
(int) 2 => array(),
(int) 3 => array(
(int) 0 => 'Resume.has_file = 1'
)
),
'joins' => array(
(int) 0 => array(
'table' => 'attempts',
'alias' => 'Attempt',
'type' => 'LEFT',
'conditions' => array(
(int) 0 => 'Attempt.user_id = User.id AND Attempt.test_id != 5'
)
)
),
'contain' => array(
(int) 0 => 'Resume',
(int) 1 => 'Attempt',
(int) 2 => 'Tag'
),
'group' => 'User.id',
'limit' => (int) 1,
'fields' => array(
(int) 0 => 'User.id',
(int) 1 => 'Resume.id'
)
)
返回的數據是這樣的:
array(
(int) 0 => array(
'User' => array(
'id' => '381'
),
'Resume' => array(
'id' => '15'
),
'Attempt' => array(
(int) 0 => array(
'id' => '16072',
'user_id' => '381',
'test_id' => '8',
'status' => 'complete'
),
(int) 2 => array(
'id' => '16073',
'user_id' => '381',
'test_id' => '8',
'status' => 'complete'
)
查詢是永久運行,需要一天運行,我只需要User.id,所以我試圖去掉不需要的字段。它適用於hasOne關聯,但hasMany與文檔中的語法不匹配。例如,'limit'=>array('Attempt.id')
會拋出錯誤,因爲沒有$ user ['Attempt'] ['id']。相反,它是$ user ['Attempt'] [#] ['id']。
我該怎麼做?我也很想聽聽任何其他的建議,這將加快這個查詢。
謝謝吉列爾莫。我應該澄清,即使這個查詢在標籤模型上沒有任何條件,他們中的許多人都會。所以我無法擺脫那個或任何關聯。 – emersonthis
好吧,也許如果你告訴我們這個完整的條件以及你需要從這個查詢中得到什麼,我們可以更好地幫助你。 –
這些是完整的條件集。我只需要獲取User.id.所有其他內容只是過濾器。各種條件引用這些表格。 – emersonthis