如何通过在CakePHP 3中组合/有两个字段来创建keyValue对(显示字段)? [英] How do I create a keyValue pair (display field) by combining/having two fields in CakePHP 3?

查看:238
本文介绍了如何通过在CakePHP 3中组合/有两个字段来创建keyValue对(显示字段)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 find get 来获取两个值(用于人性化的下拉列表) / strong>或其他任何东西。我需要CakePHP最简单的方法来帮助你。



这是我的尝试:

$ ol

  • 在我的控制器中

      $ users = $ this-> LocationsUser-> Users-> find('list',[
    'limit'=> 1,
    'keyField'=>'id',
    'valueField'=> ['first_name','last_name ']
    ]) - >其中(['id'=> $ id]);


  • 在我看来,

    'multiple'=> false,
    ]);


  • 结果显示在我的下拉列表中:

     < option value =10> Fabian; Pankiers< / option> 


  • 看到我需要一个没有分号的结果<强> ;即可。现在我可以使用JavaScript来删除分号,但那就是矫枉过正。有没有一种简单的方法来实现这个在CakePHP 3?

    解决方案

    有多种方法可以解决这个问题,这里有三个:

    valueField使用回调



    一个回调函数,并使用结果中的值将这些值拼接在一起。

      $ query = $ this-> LocationsUser->用户
    - > find('list',[$ b $'valueField'=> function($ row){
    return $ row ['first_name']。''。$ row [ 'last_name'];
    }
    ])
    - >其中(['id'=> $ id]);

    另请参阅



    结果格式化程序



    使用结果格式化程序将这些值拼接在一起,这就是 list finder在内部也是这样做的,基本上和上面的例子一样,只是更复杂

      $ query = $ this-> LocationsUser->用户
    - > find()
    - > select(['id','first_name','last_name'])
    - > formatResults(function($ results){
    / * @var $ results \Cake\\ \\数据源\结果集接口| \Cake\Collection\CollectionInterface * /
    返回$结果 - >组合(
    'id',
    函数($ row){
    $ return ['first_name']。''。$ row ['last_name'];
    }
    );
    })
    - > where(['id '=> $ id]);



    结合虚拟属性



    还可以在这里使用(重新)使用虚拟属性,例如在 User 实体类中定义的属性名称 full_name

    pre $保护函数_getFullName()
    {
    return $ this-> _properties ['first_name' ]。 ''。 $这 - > _properties [姓氏];





    $ b您可以在格式化程序中将其返回,而不是再次手动合并这两个字段:

     函数($ row){
    return $ row ['full_name'];



    $ b $ p $也可以在 valueField code> example。



    另请参阅



    计算字段



    添加一个计算的字段并使用该字段作为 valueField 选项

      $ query = $ this-> LocationsUser-> Users 
    - > find('list',[
    'keyField'=>'id',
    ' valueField'=>'concatenated'
    ]);
    $ query
    - > select([
    'id',
    'concatenated'=> $ query-> func() - > concat([
    'first_name'=>'literal',
    '',
    'last_name'=>'literal'
    ])
    ])
    - > ;其中(['id'=> $ id]);

    另请参阅



    覆盖列表查找器



    如果您希望始终应用自定义列表,则可以在 Users 中覆盖 list finder c $ c> table class。

     使用Cake\ORM\Query; 



    公共函数findList(查询$查询,数组$ $选项)
    {
    返回$查询
    - > find()
    - > select(['id','first_name','last_name'])
    //等等...
    ;



    $ b $ p
    $ b

    这样你可以继续使用 find('


    $ b


  • $ b
    食谱>数据库访问&安培; ORM>检索数据&结果集>自定义查找方法


  • I need help getting two values (for human friendly dropdown) in a key-value pair using find or get or anything else. I need help with the simplest method of doing this in CakePHP.

    Here's my attempt:

    1. in my controller

        $users = $this->LocationsUser->Users->find('list', [
          'limit' => 1,
          'keyField' => 'id',
          'valueField' => ['first_name', 'last_name']
        ])->where(['id' => $id]);
      

    2. In my view

        echo $this->Form->input('user_id', [
              'options' => $users,
              'type' => 'select',
              'multiple' => false,
            ]);
      

    3. The result on my drop-down:

      <option value="10">Fabian;Pankiers</option>
      

    See I need a result without the semicolon ";". Now I can use javascript to remove the semicolon but then thats overkill. Is there a simple way to achieve this within CakePHP 3?

    解决方案

    There are various ways to solve this, here's three of them:

    Use a callback for the valueField

    Use a callback and stitch the value together by using values from the results.

    $query = $this->LocationsUser->Users
        ->find('list', [
            'valueField' => function ($row) {
                return $row['first_name'] . ' ' . $row['last_name'];
            }
        ])
        ->where(['id' => $id]);
    

    See also

    Result formatters

    Use a result formatter that stitches the values together, that's what the list finder does internally too, and basically the same as the above example, just more complicated.

    $query = $this->LocationsUser->Users
        ->find()
        ->select(['id', 'first_name', 'last_name'])
        ->formatResults(function($results) {
            /* @var $results \Cake\Datasource\ResultSetInterface|\Cake\Collection\CollectionInterface */
            return $results->combine(
                'id',
                function($row) {
                    return $row['first_name'] . ' ' . $row['last_name'];
                }
            );
        })
        ->where(['id' => $id]);
    

    Combined with virtual properties

    You could also use (re-)use virtual properties here, given for example a property name full_name defined in your User entity class:

    protected function _getFullName()
    {
        return $this->_properties['first_name'] . ' ' . $this->_properties['last_name'];
    }
    

    You could just return that in the formatter instead of manually combining the two fields again:

    function($row) {
        return $row['full_name'];
    }
    

    The same could be done in the valueField example.

    See also

    Computed fields

    Add a computed field and use that one for the valueField option

    $query = $this->LocationsUser->Users
        ->find('list', [
            'keyField' => 'id',
            'valueField' => 'concatenated'
        ]);
    $query
        ->select([
            'id',
            'concatenated' => $query->func()->concat([
                'first_name' => 'literal',
                ' ',
                'last_name' => 'literal'
            ])
        ])
        ->where(['id' => $id]);
    

    See also

    Override the list finder

    If you want your custom list to be always applied, you could override the list finder in your Users table class.

    use Cake\ORM\Query;
    
    // ...
    
    public function findList(Query $query, array $options)
    {
        return $query
            ->find()
            ->select(['id', 'first_name', 'last_name'])
            // etc ...
            ;
    }
    

    That way you can continue to use find('list').

    See also

    这篇关于如何通过在CakePHP 3中组合/有两个字段来创建keyValue对(显示字段)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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