Relation context:
class ForumCategoriesTable extends Table {
public function beforeSave(Event $event, Entity $entity){
}
public function initialize(array $config)
{
$this->addBehavior('Timestamp'); //gestion des colonnes created et modified
$this->hasMany('ForumPosts');
$this->hasOne('LastForumPost', [
'className' => 'ForumPosts',
'foreignKey' => false,
'conditions' => function ($exp, $query) {
$subquery = $query
->getConnection()
->newQuery()
->select(['SubLastForumPost.id'])
->from(['SubLastForumPost' => 'forum_posts'])
->where([
'SubLastForumPost.draft = false', //to remove for admin
'SubLastForumPost.created IS NOT NULL',
'ForumCategories.id = SubLastForumPost.forum_category_id',
'SubLastForumPost.parent_id IS NULL'])
->order(['SubLastForumPost.created' => 'DESC'])
->limit(1);
return $exp->add(['LastForumPost.id' => $subquery]);
}
]);
}
This gives the LastForumPost for each ForumCategory.
In my ForumCategoriesController , method index is:
public function index($id = null) {
//filtre sur les brouillons
if($this->Auth->user()['role'] == 'admin') {
//todo: find a way for the admin to change the subquery and remove
// the 'SubLastForumPost.draft = false', condition
} else {
}
$query = $this->ForumCategories->find()
->contain([
'LastForumPost',
'LastForumPost.Authors'
])
->order([
'ForumCategories.part' => 'ASC',
'ForumCategories.position' => 'ASC',
]);
$forumCategories = $query->toArray();
What I am looking to do: remove the ‘SubLastForumPost.draft = false’, condition if the user is admin so he can see the ForumPosts with a draft status.
How can I achieve that?
Thanx for reading and for your help