Answer the question
In order to leave comments, you need to log in
Yii2 - how to make a selection of records?
There is a method that should count the number of comments:
public function getCountComments() {
$condition = [];
if($this->post_id) {
$condition['post_id'] = $this->post_id;
}
if($this->type) {
$condition['type'] = $this->type;
}
return self::find()
->where($condition)
->count();
}
public function getCountComments($period = false) {
$condition = [];
if($this->post_id) {
$condition['post_id'] = $this->post_id;
}
if($this->type) {
$condition['type'] = $this->type;
}
// Выборка за определенный период
if($period == 'day') {
return self::find()->where('date_create>=CURDATE()')->count();
}
return self::find()
->where($condition)
->count();
}
Answer the question
In order to leave comments, you need to log in
public function getCountComments($period = false) {
$query = self::find();
if($this->post_id) {
$query->where(['post_id' => $this->post_id]);
}
if($this->type) {
$query->andWhere(['type' => $this->type]);
}
if($period == 'day') {
$query->andWhere('date_create>=CURDATE()');
}
return $query->count();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question