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

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

问题描述

我需要获得帮助,您可以使用查找 获取获取键值对中的两个值(适合人性化的下拉菜单) / strong>或任何其他。我需要帮助在CakePHP中这样做最简单的方法。



这是我的尝试:


  1. 我的控制器

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


  2. 在我看来

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


  3. 我的下拉菜单中的结果:

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


看到我需要一个没有分号的结果;。现在我可以使用javascript来删除分号,但然后这是overkill。有没有一个简单的方法来实现这个在CakePHP 3?

解决方案

有多种方法来解决这个,



使用回应 valueField



使用回调并使用结果中的值将值拼接在一起。

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

另请参阅





结果格式化程序



使用将值连接在一起的结果格式化程序,这也是内部的列表,并且基本上与上面的例子相同,只是更复杂。

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



与虚拟属性结合



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

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

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

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

同样可以在 valueField example。



另请参阅





计算字段



添加计算字段并将其用于 valueField 选项

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

另请参阅





覆盖列表查找器



如果您希望自定义列表始终应用,可以覆盖用户列表 finder c $ c>表类。

 使用Cake\ORM\Query; 

// ...

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

这样你可以继续使用 find




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天全站免登陆