PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 1, 2022

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

 November 01, 2022     cakedc, cakephp, cakephp-2.0, php, relational-database     No comments   

Issue

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.



Answered By - Kishor Kundan
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing