Answer the question
In order to leave comments, you need to log in
Where relation?
I have a model like this:
/**
* @property int $id
* @property int $article_id
* @property int $amount
* @property int $created_at
* @property int $updated_at
*
* @property-read Article $article
* @property-read PaymentFile[] $files
* @property-read bool $isCompleted
*/
class ArticlePayment extends ActiveRecord {
public static function tableName() {
return '{{%article_payment}}';
}
public function getArticle() {
return $this->hasOne(Article::class, ['id' => 'article_id']);
}
public function getFiles() {
return $this->hasMany(PaymentFile::class, ['payment_id' => 'id'])->where(['is_deleted' => false]);
}
public function getIsCompleted() {
return $this->getFiles()->select('count(id) > 0')->groupBy('id')->having('count(id) > 0')->asArray();
}
}
$article->getPayments()->where(['isCompleted' => true]);
Answer the question
In order to leave comments, you need to log in
Found a solution
public function getFilesAggregation() {
return $this->getFiles()
->alias('f')
->select(['isCompleted' => 'count(f.id) > 0'])
->groupBy('f.id')
->having('count(f.id) > 0')
->asArray();
}
public function getIsCompleted() {
return $this->isNewRecord ? false : (bool)$this->filesAggregation[0]['isCompleted'];
}
$article->getPayments()->joinWith('filesAggregation');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question