Search code examples
phpcakephprelational-databasecakephp-2.0cakedc

How can i set association between comment model and Users.User model?


It's CakePHP-2.0 and CakeDC-Users plugin

mysql> describe comments;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| post_id  | int(11)      | NO   | MUL | NULL    |                |
| user_id  | int(11)      | NO   |     | NULL    |                |
| body     | varchar(500) | NO   |     | NULL    |                |
| mark     | tinyint(4)   | NO   |     | 1       |                |
| created  | datetime     | YES  |     | NULL    |                |
| modified | datetime     | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

I need the username not the user_id. How can i set the association between Users.User model and Comment model and with Post model.

So far i have this in Comment model public $belongsTo = array('Post','Users.User');

Can anyone please explain with example about this association?


Solution

  • the field user_id is a foreign key in that table.

    Cake will automatically fetch records related to that id, including username and other fields depending on your recursive level.