This is my first Yii project and so wanted someone to do a code review. Any suggestion welcome
Controller
Model
Controller
<?php class VerifyUsersController extends Controller { /** * @param string $param key to be checked * @return boolean $YiiResult weather data was saved or not. */ public function actionAccount($param = null) { $message = array( 'type' =>'error', 'message' =>'Invalid URL.'); if($param != null){ $VerifyUser = new VerifyUser(); if($VerifyUser->readByKey($param) !== false and $VerifyUser->status == 'active'){ $User = new User(); $User->id = $VerifyUser->user_id; if(($User->get() and $User->status == 'active') || ($User->status == 'new' and $User->changeStatus('active'))) { $VerifyUser->changeStatus('deleted'); $message = array( 'type' =>'success', 'message' =>'Account verified.'); } else{ $message = array( 'type' =>'error', 'message' =>'Verification failed'); } } } $model = new LoginForm; $this->render('//site/login',array('message'=>$message, 'model' => $model)); } public function newUser($user){ $model = new VerifyUser; $model->attributes = array( 'user_id' => $user->id, 'data' => $user->key, 'data' => $user->email, 'type' => 'account' ); return $model->create(); } } ?>
Model
<?php class VerifyUser extends CActiveRecord { /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return VerifyUser 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 '{{verify_users}}'; } /** * @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 array( array('id, user_id, key, type', 'required'), array('id, user_id', 'numerical', 'integerOnly'=>true), array('key', 'length', 'max'=>200), array('data', 'length', 'max'=>255), array('type', 'length', 'max'=>8), array('created', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, user_id, key, data, type, created', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'id' => 'ID', 'user_id' => 'User', 'key' => 'Key', 'data' => 'Data', 'type' => 'Type', 'created' => 'Created', ); } /** * Genetate a key. * @return boolean $YiiResult weather data was saved or not. */ public function create(){ $YiiResult = Yii::app()->db->createCommand() ->insert('{{verify_users}}', array( 'user_id' => $this->user_id, 'key' => $this->key, 'data' => $this->data, 'type' => $this->type )); return $YiiResult; } /** * Read a key. * @param string $key key to be checked * @return mixed $YiiResult result or false. */ public function readByKey($key){ $YiiResult = Yii::app()->db->createCommand() ->select('*') ->from($this->tableName()) ->where('`key`=:key', array(':key'=>$key)) ->queryRow(); if($YiiResult == true){ $this->setAttributes($YiiResult, false); } return $YiiResult; } /** * Change Key Status. * @param string $status user status * @return boolean $YiiResult result or false. */ public function changeStatus($status){ if($this->id == false){ return false; } $YiiResult = Yii::app()->db->createCommand() ->update($this->tableName(),array( 'status'=>$status ), 'id=:id', array(':id'=>$this->id)); return ($YiiResult == 0) ? false : true ; } } ?>