CakePHP 3查询生成不正确的字段名称

时间:2016-08-05 21:02:43

标签: php mysql cakephp cakephp-3.x

我的控制器中的查询对象:

$this->Users
    ->find('all')
    ->matching('Projects')
    ->where(['User_has_access.projectid' => $id]);

创建此查询:

'sql' => '
    SELECT
        Users.userid AS `Users__userid`,
        Users.email AS `Users__email`,
        Users.password AS `Users__password`,
        Users.first_name AS `Users__first_name`,
        Users.last_name AS `Users__last_name`,
        Users.affiliation AS `Users__affiliation`,
        Users.address AS `Users__address`,
        Users.city AS `Users__city`,
        Users.state AS `Users__state`,
        Users.zip AS `Users__zip`,
        Users.phone AS `Users__phone`,
        Users.role AS `Users__role`,
        Users.created_at AS `Users__created_at`,
        Users.modified AS `Users__modified`,
        Users.deleted AS `Users__deleted`,
        Projects.projectid AS `Projects__projectid`,
        Projects.userid AS `Projects__userid`,
        Projects.name AS `Projects__name`,
        Projects.point_of_contact AS `Projects__point_of_contact`,
        Projects.description AS `Projects__description`,
        Projects.start_date AS `Projects__start_date`,
        Projects.end_data_date AS `Projects__end_data_date`,
        Projects.end_date AS `Projects__end_date`,
        Projects.testing_for AS `Projects__testing_for`,
        Projects.monday AS `Projects__monday`,
        Projects.tuesday AS `Projects__tuesday`,
        Projects.wednesday AS `Projects__wednesday`,
        Projects.thursday AS `Projects__thursday`,
        Projects.friday AS `Projects__friday`,
        Projects.saturday AS `Projects__saturday`,
        Projects.sunday AS `Projects__sunday`,
        Projects.created_at AS `Projects__created_at`,
        Projects.modified AS `Projects__modified`,
        Projects.deleted AS `Projects__deleted`,
        User_has_access.userid AS `User_has_access__userid`,
        User_has_access.projectid AS `User_has_access__projectid`,
        User_has_access.access AS `User_has_access__access`,
        User_has_access.created_at AS `User_has_access__created_at`,
        User_has_access.modified AS `User_has_access__modified` 
    FROM
        users Users 
    INNER JOIN
        projects Projects 
            ON 1 = 1 
    INNER JOIN
        user_has_access User_has_access 
            ON (
                Users.userid = (
                    User_has_access.user_id
                ) 
                AND Projects.projectid = (
                    User_has_access.project_id
                )
            ) 
    WHERE
        User_has_access.projectid = :c0
',
'params' => [
    ':c0' => [
        'value' => '28',
        'type' => null,
        'placeholder' => 'c0'
    ]
],

问题在于内连接

INNER JOIN
    user_has_access User_has_access 
        ON (
            Users.userid = (
                User_has_access.user_id
            ) 
            AND Projects.projectid = (
                User_has_access.project_id
            )
        )

user_has_access中使用user_idproject_id应为useridprojectid的字段名称不正确。除此之外,查询执行了应该执行的操作,即查找与特定项目关联的所有用户。用户可以有很多项目,项目可以有很多用户。

表类在这里:

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

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

        $this->addBehavior('Timestamp');

        $this->hasMany('Datapoints',[
          'foreignKey' => 'userid'
        ]);

        //$this->hasMany('Projects',[
        //  'foreignKey' => 'userid'
        //]);

        $this->belongsToMany('Projects', [
            'through' => 'User_has_access',
        ]);
    }
}

这里:

class ProjectsTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('projects');
        $this->displayField('name');
        $this->primaryKey('projectid');

        $this->addBehavior('Timestamp');
        $this->hasMany('Datapoints',[
          'foreignKey' => 'projectid'
        ]);

        $this->belongsToMany('Users', [
            'through' => 'User_has_access',
        ]);
    }

在这里:

class User_has_accessTable extends Table
{
    public function initalize(array $config)
    {
        parent::initalize($config);

        $this->entityClass('App\Model\Entity\User_has_access');

        $this->table('user_has_access');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Projects', [
            'foreignKey' => 'projectid',
            'joinType' => 'INNER',
        ]);
        $this->belongsTo('Users',[
            'foreignKey' => 'userid',
            'joinType' => 'INNER',
        ]);
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。问题是,当我应该让Cake烘焙它时,我手动创建了模型文件。我还浏览了我的数据库并更改了我的表和字段名称,以遵循Cake的命名约定。这似乎解决了所有问题