cakephp在模型中使用另一个模型

gau*_*kum 1 cakephp cakephp-1.3 cakephp-2.0

我有两个名为message.php和的模型user.php

在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?
   }
}
?>
Run Code Online (Sandbox Code Playgroud)

那么如何在Message模型中使用User模型呢?

dec*_*eze 12

如果两个模型以任何方式关联(消息属于用户左右),则只需使用以下命令即可访问它:

$this->User->find(...);
Run Code Online (Sandbox Code Playgroud)

如果它们没有关联,您可以随时使用以下方法导入任何其他模型:

$User = ClassRegistry::init('User');
$User->find(...);
Run Code Online (Sandbox Code Playgroud)

  • 这在CakePHP <2.0中是正确的,但是在CakePHP> = 2.0中没有另外一种方法吗? (4认同)