canimbenim
canimbenim

Reputation: 649

CakePHP how to get value from another table

I have a problem that I have 3 tables: Users, Images (user_id), Comments (user_id, image_id),

Now from Image view I want to "translate" user_id from table Comment. How to do it? I was trying to create helper but the view helper does not see the $this->tableName variable or I had an error there. I know that I have access to Comment table in the Image view ($image['Comment']) so I have the user_id which I want to convert to the username but how to do it?

Thanks,

Upvotes: 0

Views: 1618

Answers (3)

Tim Joyce
Tim Joyce

Reputation: 4517

You should have access to the User and Comment table. I prefer pr($images); might be easier to read but, user's data should be in there.

foreach ($images as $image){
    pr($image['User']['fname']);
}

Upvotes: 0

Gevious
Gevious

Reputation: 3252

Have a look at the containable behaviour. http://book.cakephp.org/view/1323/Containable

When you search for comments do it something like this

$this->Comment->find('all',array('contain'=>'User'));

That will return your comments along with the user details that you need. Pass this information into your view, rather than trying to retrieve it in your view.

Upvotes: 0

Dry
Dry

Reputation: 116

I'm not sure about this but you may find that altering model recursive will make this available. Debug the $image array in your view to make sure:

<?php debug($image) ?>

Upvotes: 0

Related Questions