I need to be able to use an ActiveRecord Query class to refind my search to “is the day of week X AND other conditions.” I am stumped and having issues figuring this out, esp since I can do it easily in mysqlc but totally fail when doing the same where clause in ActiveRecord.
I have an ActiveRecord that is defined like this:
type = enum('recurring','once')
recurring = enum('every day', 'every week day')
dow=integer()
...
If type=‘recurring’, then I want to know if it is ‘every day’ or ‘every week day’. For ‘every day’, that’s easy because I don’t have to compare the current day to anything:
[ 'and',
[ 'rtm_schedule_type.name' => ScheduleType::SCHEDULE_TYPE_RECURRING],
[ 'rtm_schedule_option.recurring' => ScheduleOption::SCHEDULE_OPTION_RECURRING_EVERY_DAY],
],
Where I’m struggling is with ‘every week day’. In mysqlc, I can do this:
mysql> select count(id) as cntid from rtm_category where 3 < 5;
+-------+
| cntid |
+-------+
| 4 |
+-------+
1 row in set (0.00 sec)
Put another way, I can say the following to determine if I am within ‘every week day’:
select * from table where `dow +%w` >= 1 and `dow +%w` <= 5
However, I can’t make this work with ActiveRecord. I’ve tried everything, including variations on the format below, parameters, passing a single string with SQL, etc:
[ 'and',
[ 'rtm_schedule_type.name' => ScheduleType::SCHEDULE_TYPE_RECURRING],
[ 'rtm_schedule_option.recurring' => ScheduleOption::SCHEDULE_OPTION_RECURRING_EVERY_WEEK_DAY],
[ '>=', $dateToUse->format('w'), '1' ],
],
Generally I get an error like this:
Caused by: Exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column '4' in 'where clause''
And the error is here:
((`rtm_schedule_type`.`name`='Recurring') AND (`rtm_schedule_option`.`recurring`='Every Week Day') AND (`4` >= '1'))
Specifically, here: (4
>= ‘1’)
I am trying to extend my Query class and am really stumped on this one. It’s probably obvious, but I can’t figure it out after reading the docs a few times: Working with Databases: Active Record | The Definitive Guide to Yii 2.0 | Yii PHP Framework
Maybe I’m approaching this in the wrong way? Or, I just don’t know the syntax that AR wants for this…