我不能在cakephp3中显示带下划线的表中的字段 [英] I cant display fields from a table with an underscore in cakephp3

查看:96
本文介绍了我不能在cakephp3中显示带下划线的表中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这个错误的字段,确实存在。我不能显示TutoringTypes表中的任何字段。我的连接是错误的,我不能看到我犯了错误,因为我相信我遵守惯例。

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?

未找到列:1054'field list'中的未知列'tutoring_types.value'

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

推荐答案

这不是要做的事情。在查询中,您必须使用他们已经注册/选择的表/关联别名,而不是实际的表名。

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.

默认情况下,别名遵循表类名惯例,即复数和骆驼套/封。因此,除了 tutoring_types ,您可以使用 TutoringTypes ,只需查看生成的SQL中的别名。

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.

复数表名,单变量名示例是与表别名无关的不同约定,但是具有关联类型( 1:n / n:n = plural, n:1 1 =单数)和实体(一个实体=一个记录,因此使用单个名称)。

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, tutoring_types ,因为您使用的DBMS以不区分大小写的方式处理标识符,即 lessons 将匹配 Lessons ,但 tutoring_types 当然不会匹配 TutoringTypes 即使在处理不区分大小写时也是如此。

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.

这篇关于我不能在cakephp3中显示带下划线的表中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆