Member Avatar for ping0ping

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.

Recommended Answers

All 3 Replies

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.

Member Avatar for ping0ping

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.

Member Avatar for ping0ping

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 ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.