Using same field in Model.beforeFind, bugs other where's clausules
tiagoa opened this issue · 3 comments
In a model table, I configure Model.beforeSave for attributing the logged user in the new registry. And Model.beforeFind to retrieve just the registers that this user saved.
But if I have one where's clausule in a controller or other beforeFind, it causes an error "Error: SQLSTATE[HY093]: Invalid parameter number".
class ArticlesTable extends Table{
public function initialize(array $config){
parent::initialize($config);
$this->table('articles');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Muffin/Footprint.Footprint', [
'events' => [
'Model.beforeSave' => [
'user_id' => 'always',
],
'Model.beforeFind' => [
'user_id',
],
],
'propertiesMap' => [
'user_id' => '_footprint.user_id',
],
]);
...
And then in a Controller:
$articles = $this->Articles->find('all', [
'fields' => ['id', 'title', 'slug']
])
->where(['user_id IN' => array_keys($users)], [], true);
Return:
object(Cake\ORM\Query) {
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT Articles.id AS "Articles__id", Articles.ano AS "Articles__ano", Articles.user_id AS "Articles__user_id" FROM articles Articles WHERE user_id in (:c0)',
'params' => [
':c0' => [
'value' => (int) 54769,
'type' => 'biginteger',
'placeholder' => ':c0'
],
':c1' => [
'value' => (int) 54769,
'type' => 'biginteger',
'placeholder' => ':c1'
]
],
...
@tiagoa Did you make any progress on this? I'm having the same issues.
@justinatack No. I picked up the code of Ceeram/Blame and make an custom behavior.
@tiagoa the sql query has a single param: WHERE user_id in (:c0)', whille two params are sent over, are you sure that you have pasted exactly your Controller code?
Don't you have a space after "IN" ?
WRONG
->where(['user_id IN ' => array_keys($users)], [], true);
GOOD
->where(['user_id IN' => array_keys($users)], [], true);
If you have a space, i think that explains it.