[英]cakePHP build a select box from custom array
因此,我们有各种各样的搜索页面,每个搜索页面都有不同的搜索条件,所以我决定编写一个组件,该组件将从控制器传递给它的参数,收集必要的数据并返回一个数组,然后可以将其设置为在我的内部使用查看要填充的文件下拉框以过滤条件。
我设法使所有内容写到必须使用cakePHP帮助程序来构建动态选择框的位置。 我坚信我做错了什么,如果有更简单的方法可以使它保持动态状态,请在可能的地方提供帮助:
// COMPONENT METHOD:
public function filterQueries($parameters) {
// Get defaults from the user:
$criteria = $parameters["custom"];
$defaults = $parameters["defaults"];
// Validate the defaults the user may want and assign them to the return array:
if($defaults != "false") {
foreach($defaults as $key => $value) {
if(array_key_exists($value, $this->defaults)) {
$this->returnArray["defaults"][$value] = $this->defaults[$value];
}
}
}
// Get all data for the custom requested form fields:
if($criteria != false) {
foreach($criteria as $model => $arguments) {
$fields = $arguments["fields"];
$conditions = $arguments["conditions"];
$recursive = $arguments["recursive"];
if(!in_array($model,$this->uses)) {
$useModel = ClassRegistry::init($model);
} else {
$useModel = $this->$$model;
}
$array = $useModel->find("all",array("conditions" => $conditions, "fields" => $fields, "recursive" => $recursive));
$this->returnArray["custom"][$model] = $array;
}
}
return $this->returnArray;
}
上面的函数将获得一个数组,该数组可以分解自定义搜索或默认值(以上未包括在内,但是全部有效,它返回的数组与我期望的完全相同。
// The code within my action to get the content from above:
// Load the Filters component to search data:
$search = $this->Components->load("Filter");
// Tell search what you want:
$searchBoxes = array(
"defaults" => array("statuses", "survey_type"),
"custom" => array(
"User" => array(
"fields" => array("User.id","User.first_name", "User.last_name"),
"conditions" => array("User.user_group_id" => "4f847c63-1840-446e-88be-3e4d29566cf0"),
"recursive" => -1
)
)
);
$filterResults = $search->filterQueries($searchBoxes);
$this->set("filters",$filterResults);
因此,现在我在视图文件中得到了这个多关联数组(仍然很好),但是现在我想基于上面创建的数组构建一个用户下拉列表示例,但是结果却与我的预期不同:
echo $this->Form->input('user_id',
array(
"type" => "select",
"options" => $filters["custom"]["User"]
)
);
HTML输出已损坏,并显示如下:
<option value="last_name">Doe</option>
<option value="first_name">Jihn</option>
<optgroup label="User"> </optgroup>
<optgroup label="1"> </optgroup>
<option value="last_name">Marilyn</option>
<option value="first_name">Monroe</option>
我承认我没有很多蛋糕方面的经验,但是无法理解如何将结果简单地转换为:
<option value='USERID'>NAME</option> // Yes I know the names and surnames must be concatinated still
任何关于如何做到这一点以及以正确方式做到的建议帮助或指导,将不胜感激:)
VARDUMP ON $ filters ['custom'] ['users']
array
0 =>
array
'User' =>
array
'id' => string '4f84840e-cda8-4704-8fdf-210729566cf0' (length=36)
'first_name' => string 'Name' (length=4)
'last_name' => string 'Surname' (length=11)
1 =>
array
'User' =>
array
'id' => string '4f8488cb-53e0-4f72-af73-3de229566cf0' (length=36)
'first_name' => string 'Name' (length=6)
'last_name' => string 'Surname' (length=6)
您可以通过执行以下操作来增强输出:
1)要合并表的两个字段,可以在模型中使用"virtualfields"
,如下所示:例如,如果您有用户模型,则可以定义如下:
public $virtualFields = array(
'full_name' => 'CONCAT(first_name, " ",last_name)'
);
因此,无论何时调用User模型的find
方法,都将获得“ full_name”字段。
2)为了从选择框中的表中获取数据,可以使用find('list')
方法。 例如对于用户模型,如果您需要表的id,full_name (last and first name combined using the virtual fields)
,可以按以下步骤进行操作:
$this->User->find('list',array('fields'=>array('id','full_name'),'conditions'=>$conditions))
我希望这有帮助..
好吧,我想您实际上想要创建另一个带有格式化选项的数组。
foreach ($filters["custom"]["User"] as $arr)
{
$options[$arr['id']] = $arr['first_name'] . ' ' . $arr['last_name'];
}
然后
echo $this->Form->select('user_id', $options);
我认为您需要这样的东西:
$result = Set::combine($filters["custom"]["User"],
'{n}.User.id', // this is the value of select box
array(
'{0} {1}',
'{n}.User.first_name',
'{n}.User.last_name'
) // this is the text of select box
);
pr($result);
$ this-> form-> input('inputname',array('label'=> false,'options'=> array('Select optionstype','a','b'),'class'=> array( 'form-a','b'),'id'=>'bacid','style'=>'width:280px;','value'=> $ abc ['model'] ['array_attribute']) );
在cakePHP 3上,我还不能使用“ virtualFields”,但是可以使用以下方法:
//In PostsController
$users = $this->Posts->Users->find('list',
['keyField' => 'id',
'valueField' => 'full_name', //Don't forget to call concatened field here
'conditions' => $conditions,
'order' => $orderBy
]);
//here the magic happens
$concat = $users->func()->concat(
['Users.first_name' => 'identifier',
' ',
'Users.last_name' => 'identifier'
]);
//selecting fields
$users->select(['id', 'full_name' => $concat]);
//sending to view
$this->set('users', $users->toArray());
我希望这对CakePHP 3开发人员有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.