Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's considered poor form to use <code>var $uses</code> in CakePHP 1.3+. It's been replaced by App::import (<em>see below</em>)</p> <p>Given you have a Users controller with User model, and a Comments controller with Comment model. The accepted patterns are:</p> <p><strong>Using Associations</strong></p> <p><em>This is your best bet if the models can be logically associated.</em></p> <pre><code>// models/user.php Class User extends AppModel { public $hasMany = array('Comment'); } // controllers/users_controller.php Class UsersController extends AppController { function allComments() { $this-&gt;User-&gt;Comment-&gt;find('all'); // You can use this across multiple models (e.g. $this-&gt;User-&gt;Comment-&gt;NestedComment-&gt;find('all'); } } </code></pre> <p><strong>Instantiating the model object</strong></p> <p><em>This will load your model file, add the instance to CakePHP's object map, and returns the instance.</em></p> <pre><code>// models/user.php Class User extends AppModel {} // models/comment.php Class Comment extends AppModel {} // controllers/users_controller.php Class UsersController extends AppController { function allComments() { $Comments =&amp; ClassRegistry::init('Comment'); $Comments-&gt;find('all'); } } </code></pre> <p><strong>Using $this->loadModel</strong></p> <p><em>Internally this uses ClassRegistry::init, then also adds the model as a property of the controller.</em></p> <pre><code>// models/user.php Class User extends AppModel {} // models/comment.php Class Comment extends AppModel {} // controllers/users_controller.php Class UsersController extends AppController { function allComments() { $this-&gt;loadModel('Comment'); $this-&gt;Comment-&gt;find('all'); // using load model allows you to access it via $this-&gt;Model } } </code></pre> <p><strong>App::import</strong></p> <p><em>This is really just the CakePHP way of requiring a file. You'll still need to instantiate the object.</em></p> <pre><code>// models/user.php Class User extends AppModel {} // models/comment.php Class Comment extends AppModel {} // controllers/users_controller.php App::import('Model', 'Comment'); Class UsersController extends AppController { function allComments() { $Comment = new Comment(); $Comment-&gt;find('all'); } } </code></pre> <p>I hope this helps.</p> <p><em>Edit: If you want to use the model object globally within the controller, you can use any of these patterns I specified within your <code>beforeFilter()</code>.</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload