different output for model name with capital letter in cakephp3

最后都变了- 提交于 2019-12-08 11:17:20

问题


I dont understand how I get a different output for lesson_date field if in the select I use Lessons.lesson_date' or lessons.lesson_date. I thought I am supposed to use Lessons and not lessons and that the for a single name it doesnt really matter.

with Lessons.lesson_date I get on the debug :

'lesson_date' => object(Cake\I18n\FrozenDate) {
        'time' => '2015-07-09T00:00:00+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },

with lessons.lesson_date I get a better output:

'lessons' => [
        'id' => '5399',
        'lesson_date' => '2015-07-09'

//this is the code below I am talking about. The Lessons.lesson_date gives a different output than if I change this to lessons.lesson_date

 $query3 = $this->Lessons->find()
              ->contain(['TutoringTypes'])
              ->select(['lessons.id','Lessons.lesson_date','Lessons.tutoring_type_id',
                  'TutoringTypes.value'])      
               ->where(['Lessons.lesson_date >' => $a3,'Lessons.lesson_date <' => $a4, .....

回答1:


That is the expected/correct behavior when following the conventions, the date gets casted according to its type.

The latter output might be better suited for what you are doing with the data, but generally the former is "better" since a date object gives you more freedom of manipulating dates, handling localization, output formatting, etc...

Why?

As to the why, the output is different because the ORM will not cast the value in case of a non-conventional column alias, as it's not present in the type map that holds the information about which column is of which type.

Using lessons.lesson_date will create an alias of

lessons__lesson_date

which is not following the conventions, where as using Lessons.lesson_date will create an alias of

Lessons__lesson_date

which does follow the conventions, and will match the field in the type map, causing the ORM to cast the data.

Changing the behavior

If you need YYYY-MM-DD output, then you could simply output it formatted it in your view like

echo $lesson->lesson_date->i18nFormat('yyyy-MM-dd')

or change the default output format (which is used when the date gets casted to a string)

\Cake\I18n\FrozenDate::setToStringFormat('yyyy-MM-dd');

or maybe even change the type in the type map in order to keep the date string as is

    $query = $this->Lessons
        ->find()
        // ...

    $types = ['Lessons__lesson_date' => 'string'] + $query->selectTypeMap()->defaults();
    $query->selectTypeMap()->defaults($types);

See also

  • Cookbook > Date & Time > Formatting
  • Cookbook > Date & Time > Setting the Default Locale and Format String

  • \Cake\ORM\Query::_addDefaultSelectTypes()

  • \Cake\Database\Query::selectTypeMap()
  • \Cake\Database\Query::typeMap()


来源:https://stackoverflow.com/questions/37716863/different-output-for-model-name-with-capital-letter-in-cakephp3

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