M
M
Mark2021-02-23 15:16:52
Yii
Mark, 2021-02-23 15:16:52

How to separate where() and andWhere() condition in ActiveRecord Yii2?

Task : It is necessary to get the number of records for the current day that have a specific link or email.

Problem : ActiveRecord combines where and andWhere() into one condition. Output: "Get all posts that are made today and have a $link OR posts with $email" instead of "Get all posts for today that have a $link or $email".

In AR I have the following structure:

Order::find()->where('date > UNIX_TIMESTAMP(CURDATE())')
  ->andWhere(['link' => $link])
  ->orWhere(['email' => $email])
  ->count();


The output is the following SQL query:
SELECT COUNT(*) FROM `order` WHERE ((date > UNIX_TIMESTAMP(CURDATE())) AND (`link`='somelink')) OR (`email`='[email protected]')'


Question : How can I force ActiveRecord to separate the where and andWhere conditions?
inb4 : I understand that it is possible to solve the problem using findBySql(), I want to clarify whether it is possible to solve it with ActiveQuery

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2021-02-23
@MarkLb

Order::find()->where('date > UNIX_TIMESTAMP(CURDATE())')
  ->andWhere(['OR',
              ['link' => $link],
              ['email' => $email]
  ])
  ->count();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question