Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using same field in Model.beforeFind, bugs other where's clausules #39

Closed
tiagoa opened this issue Dec 5, 2016 · 3 comments
Closed

Comments

@tiagoa
Copy link

tiagoa commented Dec 5, 2016

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 tiagoa closed this as completed Dec 5, 2016
@tiagoa tiagoa reopened this Dec 5, 2016
@justinatack
Copy link
Contributor

justinatack commented Jan 10, 2017

@tiagoa Did you make any progress on this? I'm having the same issues.

@tiagoa
Copy link
Author

tiagoa commented Jan 10, 2017

@justinatack No. I picked up the code of Ceeram/Blame and make an custom behavior.

@juniorionut
Copy link

juniorionut commented Apr 21, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants