CakePHP/2.x - Bind parameters in `fields`


#1

In CakePHP/2.x, the prepared statements implementation allows to bind parameters in conditions:

'conditions' => array(
    'dob < ? + INTERVAL 1 DAY' => $dob,
);

Is there a syntax to do the same in fields?

I’ve tried several ideas but none worked. For instance, this:

'fields' => array(
    '? + INTERVAL 1 DAY AS next_day' => $dob,
);

… just generates expressions like

`MyModel`.`2012-05-15`

… because it ignores the key and it assumes the value is a column name.

My current workaround is escaping with DboSource::value() but it’s of course not as nice as prepared statements:

// Inside model:
'fields' => array(
    $this->getDataSource()->value($dob) . ' + INTERVAL 1 DAY AS next_day',
);

#2

Hi,
Try to use “virtual fields”


#3

How can I do that? As far as I can tell, the syntax for $this->Model->virtualFields is exactly the same than the syntax for 'fields'. Hard-coded values work:

$this->virtualFields['next_day'] = 'dob + INTERVAL 1 DAY';

But… What’d be the syntax to bind values?

$this->virtualFields['next_day'] = array(
    '? + INTERVAL 1 DAY' => $dob,
);

Notice: Array to string conversion

$this->virtualFields['next_day'] = array(
    '? + INTERVAL 1 DAY',
    $dob,
);

Notice: Array to string conversion