How can I find and show Name of first element in a table?
I'm guessing that I have to use something like
$this->data['lastInvioce'] = $this->Invoice->find('all', array('limit' => 1));
... in controller to find it...
Tnx in adv!!!
How can I find and show Name of first element in a table?
I'm guessing that I have to use something like
$this->data['lastInvioce'] = $this->Invoice->find('all', array('limit' => 1));
... in controller to find it...
Tnx in adv!!!
Try $this->Invoice->find( 'first', ... )
. You can use other conditions and/or sorting to manipulate which record is the first, of course.
Your code is almost correct. The first parameter of Model::find()
indicates how many records to retrieve. Model::find('all')
retrieves all matching records. What you want is Model::find('first')
:
// Retrieve most recent Invoice record
$lastInvoice = $this->Invoice->find('first', array(
'fields' => array( 'Invoice.name' ),
'order' => array( 'Invoice.created' => 'desc' )
);
// Make this Invoice record available in the view
$this->set( compact('lastInvoice'));
This will retrieve the most recently created Invoice
record, and only the name
field. The $this->set()
call makes the data in $lastInvoice
available to your view. So:
echo $lastInvoice['Invoice']['name'];
Or alternatively:
extract($lastInvoice);
echo $Invoice['name'];