Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question boils down to "How can I retrieve the Manager with id=1 and find his Employees.</p> <p>In CakePHP you would issue the following find query to retrieve the desired manager entry:</p> <pre><code>$manager = $this-&gt;Manager-&gt;findById(1); // or $manager = $this-&gt;Manager-&gt;find('first', array( 'conditions' =&gt; array( 'Manager.id' =&gt; 1 ) ); </code></pre> <p>The above find calls fetch the manager with id=1 from the database and because you set up the relationship Manager hasMany Employee, the result will also contain all employees for the manager ordered by Employee.created DESC.</p> <p>The resultset would look something like this:</p> <pre><code>Array ( [Manager] =&gt; Array ( [id] =&gt; 1 [field1] =&gt; value1 ) [Employee] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 2 [manager_id] =&gt; 1 [name] =&gt; Bar ) [1] =&gt; Array ( [id] =&gt; 1 [manager_id] =&gt; 1 [name] =&gt; Foo ) ) ) </code></pre> <p>As you can see, CakePHP uses an array format for returned results and not an object as in Ruby on Rails. Therefore you have to access your related data within the resulting array.</p> <p>Another important difference between CakePHP and Ruby on Rails(RoR) is, that in CakePHP the queries to the db are executed the moment you call them, whereas in RoR they are lazily executed the moment you try to access the results.</p> <p>To complement your RoR example for accessing employees here is the CakePHP version:</p> <pre><code>$employee_count = count($manager['Employee']); foreach ($manager['Employee'] as $e) { echo $e['name']; } </code></pre> <p>I hope this clears up some confusion.</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. 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