I have one controller for invoices and another for clients.
I have made some "relationships" in the models of these controllers as below:
Client.php
<?php
class Client extends AppModel {
public $hasMany = array(
'Invoice' => array(
'className' => 'Invoice',
'foreignKey' => 'client_id'
)
);
}
?>
Invoice.php
<?php
class Invoice extends AppModel {
public $belongsTo = array(
'Client' => array(
'className' => 'Client',
'foreignKey' => 'client_id'
)
);
}
?>
So I thought that making a drop down with each client would be easy. Although, I can't figure out how to do this.
First off this is what I want as a result:
<select>
<option value="1">Client Name</option>
</select>
Where value is equal to id in client table and Client Name is equal to name in client table.
I tried placing the following into the invoices controleler:
$client = $this->Client->find('all');
and then the following into my view:
echo $this->Form->select('client_name',client, array(
'class' => 'form-control',
'name' => 'client_id'
));
But I received the following error:
Call to a member function find() on a non-object
Directly on page load. What do I need to do to make this work?
$this->Invoice->Client->find('all');. You probably also want to make use of the find type, list, book.cakephp.org/2.0/en/models/… which will return results formatted in a way that you can use to create your select options easily.$client = $this->Invoice->Client->find('list');in my controller and then:echo $this->Form->select('client_name', $client, array((shortened of course) in my view.