gautamlakum
gautamlakum

Reputation: 12015

cakephp use another model within model

I have two models named message.php and user.php

In message.php, I have following method that counts #no of messages of verified users.

<?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?
   }
}
?>

So how can I use User model in Message model?

Upvotes: 1

Views: 5434

Answers (1)

deceze
deceze

Reputation: 521995

If the two models are associated in any way (Message belongsTo User or so), you can access it simply with:

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

If they're not associated, you can import any other model at any time using:

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

Upvotes: 12

Related Questions