CakePHP 3 - download messages from users that the user is executing

Trying to download all messages from users that the user is executing in Cakey mode.

Three tables:

users (id, username, password)

messages (id, text, user_id, created)

follows (id, user_id, follow_id)

I think my relationship is set up correctly, so I can download followers of users and people following them:

User table

$this->belongsToMany('Following', [ 'className' => 'Users', 'foreignKey' => 'user_id', 'targetForeignKey' => 'following_id', 'joinTable' => 'follows' ]); $this->belongsToMany('Followers', [ 'className' => 'Users', 'foreignKey' => 'following_id', 'targetForeignKey' => 'user_id', 'joinTable' => 'follows' ]); 

According to table

  $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER' ]); 

Message table

  $this->belongsTo('Users', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER' ]); 

Now, trying to select all messages from users that follow one user. I think I need to use the matching() method described here: Filtering by related data through matching and joining , but it doesn't seem to be right,

Try something in this direction:

 $user_id = $this->Auth->user('id'); $posts = $this->Posts->find()->matching('Users.Following', function ($q) use ($user_id) { return $q->where(['Users.id' => $user_id]); })->all(); 

UPDATE:

I think my relationship is set correctly, this search will return the user along with all the users that they follow:

  $users = $this->Posts->Users->get(1, [ 'contain' => ['Following'] ]); 
+5
source share
2 answers

You skipped $user_id for inheritance in the matching anonymous function .

 $user_id = $this->Auth->user('id'); $posts = $this->Posts->find() ->matching('Users.Following', function ($q) use ($user_id) { return $q->where(['Users.id' => $user_id]); }) ->all(); 

Closing can also inherit variables from the parent scope. Any such variables must be passed into the construction of the use language. From PHP 7.1, these variables should not include superglobal , $this or variables with the same name as the parameter.

See also Anonymous Functions.

UPDATE:

Add to user table

 $this->hasMany('Posts', [ 'className' => 'Posts', 'foreignKey' => 'user_id' ]); 

controller

 $users = $this->Posts->Users->get(1, [ 'contain' => ['Following.Posts'] ]); 
0
source

As you mentioned in the comment "Ideally, I only need an array of mail objects consisting of messages from users that the user is following." A simple search query with proper join conditions should do the job:

  $user_id = $this->Auth->user('id'); $this->loadModel("Posts"); $posts = $this->Posts->find('all') ->select() ->where(["users.id" => $user_id]) ->join([ 'follows' => [ 'table' => 'Follows', 'type' => 'LEFT', 'conditions' => 'follows.following_id = Posts.user_id' ], 'users' => [ 'table' => 'Users', 'type' => 'LEFT', 'conditions' => 'users.id = follows.user_id' ] ]); $posts->toArray(); 

Note: I did not use any belongsToMany , belongsTo relationships that you defined in the models

I hope this solves your problem.

0
source

Source: https://habr.com/ru/post/1268189/


All Articles