在cakephp3中使用大写字母的型号名称的不同输出 [英] different output for model name with capital letter in cakephp3
问题描述
我不明白我如何获得不同的输出为lesson_date字段如果在选择我使用Lessons.lesson_date'或lessons.lesson_date。我认为我应该使用课程,而不是课程,并且对于一个单一的名称,它并不重要。
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.
与Lessons.lesson_date我得到的调试: p>
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我得到更好的输出:
with lessons.lesson_date I get a better output:
'lessons' => [
'id' => '5399',
'lesson_date' => '2015-07-09'
//这是我在说的代码。 Lessons.lesson_date提供了一个不同的输出,如果我将其更改为lessons.lesson_date
//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, .....
推荐答案
后面的输出可能更适合你对数据的处理,但通常前者是更好的因为日期对象可以让您更自由地操作日期,处理本地化,输出格式等...
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...
至于为什么,输出是不同的,因为在非常规列别名的情况下,ORM不会转换值,因为它不存在于类型映射中,该类型映射包含哪个列是哪个类型的信息
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.
使用 lessons.lesson_date
将创建
lessons__lesson_date
不遵循约定,其中使用 Lessons.lesson_date
将创建
which is not following the conventions, where as using Lessons.lesson_date
will create an alias of
Lessons__lesson_date
如果您需要 YYYY-MM-DD
输出,那么你可以简单地输出格式化它在你的视图中
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')
b $ b
或更改默认输出格式(当日期转换为字符串时使用)。
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);
另请参阅
- 食谱>日期&时间>格式
-
Cookbook> Date&时间>设置默认区域设置和格式字符串
See also
- Cookbook > Date & Time > Formatting
Cookbook > Date & Time > Setting the Default Locale and Format String
\Cake\ORM\Query :: _ addDefaultSelectTypes()
\Cake\ORM\Query::_addDefaultSelectTypes()
这篇关于在cakephp3中使用大写字母的型号名称的不同输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!