Note that there are some explanatory texts on larger screens.

plurals
  1. POChange Client id to Client Name with CakePHP
    primarykey
    data
    text
    <p>Okay, so I have a form in CakePHP that submits to a database. In this form it submits a field called <code>client_id</code> and it is stored in the database as such as well. </p> <p>Then I have a view to allow me to view all of the invoices that have ever been created. To view the client responsible for the invoice, I can currently only see the id entered in the form by placing: <code>&lt;?php echo $invoice['Invoice']['client_id']; ?&gt;</code> in the view. </p> <p>The invoices go to one database called: invoices</p> <p><strong>The clients name is not stored in the invoices table, just the id</strong></p> <p>The clients information is stored in one database called: clients </p> <p>I want to be able to actually display out the clients real name in the invoices view rather than the client id. </p> <p>I tried adding the following query to my index controller, But i'm not sure what to do after this or if this is even right. </p> <p><code>$this-&gt;set('clients', $this-&gt;Invoice-&gt;query("SELECT * FROM clients WHERE id='89545'"));</code></p> <p>In order to keep this question short in the first place, Please request specific code by commenting. i.e. Controller code, view code, etc... Thank you in advance.</p> <p><strong>Additional Thoughts</strong> </p> <p>If I wasn't using CakePHP I could use something like the following, So I guess I just don't know how to put this into cakephp "language".</p> <pre><code>&lt;?php query($con, "SELECT name FROM clients WHERE id='$client_id'"); echo $row['name']; ?&gt; </code></pre> <p>roughly!</p> <p><strong>Update</strong></p> <p>Here are my models </p> <p>First one being the <code>Client.php</code></p> <pre><code>&lt;?php class Client extends AppModel { public $hasMany = array( 'Invoice' =&gt; array( 'className' =&gt; 'Invoice', 'foreignKey' =&gt; 'client_id' ) ); } ?&gt; </code></pre> <p>Second one being the <code>Invoice.php</code></p> <pre><code>&lt;?php class Invoice extends AppModel { public $belongsTo = array( 'Client' =&gt; array( 'className' =&gt; 'Client', 'foreignKey' =&gt; 'client_id' ) ); } ?&gt; </code></pre> <p>And finally, to get my invoices from the database, I am using the following inside: <code>InvoicesController.php</code></p> <pre><code>public function index() { $this-&gt;set('invoices', $this-&gt;Invoice-&gt;find('all')); } </code></pre>
    singulars
    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. 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