Hello, for several days I have encountered great difficulty in correctly sorting the recordings on CakePHP 4.
I want to view the last post a user posted, which is very simple, but it doesn’t work:
$s = $this->Articles->find()
->select('Articles.id')
->distinct('Articles.user_id');
$articles = $this->Articles
->find()
->where(['Articles.id IN' => $s])
->order(['Articles.created' => 'DESC'])
->contain(['Users']);
All articles are displayed without taking into account the “where” conditions.
Same with :
$s = $this->Users->find()
->select('id')
->distinct('country_id');
$users = $this->Users
->find()
->where(['Users.id IN' => $s])
->order(['Users.country_id' => 'DESC'])
->contain(['Countries']);
SQL:
SELECT
Users.id AS Users__id,
Users.alias AS Users__alias,
Users.username AS Users__username,
Users.email AS Users__email,
Users.password AS Users__password,
Users.country_id AS Users__country_id,
Users.presentation AS Users__presentation,
Users.secret_key AS Users__secret_key,
Users.account_status AS Users__account_status, ,
Users.role AS Users__role,
Users.created AS Users__created,
Users.modified AS Users__modified,
Countries.id AS Countries__id,
Countries.name AS Countries__name,
Countries.slug AS Countries__slug,
Countries.user_count AS Countries__user_count
FROM
users Users
LEFT JOIN countries Countries ON Countries.id = (Users.country_id)
WHERE
Users.id in (
SELECT
Users.id AS Users__id
FROM
users Users
GROUP BY
country_id
)
ORDER BY
Users.country_id DESC
Why is it not working in CakePHP?
Thanks a lot for your help