English 中文(简体)
cakephp
原标题:cakephp find list
  • 时间:2010-09-24 09:34:00
  •  标签:
  • cakephp

然而,我希望能够利用发现得出一个名单,以便我能够选择帮助者。 但有问题。 i 要求过多,姓名(第一次+最后一次)。 因此,我如何能够做到这一点。 我首先要把姓名和最后名字作为名字。 我如何能够做到这一点。

$this->User->find( all ,array( fields  => array( first_name , last_name , id )));

我可以使用模型过滤器和反馈。 我建议我如何能够在控制者自己中这样做。

最佳回答

我认为,可以采用以下模型中的<代码>虚拟领域和<编码> 外地<>/代码>特性。

在您的模型中,确定了一个全名的虚拟领域:

public $virtualFields = array(
     full_name  =>  CONCAT(User.first_name, " ", User.last_name) 
);

如果你现在开张, 填满姓名的外地人员应当能够找到一份用户名单,其中附有<代码>$this->User->find(清单), 您可以在无问题的情况下使用。

public $displayField =  full_name ;

public $displayField =  User.full_name ;

补贴自动发放。

问题回答

另一种解决办法是使用《面包一套: 共同建设你们所需要的东西......

$users = $this->User->find( all ,array( fields  => array( first_name , last_name , id )));

$user_list = Set::combine($users,  {n}.User.id , array( {0} {1} ,  {n}.User.first_name ,  {n}.User.last_name ));

成果将探讨这样的问题:

array(
 [2] =>  First Last ,
 [5] =>  Bob Jones 
)

文件链接:

http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set:combine

实现这一点,首先要看模式,并添加这一条。

  public $virtualFields = array( full_name  =>  CONCAT(first_name, " ", last_name) );

接着,控制者只使用你在虚拟领域使用的“全称”的名称。

$this->User->find( all ,array( fields  => array( full_name , id )));

名称与合并田

就我而言,Seta:combine是前进的道路,因为我不得不处理相关领域的分类问题,例如:

$bancos_enteros = $this->Financiacion->Banco->find( all , array(
   fields  => array( Empresa.codigo_contable , Empresa.nombre_corto ,  Banco.id ),
   order  => array( Empresa.codigo_contable  =>  asc ),
   recursive  => 1
));
$bancos = Set::combine(
   $bancos_enteros,
    {n}.Banco.id ,
    array(
        {0} {1} ,
        {n}.Empresa.codigo_contable ,
        {n}.Empresa.nombre_corto 
     )
 );

返回

    array(
    (int) 14 =>  57200002 Caixa ,
    (int) 15 =>  57200003 Sabadell ,
    (int) 3 =>  57200005 BBVA ,
    (int) 16 =>  57200006 Deutsche Bank ,
    (int) 17 =>  57200007 Popular ,
    (int) 18 =>  57200009 March ,
    (int) 26 =>  57200010 Bankinter ,
    (int) 4 =>  57200011 Santander 
)

......

$this->Financiacion->Banco->Empresa->virtualFields = array(
     codigo_nombre  =>  CONCAT(Empresa.codigo_contable,Empresa.nombre_corto) 
);
$this->Financiacion->Banco->virtualFields[ codigo_nombre ] =  $this->Financiacion->Banco->Empresa->virtualFields[ codigo_nombre ];
$bancos = $this->Financiacion->Banco->find( list , array(
    fields  => array( Banco.id , Banco.codigo_nombre ),
    order  => array( Banco.codigo_nombre  =>  asc ),
    recursive  => 1
    )   
);

如果我不首先删除虚拟领域的话,我就在以下询问中发现错误:

unset($this->Financiacion->Banco->Empresa->virtualFields);
unset($this->Financiacion->Banco->virtualFields);

Update for CakePHP 4x.

// Model/Entity/{entityName}.php
// "fname, lname, email" are my DB field names
protected function _getFullName()
{
    return $this->_fields[ fname ] .     . $this->_fields[ lname ];
}

protected function _getLabelDropdown()
{
    return $this->_fields[ fname ] .     . $this->_fields[ lname ] .   (  . $this->_fields[ email ] .  ) ;
}

...


// Model/Table/{tableName}.php
public function getUserList()
{
    return $this->find( list , [
         keyField  =>  uuid ,
         valueField  => function ($user) {
            return $user->get( full_name ); // It s _getFullName()
            // or 
            // return $user->get( label_dropdown ); // It s _getLabelDropdown()
        }
    ])->where([
         is_delete  => 0,
         is_active  => 1,
    ]);
}

// Controller/{ControllerName}.php
public function add()
{
  ...

  $this->set( user_list , $this->Users->getUserList());

  ...
}

// View
<?= $this->Form->control( role_uuid , [
    options  => $user_list,
    class  =>  form-select ,
    id  =>  user_uuid ,
    label  => __( Choose User ),
    default  => DEFAULT_USER
]); ?>

阅读更多:https://book.cakephp.org/4/en/orm/retrieving-data-and-resultsets.html#customize-key- Value-out

希望会提供帮助。





相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

Using DISTINCT in a CakePHP find function

I am writing a CakePHP 1.2 app. I have a list of people that I want the user to be able to filter on different fields. For each filterable field, I have a drop down list. Choose the filter ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

Prevent controller from trying to autoload model

I am a beginning Cake user and trying to do some work on an already existing application. Running into a problem when I create a new controller. I have created StoreController and when I try to call ...

热门标签