CakePHP: 一些资料和记录

CakePHP配置: [url]http://panyongzheng.iteye.com/blog/1961699[/url]

Cake的全局常量及方法 [url]http://docs.30c.org/cakephp/globalconstants.html[/url]
CakePHP2命名规范 [url]http://www.ruiwant.com/cakephp-conventions.html[/url]
常用的8个.htaccess代码 [url]http://help.vit.cn/item/78.html[/url]
CakePHP应用开发 第七章 视图:创建用户界面(2) [url]http://www.21haolou.com/articles/show/19[/url]
CakePHP2 Request请求对象 [url]http://www.ruiwant.com/cakephp-request-object-tutorial-basic.html[/url]
cakephp 中的controller 变量 [url]http://www.phpgz.com/html/framework/kcakephp/20090724/758.html[/url]
CakePHP 2.x CookBook 中文版 第七章 模型 之 保存数据 [url]http://www.cnblogs.com/matchless/archive/2013/02/04/2891845.html[/url]
Model定义& 关联关系定义 [url]http://www.1x3x.net/cakephp/model.html[/url]
CakePHP控制器向视图传值总结 [url]http://blog.csdn.net/simengsiyu/article/details/12713671[/url]
CakePHP自动填充表单域模型 [url]http://www.91r.net/ask/6848429.html[/url]
CakePHP中Router的机制和使用方法 [url]http://flyer0126.iteye.com/blog/1013255[/url]
cakephp路由配置 [url]http://www.jiangkunlun.com/2010/12/cakephp%E8%B7%AF%E7%94%B1/[/url]
CakePHP你必须知道的21条技巧 [url]http://tech.idv2.com/2007/10/19/21-tips-you-must-know-about-cakephp/[/url]


[color=red][b]检索数据[/b][/color]
如前所述,模型层的角色之一是从多种类型的存储中获取数据。CakePHP 的模型类拥有一些 功能,能够帮助你搜索数据、把数据排序、分页以及过滤。模型中最常用的功能是 Model::find() 方法。
find
find(string $type = 'first', array $params = array())

find 方法是所有检索数据方法中的多功能机器。$type 参数值可以是 'all' 、 'first' 、 'count' 、 'list' 、'neighbors' 或 'threaded',或 者任何自定义查询类型。切记 $type 是大小写敏感的。使用大写字母(如 All )将 无法得到期望的结果。
$params 用于向各种类型的 find() 方法传递所有参数,默认有如下的键,都是可选的:
array(
'conditions' => array('Model.field' => $thisValue), //查询条件数组
'recursive' => 1, //整型
//字段名数组
'fields' => array('Model.field1', 'DISTINCT Model.field2'),
//定义排序的字符串或者数组
'order' => array('Model.created', 'Model.field3 DESC'),
'group' => array('Model.field'), //用来分组(*GROUP BY*)的字段
'limit' => n, //整型, 页长度
'page' => n, //整型,页下标,1开始
'offset' => n, //整型,起点位置,0开始
'callbacks' => true //其他值可以是 false, 'before', 'after'
)



[b][color=red]Controller[/color][/b] : [url]http://book.cakephp.org/2.0/en/controllers.html[/url]
[color=red][b]View[/b][/color] : [url]http://book.cakephp.org/2.0/en/views.html[/url]
[color=red][b]Model[/b][/color] : [url]http://book.cakephp.org/2.0/en/models.html[/url]

[color=red][b]修改模板后缀:[/b][/color]
CakePHP默认模板文件的后缀为ctp,
如果想修改此后缀,可以在/app/Controller/AppController.php文件中添加以下代码:
public $ext = '.html';


[color=red][b]1. 定义全局变量[/b][/color]:可以在app\webroot\index.php,使用define('CONTEXT_PATH', "/");方法来定义;

[color=red][b]2. 主键:$primaryKey[/b][/color],如果主键字段不为'id',COC无法发挥的时候,你可以通过该变量来指定主键字段名字。

[color=red][b]3. 创建新的布局[/b][/color]:创建任意数量的layout,只要把他们放在app/views/layouts目录,并且在你的controller action里边使用controller的$layout变量或者setLayout()方法来切换layout。
在方法里面使用:$this->layout = 'administrator';

[color=red][b]4. 模型的对象关联[/b][/color]:hasOne,hasMany,belongsTo,hasAndBelongsToMany。[url]http://lesorb.iteye.com/blog/736694[/url], Cakephp查询关联表的方法总结 [url]http://www.php1.cn/article/6759.html[/url]

[color=red][b]5. CakePHP中回调函数的使用[/b] [/color][url]http://www.cnblogs.com/mafeifan/archive/2013/08/21/3273097.html[/url]

[color=red][b]6. 帮助类[/b][/color]:HTML Helper,Ajax helper,Javascript helper,Number helper,Text helper,Time helper
echo $this->Html->image($coll["web_path"], array('alt' => 'CakePHP','width'=>'56','height'=>'56'));
echo $this->Html->link(
'Enter',
'/pages/home',
array('class' => 'button', 'target' => '_blank')
);
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array("alt" => "Brownies")),
array(
'controller' => 'recipes',
'action' => 'view',
'id' => 6,
'comments' => false
)
);//好像无效
echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array("alt" => "Brownies")),
"recipes/view/6",
array('escape' => false)
);
echo $this->Html->link(
$this->Html->image($coll["web_path"], array('alt' => 'CakePHP','width'=>'56','height'=>'56')),
array(
'controller' => 'recipes',
'action' => 'view',
'id' => 6,
'comments' => false
),
array('escape' => false)
);

[url]http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html[/url]


[color=red][b]7.Ajax方式接受传进Controller的参数[/b][/color]
[b]get[/b] :
[color=darkblue]$this->request->query['page']
[b]post[/b]:
[color=darkblue]$this-request->data['Post']['title'];[/color],没测试成功。
[color=darkblue]$this->request->data["password"][/color],测试成功。
否则直接使用PHP自己的:$_GET,$_POST对象获取数据
其他:
A: [color=darkblue]$this->redirect(array('controller' => 'login', 'action' => 'index',"message"=>"错误信息."));[/color] 传信息进来,使用[color=darkblue]$this->request->params["named"]["message"][/color]读取
Ajax返回:
$php_json = json_encode($list);
$result = array(
'rows' => $list,
'page' => 1,
'total' => 2,
'records' => 1
);
return new CakeResponse(array('body' => json_encode($result), 'status' => 200));


[color=red][b]8. 保存数据:[/b][/color]
// 创建新记录: id 没有设置或设置为 null
$this->Recipe->create();
$this->Recipe->save($this->request->data);

// 更新记录: id 被设置为一个数字值
$this->Recipe->id = 2;
$this->Recipe->save($this->request->data);


//只更新某个字段
$this->User->id=$users["User"]["user_id"];
$this->User->saveField('login_error_num', 10);


[color=red][b]9.Controller使用其他Model[/b][/color]
var $uses = array("Accesse", "Site");

这样就可以直接在这个Controller使用$this->Site->?????了。

[color=red][b]10.Controller使用其他组件[/b][/color]
var $components = array('Session', 'Email');


[color=red][b]11按钮的样式[/b][/color]:由于From助手只提供submit的input类型,button类型的样式有点问题,所以换一种方式生成按钮:
echo $this->Form->submit('按钮',array("type"=>"button"));
,
这样虽然改变了按钮的类型是button,而不是submit,但因为按钮的外面被div包含,所以几个按钮在一起的时候,总是要换行,现在改变外面使用span来包含,就解决问题:
echo $this->Form->submit('保存',array("type"=>"button","name"=>"save","div"=>false,"before"=>"<span class='submit'>","after"=>"</span>"));

增加样式:
input[type=button] {
display: inline;
font-size: 110%;
width: auto;
}
form .submit input[type=button] {
background:#62af56;
background-image: -webkit-gradient(linear, left top, left bottom, from(#76BF6B), to(#3B8230));
background-image: -webkit-linear-gradient(top, #76BF6B, #3B8230);
background-image: -moz-linear-gradient(top, #76BF6B, #3B8230);
border-color: #2d6324;
color: #fff;
text-shadow: rgba(0, 0, 0, 0.5) 0px -1px 0px;
padding: 8px 10px;
}
form .submit input[type=button]:hover {
background: #5BA150;
}
input[type=button] {
font-weight:normal;
padding: 4px 8px;
background: #dcdcdc;
background-image: -webkit-gradient(linear, left top, left bottom, from(#fefefe), to(#dcdcdc));
background-image: -webkit-linear-gradient(top, #fefefe, #dcdcdc);
background-image: -moz-linear-gradient(top, #fefefe, #dcdcdc);
background-image: -ms-linear-gradient(top, #fefefe, #dcdcdc);
background-image: -o-linear-gradient(top, #fefefe, #dcdcdc);
background-image: linear-gradient(top, #fefefe, #dcdcdc);
color:#333;
border:1px solid #bbb;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
text-decoration: none;
text-shadow: #fff 0px 1px 0px;
min-width: 0;
-moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0px 1px 1px rgba(0, 0, 0, 0.2);
-webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0px 1px 1px rgba(0, 0, 0, 0.2);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0px 1px 1px rgba(0, 0, 0, 0.2);
-webkit-user-select: none;
user-select: none;
}


[color=red][b]11.引入js和css文件:[/b][/color]
echo $this->Html->css('cake.generic');
echo $this->Html->script("site/Admins/admin_site");


[color=red][b]12.输出图片[/b][/color],一些图片直接使用url,但是有不是存放在img路径下,那么会被要求经过controller,比如图片位置:
http://localhost/apps/EnterpriseSite/storage_file/storage_file_tmp/p189gt0ekfm3f17
t01bvsolgjsu4.png,其中EnterpriseSite是app的根目录,那
App::import("Vendor", "EnterpriseSiteUtils");
class StorageFileController extends AppController{
var $helpers = array('Helper');

public function storage_file_tmp() {
//$this->Html->image(EnterpriseSiteUtils::getContextPath()."/storage_file/storage_file_tmp/p189gt0ekfm3f17t01bvsolgjsu4.png");
//$filename= EnterpriseSiteUtils::getContextPath()."/storage_file/storage_file_tmp/p189gt0ekfm3f17t01bvsolgjsu4.png";
$filename= WWW_ROOT."../../storage_file/storage_file_tmp/p189gt0ekfm3f17t01bvsolgjsu4.png";
if (!file_exists($filename)) {
throw RuntimeException("File $filename not found");
}
header("Content-Type: png");
header("Content-Length: ".filesize($filename));
readfile($filename);
}
}
,就可以直接使用url访问存放在非Img目录下面的图片来显示了。
当然,也可以配置.htaccess文件(见配置的文章)

[color=red][color=darkblue][b]13 自定义主题[/b][/color] [/color][url]http://www.cnblogs.com/matchless/archive/2013/01/31/2886519.html[/url]
详细参考链接,经过实践,得到几个注意的地方
1.最好使用助手,否则问题会碰到一大堆,因为助手支持了主题。
2.主题的名称第一个字母最好大写,否则到网上的服务器可能出问题。

[color=red][b]13. 改变controller默认视图[/b][/color]
在controller里面定义一个变量:
public $name = 'User'; //指定这个controller使用的是User视图


[color=red][b]14. URL[/b][/color]
A:
$link = $this->Html->link($title, $url, array('class' => 'edit'));
,创建一个以a为标签的连接.
B:
echo $this->Html->url(array(
"controller" => "posts",
"action" => "view",
"bar"
));


[color=red][b]15. 引入第三方库[/b][/color]
App::import("Vendor", "EnterpriseSiteUtils");  


[b][color=red]16. 重定向和跳转[/color][/b]
A: 重定向:
$this->redirect(array('controller' => 'login', 'action' => 'index'));


[color=red][b]17. SESSION[/b][/color]
$this->Session->write('Person.eyeColor', 'Green');//设定session, 注意:这里的key有点,那么保存到session的时候是以对象的方式保存
$green = $this->Session->read('Person.eyeColor');

检查是否被设定: SessionComponent::check($name)
删除session数据: SessionComponent::delete($name)
销毁session: SessionComponent::destroy()

[color=red][b]18. 手动创建Model和执行自定义sql[/b][/color]
$Menu = new Model("menu_id","menus");
$list = $Menu->query("select * FROM wfms_menus where company_id=1");


[color=red][b]19. 事务处理[/b][/color] [url]http://www.cakephp.cn/forum.php?mod=viewthread&tid=620[/url]
[url]http://www.cnblogs.com/anhelida/p/3987339.html[/url]
在ocntroller使用事务例子:
[url]http://stackoverflow.com/questions/18032532/cake-php-2-3-x-transaction[/url]
Model:Product,Price,Property增加下面三个方法
function begin() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db->begin($this);
}

function commit() {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db->commit($this);
}
function rollback()
{
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$db->rollback($this);
}

Controller:
$datasource = $this->Product->getDataSource();
try{
$datasource->begin();
if(!$this->Product->save($data)
throw new Exception();

if(!$this->Price->save($data_one)
throw new Exception();

if(!$this->Property->save($my_data)
throw new Exception();

$datasource->commit();
} catch(Exception $e) {
$datasource->rollback();
}


[color=red][b]20. 临时文件夹权限[/b][/color]: Could not apply permission mask
[url]http://stackoverflow.com/questions/20189251/after-setting-cache-mask-at-777-the-permissions-generated-on-the-cache-file-is[/url]
这里可能是解决方式:[url]http://stackoverflow.com/questions/12718331/ocasional-error-warning-when-cakephp-is-writing-to-the-cache[/url],[color=red]同时给app/tmp路径777权限。[/color]


[b]cakephp修改时区[/b] [url]http://blog.csdn.net/php_byrnes/article/details/37507149[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值