Answer the question
In order to leave comments, you need to log in
How to make hasMany with condition in variable?
Good afternoon.
There are comments on the articles, I need to get the comments for the last 7, 28 days.
now I have done this:
Article model:
public function getComments7(){
return $this->hasMany(Comments::className(), ['page_id' => 'id'])->andWhere('date > DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)');
}
public function getComments28(){
return $this->hasMany(Comments::className(), ['page_id' => 'id'])->andWhere('date > DATE_SUB(CURRENT_DATE, INTERVAL 28 DAY)');
}
Answer the question
In order to leave comments, you need to log in
2 connections are not needed, one is enough
public function getComments($period=false){
$query = $this->hasMany(Comments::className(), ['page_id' => 'id']);
if($period){
$query->andWhere('date > DATE_SUB(CURRENT_DATE, INTERVAL '.$period.' DAY)');
}
return $query;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question