I cant display fields from a table with an underscore in cakephp3

梦想与她 提交于 2019-12-08 11:14:56

问题


I am getting this error on a field that does exist. I cant display any field from the TutoringTypes table. My connection is wrong and i cant see where I made the mistake as I believe I followed the conventions. However the docs talk about plural table names but give singular variables names as an example?

Column not found: 1054 Unknown column 'tutoring_types.value' in 'field list'

$query3 = $this->Lessons->find()
     ->contain(['Tutors', 'TutoringTypes'])
     ->select(['lessons.id','lessons.lesson_date','tutors.id','tutors.first_name','tutors.last_name','lessons.tutoring_type_id',
                  'tutoring_types.value'])      
     ->where(['Lessons.lesson_date >' => $a3,'Lessons.lesson_date <' => $a4,
          'OR' => [['lessons.tutoring_type_id' => 2], ['lessons.tutoring_type_id' => 1]]
     ]);     


Lessons Model
    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->belongsTo('TutoringTypes', [
        foreignKey' => 'tutoring_type_id'
        ]);
//////


SELECT 
  lessons.id AS `lessons__id`, 
  lessons.lesson_date AS `lessons__lesson_date`, 
  tutors.id AS `tutors__id`, 
  tutors.first_name AS `tutors__first_name`, 
  tutors.last_name AS `tutors__last_name`, 
  lessons.tutoring_type_id AS `lessons__tutoring_type_id`, 
  tutoring_types.value AS `tutoring_types__value` 
FROM 
  lessons Lessons 
  LEFT JOIN tutors Tutors ON Tutors.id = (Lessons.tutor_id) 
  LEFT JOIN tutoring_types TutoringTypes ON TutoringTypes.id = (Lessons.tutoring_type_id) 
WHERE 
  (
    Lessons.lesson_date > '2015-05-30' 
    AND Lessons.lesson_date < '2016-06-01' 
    AND (
      lessons.tutoring_type_id = '2' 
      OR lessons.tutoring_type_id = '1'
    )
  )

http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html http://book.cakephp.org/3.0/en/intro/conventions.html


回答1:


That's not how things are ment to be done. In queries you have to use the table/association aliases with which they have been registered/selected, not the actual table names.

By default the aliases follow the table class name convention, that is, plural and camel cased/capsed. So instead of tutoring_types, you'd use TutoringTypes, just look at the aliases in the generated SQL.

The plural table name, singular variable name example is a different convention that has nothing to do with table aliases, but with association types (1:n/n:n = plural, n:1/1:1 = singular) and entities (one entity = one record, hence singular names are used).

btw, you are receiving that error only for tutoring_types because you are using a DBMS that handles identifiers in a case insensitive manner, ie lessons will match Lessons, but tutoring_types will of course not match TutoringTypes, as it's not even the same when treated case insensitive.



来源:https://stackoverflow.com/questions/37714749/i-cant-display-fields-from-a-table-with-an-underscore-in-cakephp3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!