2016-08-05 16 views
0

ここに私のコントローラ内のクエリオブジェクト:CakePHPの3クエリの生成不正なフィールド名

$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である必要があります。それ以外のことは、特定のprojectidに関連付けられているすべてのユーザーを検索することです。ユーザーは多くのプロジェクトを持つことができ、プロジェクトは多くのユーザーを持つことができます。

テーブルクラスはここにある:

ここ
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', 
     ]); 
    } 
} 

答えて

0

私は問題を修正しました。問題は、私が手動でモデルファイルを作成していたときに、Cakeがそれらを焼くべきだったということでした。私もデータベースに行き、Cakeの命名規則に従ってテーブルとフィールドの名前を変更しました。それはすべてを修正するように見えた

関連する問題