简体   繁体   中英

Strange behaviour with Doctrine_Query in Doctrine 1.2

I have a query that has multiple orWhere statements. The PHP code looks like this:

$firstDay = $params['datetimeForMonth']->format('Y-m-d');
$lastDay = $params['datetimeForMonth']->format('Y-m-t 23:59:59');
$whereStatement = "(s.level = ?
    AND s.idForLevel IN (?) 
    AND s.startDatetime BETWEEN '$firstDay' AND '$lastDay')";
$q = Doctrine_Query::create()
    ->from('Mmb_Model_Schedule s')
    ->where($whereStatement, ['prefecture', $this->id])
    ->orWhere($whereStatement, ['district', $districtIds])
    ->orWhere($whereStatement, ['municipality', $municipalityIds])
    ->orWhere($whereStatement, ['workplace',    $workplaceIds])
    ->orWhere($whereStatement, ['user', $userIds]);

The variables $districtIds, $municipalityIds, $workplaceIds, and $userIds are all defined ahead of time as comma separated values.

This query works just fine with every level except for the district ids. Any record with the level "district" will not be returned in the query.

What's really strange is that, if I don't use bound parameters but rather insert the following code, everything works fine:

->orWhere("s.level = 'district' AND s.idForLevel IN ($districtIds) AND s.startDatetime BETWEEN '$firstDay' AND '$lastDay'")

What's even more infuriating is that even this query done all by itself as a single where statement (excluding all the others) also gives me nothing if I use bound parameters.

And as if that weren't enough, I even tried updating the table changing 'district' to 'prefDistrict', just in case the word 'district' was causing the problems. Nothing changed.

Despite the fact that I'm inserting the EXACT SAME data as bound parameters as when entering them directly, the behaviour changes dramatically. What on Earth is going on here?

I want to say that there must be something in the values of the $districtIds var that is tripping up the query builder in Doctrine. Can you post a sample of what those values are?

Also, have you looked at the generated SQL that doctrine runs (either tailing an SQL log, or hooking into the preExecute event)? I'd be curious to see the output of something like that.

(this should probably be a comment, but my reputation isn't high enough for that ;) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM