Yii学习笔记:实现相似于ThinkPHP的模型字段映射,字段别名

在TP中,咱们只要在模型类中定义一个 php


protected $_map = array(
        'name' =>'username', // 把表单中name映射到数据表的username字段
        'mail'  =>'email', // 把表单中的mail映射到数据表的email字段
    );



这样,咱们前端模板中使用 <input type=text name="name"/>


在模型收集表单数据时会自动将值同时映射到username字段上。 前端

好处是避免数据库字段直接暴露。 web

但很遗憾,我在学习Yii过程当中没有找到相似的机制,而Yii的类库不少,我又不敢贸然本身扩展,总以为在某个角落里有某个类可能已经提供了解决方案,怕本身作重复无心义之举。 数据库

通过一系列的思想斗争,楼主仍是决定本身动手啦。。 post

方法很简单,请先看代码: 学习


<?php
class CustomModel extends CActiveRecord {
    //定义所须要收集的属性
    public $username;
    public $email;
    public $passwd;
    public $password;
    public $password1;
    
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
 
    public function tableName()
    {
        return '{{custom}}';
    }
    
    public function rules(){
        return array(
            array("username,password,email,password1","safe")
        );
    }
    //字段映射配置
    protected $_alias_ = array(
        "passwd" => "password"
    );
    //经过引用传递处理映射
    protected function afterConstruct(){
        parent::afterConstruct();
        //字段映射处理
        if(!empty($this->_alias_)){
            foreach($this->_alias_ as $a => $b){
                if(property_exists($this,$a) && property_exists($this,$b)){
                    $this->$a = &$this->$b;
                }
            }
        }
    }
}



模板:



<?php defined("APP_NAME") or exit;?>
<form action="" method="post">
    <table>
        <tr>
            <th>用户名:</th>
            <td><input type="text" name="username" placeholder="请设置您的帐号名" value=""/></td>
        </tr>
        <tr>
            <th>邮箱:</th>
            <td><input type="email" name="email" placeholder="example@website" value=""/></td>
        </tr>
        <tr>
            <th>密码:</th>
            <td><input type="password" name="password" value=""/></td>
        </tr>
        <tr>
            <th>重复密码:</th>
            <td><input type="password" name="password1" value=""/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="提交"/></td>
        </tr>
    </table>
</form>



能够发现,我在表单中使用了password字段名做为密码,可是我数据库中的字段是passwd,若是直接这样收集表单,是没法将密码数据入库的。


因此我在模型类中定义了1个方法: this

afterConstruct()
一个属性
protected $_alias_
咱们在$_alias_这个属性里面定义映射,好比我定义了 "passwd"=>"password"的映射。

afterConstruct()这个方法是在你实例化模型类以后执行的,能够当作在 $model = new CustomModel()的时候触发,这里面主要作了一个工做,就是检测$_alias_有没有定义映射,若是有的话,咱们使用php的引用传递特性来使两个字段指向同一个值。 spa

最后,为了能够全局使用,建议在components/目录下创建一个 CommonModel类,将afterConstruct()方法放在这个类里,其余模型继承这个类就能够了,这样只要在各自的模型中定义_alias_属性便可。 code

/////////基于事件的处理方法 component

<?php
class CommonAR extends CActiveRecord{
    function init(){
        $this->onAfterConstruct = array($this,"autoMap");
    }
    //字段映射配置
    protected $_alias_ = array(
    );
    
    function autoMap($event){
        //字段映射处理
        if(!empty($this->_alias_)){
            foreach($this->_alias_ as $a => $b){
                if(property_exists($this,$a) && property_exists($this,$b)){
                    $this->$a = &$this->$b;
                }
            }
        }
    }
}



假设在components/下建立一个 CommonAR类,咱们在init()方法中注册一个事件
$this->onAfterConstruct = array($this,"autoMap");
对应的事件方法就是类里面的autoMap()

而后建立具体模型的时候继承CommonAR类,定义$_alias_便可。

相关文章
相关标签/搜索