zhong21jia 2011-12-30 22:54:04 4479次浏览 0条回复 0 0 0

今天做开发,从官网下载了user模块和srbac模块!进行了整合!

最好不要去改动这两个模块,使用继承的方法来扩展自己想要的功能!

于是我在顶级的models里写了一个users模型继承了user模块下的user模型!

然后我想要得到一个数字转换为帐号名的方法,代码如下: Users模型:

<?php
class Users extends User {
    /**
    * @return array relational rules.
    */
    public function relations() {
        $relations = array(
            'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
            'team' => array(self::HAS_ONE, 'Team', 'parent'),
        );
        if (isset(Yii::app()->getModule('user')->relations))
            $relations = array_merge($relations, Yii::app()->getModule('user')->relations);
        return $relations;
    }
    public function getParentName($value) {
        return self::model()->findByPk($value)->attributes['username'];
    }
}

User模型:

<?php
class User extends CActiveRecord {
    const STATUS_NOACTIVE=0;
    const STATUS_ACTIVE=1;
    const STATUS_BANED=-1;
    /**
     * The followings are the available columns in table 'users':
     * @var integer $id
     * @var string $username
     * @var string $password
     * @var string $email
     * @var string $activkey
     * @var integer $createtime
     * @var integer $lastvisit
     * @var integer $superuser
     * @var integer $status
     */
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }
    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return Yii::app()->getModule('user')->tableUsers;
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.

        return ((Yii::app()->getModule('user')->isAdmin()) ? array(
                    #array('username, password, email', 'required'),
                    array('username', 'length', 'max' => 20, 'min' => 3, 'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
                    array('password', 'length', 'max' => 128, 'min' => 4, 'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
                    array('email', 'email'),
                    array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
                    array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
                    array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => UserModule::t("Incorrect symbols (A-z0-9).")),
                    array('status', 'in', 'range' => array(self::STATUS_NOACTIVE, self::STATUS_ACTIVE, self::STATUS_BANED)),
                    array('superuser', 'in', 'range' => array(0, 1)),
                    array('username, email, createtime, lastvisit, superuser, status', 'required'),
                    array('createtime, lastvisit, superuser, status', 'numerical', 'integerOnly' => true),
                        ) : ((Yii::app()->user->id == $this->id) ? array(
                            array('username, email', 'required'),
                            array('username', 'length', 'max' => 20, 'min' => 3, 'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
                            array('email', 'email'),
                            array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
                            array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => UserModule::t("Incorrect symbols (A-z0-9).")),
                            array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
                                ) : array()));
    }
    /**
     * @return array relational rules.
     */
    public function relations() {
        $relations = array(
            'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
        );
        if (isset(Yii::app()->getModule('user')->relations))
            $relations = array_merge($relations, Yii::app()->getModule('user')->relations);
        return $relations;
    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'username' => UserModule::t("username"),
            'password' => UserModule::t("password"),
            'verifyPassword' => UserModule::t("Retype Password"),
            'email' => UserModule::t("E-mail"),
            'verifyCode' => UserModule::t("Verification Code"),
            'id' => UserModule::t("Id"),
            'activkey' => UserModule::t("activation key"),
            'createtime' => UserModule::t("Registration date"),
            'lastvisit' => UserModule::t("Last visit"),
            'superuser' => UserModule::t("Superuser"),
            'status' => UserModule::t("Status"),
        );
    }

    public function scopes() {
        return array(
            'active' => array(
                'condition' => 'status=' . self::STATUS_ACTIVE,
            ),
            'notactvie' => array(
                'condition' => 'status=' . self::STATUS_NOACTIVE,
            ),
            'banned' => array(
                'condition' => 'status=' . self::STATUS_BANED,
            ),
            'superuser' => array(
                'condition' => 'superuser=1',
            ),
            'notsafe' => array(
                'select' => 'id, username, password, email, activkey, createtime, lastvisit, superuser, status',
            ),
        );
    }

    public function defaultScope() {
        return array(
            'select' => 'id, username, email, createtime, lastvisit, superuser, status',
        );
    }

    public static function itemAlias($type, $code=NULL) {
        $_items = array(
            'UserStatus' => array(
                self::STATUS_NOACTIVE => UserModule::t('Not active'),
                self::STATUS_ACTIVE => UserModule::t('Active'),
                self::STATUS_BANED => UserModule::t('Banned'),
            ),
            'AdminStatus' => array(
                '0' => UserModule::t('No'),
                '1' => UserModule::t('Yes'),
            ),
        );
        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

}

UserAdmin的视图:

<?php

$this->breadcrumbs = array(
    'Teams' => array('index'),
    'Manage',
);
$this->menu = array(
    array('label' => 'List Team', 'url' => array('index')),
    array('label' => 'Create Team', 'url' => array('create')),
);
?>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'team-grid',
    'dataProvider' => $model->search(),
    //'filter' => $model,
    'columns' => array(
        'user_id',
        array(
            'name' => 'parent_id',
            'value' => 'Users::model()->getParentName("$data->parent_id")',
        ),
        'parent_id',
        'type',
        array(
            'name' => 'type',
            'value' => 'Team::getTypeName("$data->type")',
        ),
        array(
            'header' => '查看',
            'class' => 'CButtonColumn',
            'buttons' => array(
                'chouse' => array(
                    'label' => '查看',
                    'url' => 'Yii::app()->controller->createUrl("/team/view",array("id" =>$data->user_id))',
                ),
            ),
            'template' => '{chouse} ',
        ),
    ),
));
?>

结果是死活不出来!
下面是报错信息:

CException
User and its behaviors do not have a method or closure named "getParentName".

难道Users 继承了user模型后不能进行扩展嘛? 还是我配置出错了? 求解!

    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册