最新发布 RSS Toggle Comment Threads | 键盘快捷键
-
admin
-
admin
-
admin
-
admin
cakephp中的LEFT JOIN实现
$this->Recipe->bindModel(array('hasAndBelongsToMany' => array('Tag' => array('conditions'=>array('Tag.name'=>'Dessert')))));$this->Recipe->find('all');
上面是cakephp手册中的一段不好的示范,但是这让我想起是否SQL中from子句的LEFT JOIN 可以用是bindModel实现。
http://book.cakephp.org/view/86/creating-and-destroying-associations-on-the-fly
-
admin
关于cakephp的model部分的Associations问题
假设有一个model叫Company,另一个叫Member。
Company和Member是1:n的关系。
如果Company中加了hasmany的关系。
那执行$Company->find()的时候,是不是会把所有的member数据也一次取出?
如果是,这样必然导致性能低下,甚至站点瘫痪。
参看文章:http://book.cakephp.org/complete/66/Models#Associations-Linking-Models-Together-78
以下摘自cakephp手册
- hasone
- Once this association has been defined, find operations on the User model will also fetch a related Profile record if it exists
- belongsto
- Once this association has been defined, find operations on the Profile model will also fetch a related User record if it exists:
- hasmany
- Once this association has been defined, find operations on the User model will also fetch related Comment records if they exist:
- 但是提供了’limit‘, ‘offset‘, ‘finderQuery‘,来让你自己决定如何取many端
- hasAndBelongsToMany(HABTM)
- Once this association has been defined, find operations on the Recipe model will also fetch related Tag records if they exist:
结论:
- 如上所示,find操作会取出所有定义了的related的Model数据,需要谨慎设置 hasmany和habtm。
- 根据mannual来看,cakephp在执行find的时候是不会递归取关系数据的。比如取company的时候会取出members数据,但是不会递归去除member的company数据。
重要补充
- hasone
-
admin
cakephp学习要点
- 命名规则
- class名字,class文件名,数据库表名字的命名规则
- 配置设定
- debug 输出信息级别设定
- session 的相关设定,可以设定cookie的名称,可以设定session存储方式,可以设定安全级别(当为high时,每个request时候,存在cookie中的session ID都会改变,防止被监听)
- acl 可以设定访问列表
- URL 与 Controller的Action的mapping规则
- 默认匹配
- 带名字的参数匹配
- In CakePHP, controllers are named after the model they handle, in plural form. eg:The Recipe model is handled by the RecipesController, the Product model is handled by the ProductsController, and so on.
- 可以静态化页面 HTML,route 会自动过滤扩展名
- 命名规则中的单复数映射关系
- Bootstrap的使用规则即 在什么情况下推荐使用bootstrap
- persistModel
- Redirect & Flash
- Callbacks
- requestAction
- if you are using named or passed parameters, you must put them in a second array and wrap them with the correct key. This is because requestAction only merges the named args array into the Controller::params member array and does not place the named args in the key ‘named’.
- 关于Component
- Components are packages of logic that are shared between controllers.
- You might also want to utilize other components inside a custom component.
- To access/use a model in a component is not generally recommended.
- 依据Model规则创建数据库
- title & name
- created & modified
- 从数据库返回树结构数据
- 数据安全处理
- Model中的create(array $data = array())方法
- This method resets the model state for saving new information.
- Model中如果要使用Transation
- For transactions to work correctly in MySQL your tables must use InnoDB engine. Remember that MyISAM tables do not support transactions.
- counterCache
- Model之间的关系
- Remember that associations are defined ‘one way’. If you define User hasMany Recipe that has no effect on the Recipe Model. You need to define Recipe belongsTo User to be able to access the User model from your Recipe model
- bindModel(creating-and-destroying-associations-on-the-fly)
- Removing or adding associations using bind- and unbindModel() only works for the next model operation only unless the second parameter has been set to false. If the second parameter has been set to false, the bind remains in place for the remainder of the request.
- Multiple relations to the same model
- Model的userdbconfig属性可以用来做负载均衡
- find操作时递归取related model的层数设定
- validate
- 给Model添加自己方法或属性时的原则
- Any operation that handles the saving and fetching of data is best housed in your model classes. This concept is often referred to as the fat model.
- 如何写自己的数据源
- 网络上的资源也可以作成数据源,如手册中的例子,twitter的api的读和写。
- 在View的element里面使用requestAction示范
- 如何在controller中判断是否来自requestAction
- 如何缓存element及缓存element的不同版本
- media view
- Media views allow you to send binary files to the user. For example, you may wish to have a directory of files outside of the webroot to prevent users from direct linking them. You can use the Media view to pull the file from a special folder within /app/, allowing you to perform authentication before delivering the file to the user.
- 命名规则
-
admin
-
admin
mysql的LEFT JOIN的使用时的注意事项和优化
1. SELECT 语句的执行过程
2. LEFT JOIN 中的ON中的条件和 WHERE中的条件的区别
3. LEFT JOIN的右表要尽可能的小,如果表太大要用子查询把表缩小SQL书写标准:
SELECT部分做最终结果的计算,不执行数据筛选工作
FROM部分负责数据表的连接,不负责数据筛选,如果LEFT JOIN时右表太大,用子查询的where缩小表的大小,ON的条件不能较小该表的大小,造成效率地下
WHERE部分负责数据筛选,而表之间的链接不应该放在这个部分 -
admin
Mysql的date函数在不同版本下执行结果不一样
select if(date(a) = '2009-09-09', b,0) as res
from (
select null as a,1 as b union
select '2009-09-09' as a, 2 as b union
select null as a, 3 as b
) t;
Linux下mysql的执行结果截图:
mysql版本:mysql Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (i686) using readline 5.0

mysql版本:mysql Ver 14.12 Distrib 5.0.18, for pc-linux-gnu (i686) using readline 5.0

Windows下mysql的执行结果截图:
mysql版本:mysql Ver 14.12 Distrib 5.0.67, for Win32 (ia32)

-
admin
PHP如何让页面启动的脚本,在页面退出后,脚本继续执行
事例说明
1. A:
运行B
exit
2. B:
做事情
3. 执行A后,A退出,B继续执行方案说明
方案一
在LINUX/UNIX下:
exec(“commmand &”);
exec(“perl file.pl &”);
符号”&”表示把程序放在后台运行,而调用者继续。
WIN9X/NT下,要用start命令:
exec(“start /xxxxx file.exe”);
WINNT的start命令比WIN9X的多很多选项。方案二
使用fscok实现异步调用PHP


