Edit:
Seems to me, that I can access the id of the current user in a table method with
Router::getRequest()->getAttribute('identity')->id
So I ended up with this:
$this->addBehavior('CounterCache', [
'Users' => [
'totalfollower' => [
'finder' => 'approvedFollower',
],
'totalfollowed' => [
'finder' => 'approvedFollowed',
],
],
]);
public function findApprovedFollower () {
$query = $this->find();
$query->where([
'Userrelationships.userrelationshipstate_id'=>2,
'Userrelationships.user_id'=>Router::getRequest()->getAttribute('identity')->id
]);
return $query;
}
public function findApprovedFollowed () {
$query = $this->find();
$query->where([
'Userrelationships.userrelationshipstate_id'=>2,
'Userrelationships.follower'=>Router::getRequest()->getAttribute('identity')->id
]);
return $query;
}
I tried to solve it by associating the follower data with the users, but got not the result I wanted.
Maybe I will do some more research, but at the moment, this seems to work.
Original Post:
I have two tables
tbl Users
-id
-name
-totalfollower
-totalfollowed
…
tbl Userrelationships
-id
-user_id
-follower
-userrelationshipstate_id
In UserrelationshipsTable I show the relationships between the Users.
A user can be the follower of another user or can be followed by other users.
In the UserTable I want to store the total number of followers and followed.
Therefore I added in UserrlationshipsTable the CounterCache Behavior:
$this->addBehavior('CounterCache', [
'Users' => [
'totalfollower' => [
'finder' => 'approvedFollower',
],
'totalfollowed' => [
'finder' => 'approvedFollowed',
],
],
]);
and added in UserrelationshipsTable the finders:
public function findApprovedFollower (SelectQuery $query) {
$query->where([
'Userrelationships.userrelationshipstate_id'=>2,
//'Userrelationships.user_id'=>:user_id
]);
return $query;
}
public function findApprovedFollowed (SelectQuery $query) {
$query->where([
'Userrelationships.userrelationshipstate_id'=>2,
//'Userrelationships.follower'=>:user_id
]);
return $query;
}
I would like to expand the “where” condition to select only the rows where either “user_id” or “follower” is equal to the id of the current user, but have no idea where I can get the value from.
Can anyone give me advice how to do this?