Cakephp-3.x:如何更改所选别名的数据类型? [英] Cakephp-3.x: How to change the data type of a selected alias?

查看:275
本文介绍了Cakephp-3.x:如何更改所选别名的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试执行:

  $ fields = array('id'=>'custom_id','title '=>'some_name'); 

我得到的结果是 id



如果我这样做:

  $ fields = array 'custom_id','title'=>'some_name'); 

那么它将 custom_id / p>

如何获取 custom_id id 数据类型。我读了文档,但没有发现太多帮助。



有些东西,虚拟字段可以做的我认为。



感谢在进阶

解决方案

从CakePHP 3.2



可以使用 Query :: selectTypeMap()以添加更多类型,这些类型只有在检索数据时才用于投射所选字段。

  $ query = $ table 
- > find()
- > select(['alias'=>'actual_field',/ * ... * /]);

$ query
- > selectTypeMap()
- > addDefaults([
'alias'=>'integer'
]

您可以使用任何内置数据类型以及自定义数据类型。在这种情况下,别名字段现在将被转换为整数。



另请参阅





使用CakePHP 3.1及更早版本



你必须使用 Query :: typeMap(),这不仅会影响选择的字段,在各种其他地方,根据字段类型需要转换数据,这可能会导致不必要的冲突,因此谨慎使用。

  $ query 
- > typeMap()
- > addDefaults([
'alias'=>'integer'
]

另请参阅





更改现有列的类型



更改表的现有列的​​类型也是可能的,但是它们需要使用特定语法设置,即使用CakePHP使用的列别名格式,即表别名和由 __ 分隔的列名称,例如,对于具有 Articles 别名和名为 id ,它将是 Articles__id



手动或更好地通过 Query :: aliasField()检索,如:

  // $ field将看起来像['Alias__id'=> 'alias.id'] 
$ field = $ query-> aliasField('id',$ table-> alias());

$ query
- > selectTypeMap()
- > addDefaults([
key($ field)=>'string'
] );

这将更改 id



>

  • API> \ Cake\ Datasource\QueryInterface :: aliasField()


  • WHen i try to do :

    $fields = array('id' => 'custom_id', 'title' => 'some_name');
    

    The result I get has id as a string.

    If I do:

    $fields = array('custom_id', 'title' => 'some_name');
    

    then it gives custom_id as integer.

    How can I obtain custom_id as id without loosing the data type. I read the documentation but didn't found much help.

    There is something that virtual fields can do I think. But is it possible inside the find query without the use of virtual fields etc?

    Thanks in Advance

    解决方案

    As of CakePHP 3.2

    you can use Query::selectTypeMap() to add further types, which are only going to be used for casting the selected fields when data is being retrieved.

    $query = $table
        ->find()
        ->select(['alias' => 'actual_field', /* ... */]);
    
    $query
        ->selectTypeMap()
        ->addDefaults([
            'alias' => 'integer'
        ]);
    

    You can use any of the built-in data types, as well as custom ones. In this case the alias field will now be casted as an integer.

    See also

    With CakePHP 3.1 and earlier

    you'll have to use Query::typeMap(), which will not only affect the selected field when data is being retrieved, but in various other places too where data needs to be casted according to the field types, which might cause unwanted collisions, so use this with care.

    $query
        ->typeMap()
        ->addDefaults([
            'alias' => 'integer'
        ]);
    

    See also

    Change the type of existing columns

    Changing the type of an existing column of a table is possible too, however they need to be set using a specific syntax, ie in the column alias format used by CakePHP, that is, the table alias and the column name seprated by __, eg, for a table with the Articles alias and a column named id, it would be Articles__id.

    This can be either set manually, or better yet retrieved via Query::aliasField(), like:

    // $field will look like ['Alias__id' => 'Alias.id']
    $field = $query->aliasField('id', $table->alias());
    
    $query
        ->selectTypeMap()
        ->addDefaults([
            key($field) => 'string'
        ]);
    

    This would change the the default type of the id column to string.

    See also

    这篇关于Cakephp-3.x:如何更改所选别名的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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