views:

34

answers:

1

Hi there,

Currently i'm having a problem. I want to access the data available in the 4th table of my DB.

Click here to see the db image

I have the tables in this way: Categories --> Categories_Companies --> Companies --> Affiliates

Like it shows in the image i'm on the categories and in the Categories view (views/categories/view.ctp) i want to show the fields title and url from the affiliates table.

There is another way of doing that without using the this->query?

Regards

+1  A: 

You access a table through its model. The Category model is automatically included in the CategoriesController by naming convention. You can include other models by using $uses.

var $uses = array('Category', 'Affiliate');

function view() {
    $this->Category->find(…);
    $this->Affiliate->find(…);
}

Or, if your models are linked through associations, you can access them through an association:

$this->Category->Company->Affiliate->find(…);

Both examples are equivalent, the first is just more convenient.

deceze
+1. I would prefer the second one.
bancer
@bancer So would I, but it depends on how far away the association is and how often I'm using it. I wouldn't like to have `$this->Model1->Model2->Model3->Model4->find()` in my code too often... :)
deceze