Take the 2-minute tour ×

I apologize for the confusing title, I was a little stumped as to how to word my question.

I am new to CakePHP, but am following along through the cookbook/tutorials nicely, however I have come up against something which I cannot find an answer to.

My structure is as follows:

'Invoices' hasMany 'InvoiceHistory'
'InvoiceHistory' belongsTo 'InvoiceHistoryDeliveryStatus'

Whereby, an invoice can have multiple invoice histories, and each history contains a delivery status id, which links to a name.

On the Invoice view (index.ctp) I am displaying a list of all invoices but wish to display the Most Recent Delivery Status Name (InvoiceHistory contains a date field so it can be sorted) - thereby displaying the 'current Delivery Status'. When I do:

$this->set('invoices', $this->Invoice->find('all'));

It does not go deep enough in what it returns to provide me with Delivery Status Names, nor have I deduced a way of only returning the most recent Invoice History within my result. I know how to do this manually with a MYSQL query but I figured that is probably just plain wrong.

What is the correct way of going about this while following CakePHP conventions?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Use Containable


$this->set('invoices', $this->Invoice->find('all', array(
    'contain' => array(
        'InvoiceHistory' => array(
share|improve this answer
That's a very neat function!! Thanks for your help - works like a charm. –  user984976 Jun 29 '12 at 22:26
add comment

From what I can tell, I think you should check out the Containable behavior.

share|improve this answer
Thanks for your answer Poyan - the containable behavior is exactly what I was looking for. –  user984976 Jun 29 '12 at 22:27
add comment

Your Answer


