2017-07-06 21 views
2

私は以下のようなテーブルを持っています。今は年ごとに行を取得することはできません。CakePHP 3 - 日付(年)でテーブルを含む行を選択してください

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Matches.date' in 'where clause'

私の "マッチ" テーブル

id | goalsfor | goalsagainst | date 
1 | 5   | 4    | 2017-07-05 15:57:36 
2 | 9   | 5    | 2017-07-05 17:06:31 
3 | 7   | 8    | 2015-07-05 15:57:36 
4 | 0   | 2    | 2014-07-05 15:57:36 
6 | 5   | 4    | 2014-07-05 15:57:36 
7 | 7   | 9    | 2014-07-05 15:57:36 

マイクエリ:

$playedMatches = $this->Players 
    ->find() 
    ->contain([ 
     'MatchePlayers' => [ 
      'Matches' 
     ] 
    ]); 

$playedMatches->where(['YEAR(Matches.date) =' => "2017"]); 

マイ団体:私はこれをしようとすると、しかし、私は次のエラーを取得します

含まれているため、あなたの構文が間違っている
class PlayersTable extends Table { 

    public function initialize(array $config) { 
     parent::initialize($config); 

     $this->setTable('players'); 
     $this->setDisplayField('id'); 
     $this->setPrimaryKey('id'); 

     $this->hasMany('MatchePlayers', [ 
      'className' => 'MatchPlayers', 
      'foreignKey' => 'player_id', 
      'propertyName' => 'matches', 
      'dependent' => true 
     ]); 
    } 
} 

class MatchPlayersTable extends Table { 

    public function initialize(array $config) { 
     parent::initialize($config); 

     $this->setTable('matches_players'); 
     $this->setDisplayField('id'); 
     $this->setPrimaryKey('id'); 

     $this->belongsTo('Players', [ 
      'className' => 'Players', 
      'foreignKey' => 'player_id', 
      'propertyName' => 'player' 
     ]); 

     $this->belongsTo('Matches', [ 
      'className' => 'Matches', 
      'foreignKey' => 'match_id', 
      'propertyName' => 'match' 
     ]); 
    } 
} 

答えて

2

をハッピー

$playedMatches = $this->Players 
->find() 
->contain([ 
    'MatchePlayers.Matches' 
]); 
$playedMatches->where(['YEAR(MatchePlayers.Matches.date) =' => "2017"]); 

、これを試してみてください、私はあなたの名前混乱の選択を見つける:MatchPlayers、MatchePlayersを... 。とにかく

:あなたはbelongsToMany関係を持っているときケーキは単一のクエリを実行しません(とプレイヤーbelongsToManyはトラフmatches_playersと一致する)

したがって、contains節に条件を指定する必要があります

$playedMatches = $this->Players 
->find() 
->contain([ 
    'MatchePlayers.Matches' => function ($q) 
    { 
     return $q->where(['YEAR(Matches.date) =' => "2017"]) 
    } 
]); 
+0

これは素晴らしい作品です! tx – CodeWhisperer

1

クエリに誤りがあり、コーディング:)

+0

MatchePlayersで一致する必要があるため、私の場合は動作しません。私は私の質問に関係を追加しました。 – CodeWhisperer

+0

同じエラー:エラー:SQLSTATE [42S22]:列が見つかりません:1054不明な列 'where句の' MatchePlayers.Matches.date ' – CodeWhisperer

+0

pr($ playsMatches); die;あなたの場所の前に配列が空であるかどうかをチェックし、配列の 'Date'列が取得されているかどうかを教えてください。 @CodeWhisperer –

関連する問題