2012-02-06 35 views
1

內的另一種模式我有一個名爲message.php兩款車型和user.phpCakePHP的使用模式

在message.php,我有以下計數#NO驗證的用戶的郵件的方法。

<?php 
class Message extends AppModel { 
    ... 
    ... 
    ... 
    function getInboxCount($userId) { 
     // Here I want to get list of ids of verified users. It means I need to use User model for this. How is it possible? 
     // $ids = $this->User->find('list', array('conditions' => array('User.status' => 'verified'))); Will this work? 
    } 
} 
?> 

那麼如何在消息模型中使用用戶模型?

$this->User->find(...); 

如果他們不相關的,你可以在任何導入任何其他型號:

回答

12

如果兩個模型以任何方式相關的(消息屬於關聯用戶左右),你可以簡單地訪問它使用時間:

$User = ClassRegistry::init('User'); 
$User->find(...); 
+4

這在CakePHP <2.0中是正確的,但在CakePHP> = 2.0中沒有另一種方法來做到這一點? – linkyndy 2012-02-12 16:00:54

+1

你可以使用下面的'$ this-> loadModel('ModelName');' – gonzo 2014-01-17 21:09:09