cakephp中find('list')的使用
运用一、快速实现下拉菜单
控制器中,使用find('list')返回的是键值对的数组,键名是array的第一个参数id,键值就是第二个参数content。
public function list_select(){
//查询键值对
$list = $this->Post->find('list',array('fields' => array('Post.id','Post.content')));
//var_export($list);
$this->set('options',$list);
}
$list的内容类似
array ( 1 => 'new_content', 2 => 'content2', 3 => 'new_content', 4 => 'c4', 5 => 'w', )
View中,$list的格式恰恰满足$options选项的要求
<?php
echo $this->Form->input('', array(
'name' => 's',
'options' => $options,
'empty' => '(choose one)'
));
?>
生成结果是个下拉列表,内容就是之前的键值对

运用二、将查询的结果集放到一个数组中
有表property_type,结构如图

$arr_ids = explode(',', '1,2,3');
$result = $this->PropertyType->find('all', array(
'fields'=>array('name'),
'conditions'=>array('id' => $arr_ids)
)
);
使用find('all')得到的结果,三条记录是三个数组
Array
(
[0] => Array
(
[PropertyType] => Array
(
[name] => House
) ) [1] => Array
(
[PropertyType] => Array
(
[name] => Unit
) ) [2] => Array
(
[PropertyType] => Array
(
[name] => Townhouse
) ) )
改为find('list')
$arr_ids = explode(',', '1,2,3');
$result = $this->PropertyType->find('list', array(
'fields'=>array('name'),
'conditions'=>array('id' => $arr_ids)
)
);
得到的所有结果在一个数组中,结构干净许多
Array
(
[1] => House
[2] => Unit
[3] => Townhouse
)
再将数组转为字符串 implode(',',$result);
cakephp中find('list')的使用的更多相关文章
- Cakephp中使用JavaScriptHelper来引入js文件
页面的head部分的内容在Cakephp中主要是有htmlhelper来进行控制的,而js部分则是由JavaScripthelper来进行控制的,在controller里面设置好:var $helpe ...
- cakephp中使用大括号的形式避免用点号连接sql语句
在cakephp中可以使用{}的形式来代替点号连接sql语句,减少出错的几率
- cakephp 中Console / Shell 有什么优点?
Which is the advantage of using CakePHP Console / Shell for programmed tasks ? 查看原文 最近用到了cakephp中的sh ...
- cakephp中使用 find('count')方法
对于find('count',array('group'=>'user_id')); Model.php中这样描述: /** * Handles the before/after filter ...
- CakePHP中回调函数的使用
我们知道模型主要是用来处理数据的,有时我们想在模型操作之前或之后做一些额外逻辑处理,这时候就可以使用回调函数. 回调函数有很多种,beforeFind,afterFind,beforeValidate ...
- cakephp中sql查询between
$trading_list = $this->Trading->find('all', array('conditions' => array('buy_time BETWEEN ? ...
- cakephp中sql查询in
$list = $this->Capital->find('all', array('conditions'=>array('remark in '=>array('银联支付' ...
- cakephp中sql查询大于
$list = $this->Capital->find('all', array('conditions'=>array('amount >'=>0)));
- [数据库]cakephp操作ENUM、tinyint等类型的一点说明
之前无法正常更新ENUM类型的数据,感觉是框架函数实现的bug. 问题很诡异,因为INIT的时候是可以成功写入的,没理由UPDATE的时候不成功. 前后琢磨了一下午,发现了一点蛛丝马迹才终于想通.问题 ...
随机推荐
- Type Systems
This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...
- win8开发
http://msdn.microsoft.com/library/default.aspx
- Oracle中转义下划线
原意是查询出所有的月粒度模型,但是在oracle中,下划线也代表匹配单一任何字符,导致15分钟粒度的模型也被查询出来,在此,需要对下划线做转义,使其只表示下划线的含义,可以使用ESCAPE()函数. ...
- python基础 列表和字符串
1.列表和字符串最大区别就是 列表可以改变,字符串不可以改变 列表: welldone = [ "hutu","python","java" ...
- android软件开发之TextView控件常用属性
TextView控件 text属性,设置显示的文本 textColor:设置文本颜色 textSize:设置文本字体大小 autoLink:设置文本为电话,URL连接等的时候是否显示为可点击的链接 c ...
- 第一个electron
1 开发环境:node环境 2 下载electron:npm install electron --save-dev 3 package.json配置如下: { "name": & ...
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.报该错误的一种原因。
今天发现某个action返回404. HTTP Status 404 – Not Found Type Status Report Message /xxx.action Description Th ...
- 【解决】could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
在同一套环境中跑了很多个项目都是用 docker-compose的方式启动的,导致创建的自定义网络过多出现下面的报错 Error response from daemon: could not fin ...
- MySQL必知必会 读书笔记二:MySQL使用
使用MySQL 选择数据库 使用USE关键字 USE database; 了解数据库和表 如果不知道可以使用的数据库名时,可用MySQL的SHOW命令来显示这些信息. SHOW DATABASES; ...
- 微信小程序车牌号码模拟键盘输入
微信小程序车牌号码模拟键盘输入练习, 未经允许,禁止转载,抄袭,如需借鉴参考等,请附上该文章连接. 相关资料参考:https://blog.csdn.net/littlerboss/article/d ...