Hii there,
Today I found a bug in my website caused by filtering some results.
this is my code:
public function __construct() {
self::$_projectsTable = TableRegistry::get('Projects');
self::$_projectsReleasesTable = TableRegistry::get('ProjectsReleases');
self::$_projectsDescriptionsTable = TableRegistry::get('ProjectsDescriptions');
}
public static function findProjects(bool $withRelease = true) {
$projects = self::$_projectsTable
->find();
if($withRelease) {
$projects = $projects
->select(['Projects.id'])
->select(self::$_projectsTable)
->matching('ProjectsReleases', function ($q) {
$conditions = [['published' => 1]];
if(Permission::check('view_privatereleases', 1)) {
$conditions[] = ['published' => 0];
}
if(Permission::check('view_prereleases', 1)) {
$conditions[] = ['published' => -1];
}
return $q
->where(['OR' => $conditions]);
})
->distinct(['Projects.id']);
}
$projects = $projects->contain([
'ProjectsDescriptions'
]);
return $projects;
}
So what happens is that when I call findProjects
with the $withRelease
being true
, it will return only the projects with releases visible by the user (based on permissions).
This part works absolutely fine but when I try to contain the ProjectsDescriptions
, it goes wrong since it just completely ignores that statement.
When I set the $withRelease
to false
, it will also show projects without a release but it does load the ProjectsDescriptions
just fine.
So my question is: why does this happen and how can I fix it?