July, 2010Archive for

Thinkphp中表关联HAS_ONE和BELONGS_TO的区别

HAS_ONE(或HAS_MANY):外键在子关联对象中 例子: 父关联对象表: product_id product_name 子关联对象表: image_id image_name product_id 外键是:product_id BELONGS_TO:外键在你父联对象中 父关联对象表: product_id class_id product_name 子关联对象表: class_id class_name 外键是:class_id

ThinkPHP中forward和redirect的区别

ThinkPHP中forward和redirect的区别在那里呢? 首先看代码注释: forward: 执行某个Action操作(隐含跳转) 支持指定模块和延时执行 redirect: Action跳转(URL重定向) 支持指定模块和延时跳转 其次看代码组织,二者的行为是不同的: forward: redirect: forward是通过框架内部的ACTION和MODULE的调用,实现页面显示的是另一个你想要的内容。 redirect则是通过改变URL地址栏,直接跳转过去的。 上论坛搜索,发现老大给出了一些指点,其说明如下: 可以把forward看成是隐含跳转,rediret是直接跳转。 redirect是会改变url ,跳过去执行另外的操作 forward只是隐含执行一个操作方法,url本身并不会跳转(这个其实不是绝对的,因为隐含执行的操作可能会有一个重定向的过程) 例外论坛里有位兄弟已经给出了forward的用法:

How to disable/enable an element with jQuery

Sometimes you need to disable or enable some elements in your document and jQuery makes this task easy. All you have to do is to set disabled attribute to "disabled". Example: In order to enable any disabled element you need to set the disabled attribute to empty string or remove it entirely like in the code below.

Javascript Equivalent of PHP’s trim function

Trim function in PHP can remove whitespace from the beginning and end of a string. Javascript do not have this function, but is easy to made one.

Javascript Equivalent of PHP’s explode() and implode()

Do you know javascript has equivalent functions as php's explode() and implode(). Let's say we have a string: We want to break down each number into an array called $num_array. In PHP we could do this: The PHP implode function would produce the following: In Javascript, we can use split function. Then let's say we wanted to piece it back into the original string. We could just do this in php: $string = implode(':',$explode_array); In Javascript, we c...

ThinkPHP上传类库的Bug修复

最近开发网站用到了ThinkPHP 2.0的上传类库, 用来实现一个图片上传功能。 使用过程中发现,如果开启了子目录创建方式,则会出现一些问题: 1. 无法生成缩略图, 此问题的修复办法如下, 在 UploadFile.class.php 中找到 line#145 替换成为: 2. 返回参数中没有包含缩略图的文件名,原始的返回参数如下: savename 参数包含了子目录路径,保存缩略图路径到数据库的时候,还需要写额外的function替换出缩略图的存放路径。 我对此做了一些修改: 返回的参数中多了一个subpath用来存放子目录路径,savename不再包含子目录路径。 如果你的thumbPrefix是'thumb_', 现在你只需 我上传了修改后的UploadFile.class.php文件,你可以随意使用。如果你发现其他问题,欢迎留言讨论。

Mass Replace String use MySQL REPLACE function

Since i changed the path for a friend's blog, i need update the files and images path URL in all previous posts. The best way i found is use MySQL REPLACE function to replace string in hundreds of records. Only need 1 minute. Here is the SQL query to replace string in database table: SQL query for mass replace string in wordpress table

使用正则表达式进行ThinkPHP的自动验证

ThinkPHP的自动验证机制是为了进行表单数据验证,验证可以支持function、 callback、confirm、equal、unique和regex,这里要讲的是使用正则表达式进行验证。 一般我们见的比较多的是设置规则为require、email之类的,其实这些本身也是属于正则表达式验证方式,只是系统内置定义了一些常用的正则表达式而已。这些内置的正则表达式的定义可以参考model类的regex方法,内置支持的正则定义包括: require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字、zip 邮编、integer 整数、double 浮点数、english 英文字母,但是并不局限于这些正则规则的,我们完全可以直接在验证规则里面使用正则表达式进行定义,这样我们可以凭借强大的正则表达式来进行表单字段验证,例如: array(‘name’,'/^[a-z]\w{3,}$/i’,'名字不符合要求!’); array(‘password’,'/^[a-z]\w{6,30}$/i’,'密码不符合要求!’); array(‘ac...

ThinkPHP的动态数据查询方法

ThinkPHP提供了数据的动态查询方法,可以简化你的查询代码,例如: $User->where(‘name=”ThinkPHP”‘)->find(); 可以简化为: $User->getByName(‘ThinkPHP’); $User->where(‘email=”thinkphp@qq.com”‘)->find(); 可以简化为: $User->getByEmail(‘thinkphp@qq.com’); getBy**** 方法里面的**** 会转换成小写的字段名,如果字段不存在,就会出错。 如果你的字段名是user_id ,那么查询方法应该写成: $User->getByUserId(5); UserId 会被解析成为数据库的user_id字段,这点需要注意,以免引起不必要的麻烦。 目前尚不支持,对多个字段的动态查询。

避免ThinkPHP URL目录过深的技巧

按照ThinkPHP的默认URL模式,通常是: http://serverName/模块名/操作名/变量1/值1/变量2/值2… 很多人担心这样的URL会导致目录层次过深,而且由于这样的URL改变了当前的相对路径,所以如果不注意写法,经常会导致JS和CSS加载不到。问题就在于这个”/”,这两个问题都可以通过一个小技巧解决,而且不影响你的开发,只需要在项目配置文件中设置 ‘URL_PATHINFO_DEPR’=>’-', 这个配置默认值是”/” 我们更改为”-” 配置修改以后,上面的URL地址就可以变成: http://serverName/模块名-操作名-变量1-值1-变量2-值2… 不过要注意的是,模板里面的链接地址最好是用U方法动态生成的,而不是固定写死的,否则模板会有一定的修改工作。