Sunday, September 6, 2015

Re: baking a model of users

I see what happend but i just don't get why. There is a belongs to made for the same table. When is remove that everything works fine. Anybody knows why?

Op zondag 6 september 2015 15:55:33 UTC+2 schreef Abdelmajid el Ibrahimi:
Hello,

I have made a table named users with a id and a username and a password
The problem is when i bake the model i get the next model:

  $this->table('users');
        $this->displayField('user_id');
        $this->primaryKey('user_id');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsToMany('Educations', [
            'foreignKey' => 'user_id',
            'targetForeignKey' => 'education_id',
            'joinTable' => 'users_educations'
        ]);
        $this->belongsToMany('Roles', [
            'foreignKey' => 'user_id',
            'targetForeignKey' => 'role_id',
            'joinTable' => 'users_roles'
        ]);
        $this->belongsToMany('Subjects', [
            'foreignKey' => 'user_id',
            'targetForeignKey' => 'subject_id',
            'joinTable' => 'users_subjects'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->requirePresence('username', 'create')
            ->notEmpty('username');

        $validator
            ->requirePresence('password', 'create')
            ->notEmpty('password');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['username']));
        $rules->add($rules->existsIn(['user_id'], 'Users'));
        return $rules;
    }

What i dont get is why the table users belongs to users because that is what my controller says. now i get a fault saying that:

SELECT Users.user_id AS `Users__user_id`, Users.username AS `Users__username`, Users.password AS `Users__password` FROM users Users INNER JOIN users Users ON Users.user_id = (Users.user_id) LIMIT 20 OFFSET 0

gives error: Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Table/alias: 'Users' non unique

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: