0

Hi, this is my first post. I'm new here and also just start playing with CakePHP. And sorry for my English since its not my native language.

I had problem to call data from two tables since I dont know how to do it in CakePHP.

So basically I doing a simple dictionary that had two tables named users (User) and words (Word).

User

id
username
password
role
created
modified
nickname

Word

id
word
description
created
modified
user_id

What I want to do now is to display all the contents in Word table. But instead of display user_id I want to display nickname column which is in User table. So now I used find('all') to get all the data in Word table. But I dont know how to used user_id which referring to id in User table to display nickname. That's all.

Hope I explain clear enough. Hope got someone can help me with this. Thank you.

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by ping0ping
0

Hi,

Please read this one Here. You need table join. In your case, code below

  'conditions' => array(
 'Channel.id = Item.channel_id',
 )

can be modified to

 'conditions' => array(
 'word.user_id = user.id',
 )

I can't write any sample for this question, because the response is pretty lengthy... please update us with your progress.

0

Thank you for your replied. I write the code as below

$options['joins'] = array(
            array('table' => 'users',
            'alias' => 'User',
            'type' => 'LEFT',
            'conditions' => array(
                'User.id = Word.user_id'))
        );

And to display on my index page I used variable 'words' to store the result

$this->set('words', $this->Word->find('all', $options));

And on my index page I used foreach loop to display the result.

<?php foreach ($words as $word): ?>
        <?php $count++; ?>

        <tr>
            <td><?php echo $count; ?></td>
            <td><?php echo $word['Word']['word']; ?></td>
            <td><?php echo $word['Word']['description']; ?></td>
            <td><?php echo $word['Word']['user_id']; ?></td>
            <td><?php echo $word['Word']['created']; ?></td>
        </tr>
<?php endforeach ?>

When I do

print_r($word)

to display the result of the array its only display the data from table Word excluding table User. I may miss something here so hopefully you can guide me. Thank you again.

Edited by ping0ping

0

I think I had this solved after reading this link

For Word.php model I add

class Word extends AppModel {
    public $name = 'Word';
    public $belongsTo = 'User';
}

which define the relationship between the two models I think. Then in index page I now can used User model on line 7.

<?php foreach ($words as $word): ?>
        <?php $count++; ?>
        <tr>
            <td><?php echo $count; ?></td>
            <td><?php echo $word['Word']['word']; ?></td>
            <td><?php echo $word['Word']['description']; ?></td>
            <td><?php echo $word['User']['nickname']; ?></td>
            <td><?php echo $word['Word']['created']; ?></td>
        </tr>
<?php endforeach ?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.