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的时候不成功. 前后琢磨了一下午,发现了一点蛛丝马迹才终于想通.问题 ...
随机推荐
- BZOJ2333:[SCOI2011]棘手的操作(Splay)
Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 x v: ...
- PHP中__get()和__set()的用法实例详
刚刚看到一个对我有用的文章,我就把它摘抄下来了. php面 ...
- Leetcode 225 两个队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- javaWeb中怎么获取提交表单里面的值
在javaWeb中如何获得html文件中的表单里面的值? <!DOCTYPE html> <html> <head> <meta charset=" ...
- Python入门基础:代码的编码风格
每种语言都有自己的编码风格,对于Python这种比较注重于空格的影响的代码而言,其风格也是相当重要的. 主要包括以下几点: 1:使用 4 空格缩进,而非 TAB .在小缩进(可以嵌套更深)和大缩进( ...
- 如何解决使用JMeter时遇到的问题
Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域. 它可以用于测试静态和动态资源例如静态文件. ...
- C/C++ Windows API——获取计算机信息 转
转自:http://blog.csdn.net/chy555chy/article 函数 头文件 作用 GetVersionEx <windows.h> 获取系统版本信息(deprecat ...
- IP Addressing
IP Addressing(处理) Each host on Internet has unique 32 bit IP address Each address has two parts: net ...
- Ubuntu下配置安装telnet server
1.安装xinetd 以及telnetd # apt-get install xinetd telnetd 2.配置文件/etc/inetd.conf #vi /etc/inetd.conf # St ...
- Sftp搭建与配置参考
Sftp搭建与配置参考 1. 介绍 sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一 ...