ThinkPHP示例之 字段映射

要使用模型的自动创建create方法的话,ThinkPHP的表单名称就是数据表的字段名称,如果担心这样不够安全,可以定义字段映射,来隐藏实际的数据表字段名称。

下面的例子就运用了字段映射定义。请注意看表单名称和字段的对应关系。

标题:
邮箱:
内容:
验证码: 输入对应的数字

示例源码

控制器IndexAction类

<?php 
class IndexAction extends Action{
    // 首页
    public function index(){
        $Form    = D("Form");
        $list    =    $Form->findAll();
        $this->assign('list',$list);
        $this->display();
    }
    // 处理表单数据
    public function insert() {
        $Form    =    D("Form");
        if($Form->create()) {
            $Form->add();
            $this->redirect();
        }else{
            header("Content-Type:text/html; charset=utf-8");
            exit($Form->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');
        }
    }

    // 生成验证码
    public function verify() {
        import("ORG.Util.Image");
           Image::buildImageVerify(); 
    }
} 
?>

模型 FormModel 类

<?php 
class FormModel extends Model {
    protected $_map    =    array(
            'name'=>'title',
            'mail'=>'email',
            'remark'=>'content',
        );
    // 自动验证设置
    protected $_validate     =     array(
        array('name','require','标题必须!'),
        array('mail','email','邮箱格式错误!',2),
        array('remark','require','内容必须'),
        array('verify','require','验证码必须!'),
        array('verify','CheckVerify','验证码错误',0,'callback'),
        );

    // 自动填充设置
    protected $_auto     =     array(
        array('status','1','ADD'),
        array('create_time','time','ADD','function'),
        );

    public function CheckVerify() {
        return md5($_POST['verify']) == $_SESSION['verify'];
    }

}
?>
affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “ThinkPHP示例之 字段映射”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>