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

Thursday, December 30, 2021

[FIXED] Cakephp - How to get data from other model using associated model?

 December 30, 2021     cakephp, php     No comments   

Issue

How to Join two model table to get user name base on user_id?

likesTable.php - id | post_id | user_id

              1      5     23

postTable.php - id | like_id | user_id | content

               5     1    23    Hello
               

userTable.php - id | user_name |

               23  sample_name

PostController

   $this->paginate = [
        'contain' => ['Users', 'Likes'],
    ];
    $posts = $this->paginate($this->posts);

Cake PHP variables in view

 'likes' => object(App\Model\Entity\likes) id:4 {
 'id' => (int) 1
 'post_id' => (int) 5
 'user_id' => (int) 23   ***I want to get user name from this ID***

Solution

Add users with likes in container (Likes.Users)

$this->paginate = [
        'contain' => ['Users', 'Likes.Users'],
];


Answered By - Alimon Karim
  • 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