<?php class User extends CActiveRecord { //// public $name; public $first_name; public $description; //// ... public function attributeLabels() { return array( 'id' => 'ID', 'login' => 'Login', 'password' => 'Password', 'status' => 'Status', //// 'name'=>'', 'first_name'=>'', 'description'=>'' //// ); }
//// protected function afterSave() { parent::afterSave(); if($this->isNewRecord){ // , // $user_profile = new UserProfile; $user_profile->user_id = $this->id; $user_profile->name = $this->name; $user_profile->first_name = $this->first_name; $user_profile->description = $this->description; $user_profile->save(); } else { // UserProfile::model()->updateAll(array( 'user_id' =>$this->id, 'name' => $this->name, 'first_name'=>$this->first_name, 'description'=>$this->description ), 'user_id=:user_id', array(':user_id'=> $this->id)); } } ////
public $name; public $first_name; public $description;
'name'=>'', 'first_name'=>'', 'description'=>''
if($this->isNewRecord){
UserProfile::model()->updateAll(array( 'user_id' =>$this->id, 'name' => $this->name, 'first_name'=>$this->first_name, 'description'=>$this->description ), 'user_id=:user_id', array(':user_id'=> $this->id));
public function actionCreate() { $model=new User; if(isset($_POST['User'])) { $model->attributes=$_POST['User']; //// $model->name = $_POST['User']['name']; $model->first_name = $_POST['User']['first_name']; $model->description = $_POST['User']['description']; //// if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('create',array( 'model'=>$model, )); }
public function actionUpdate($id) { $model=$this->loadModel($id); if(isset($_POST['User'])) { $model->attributes=$_POST['User']; //// $model->name = $_POST['User']['name']; $model->first_name = $_POST['User']['first_name']; $model->description = $_POST['User']['description']; //// if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('update',array( 'model'=>$model, )); }
public function loadModel($id) { $model=User::model()->findByPk($id); //// $modelprofile=UserProfile::model()->find('user_id=:user_id', array(':user_id'=> $id)); $model->name = $modelprofile->name; $model->first_name = $modelprofile->first_name; $model->description = $modelprofile->description; //// if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; }
<?php <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'user-form', 'enableAjaxValidation'=>false, )); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'login'); ?> <?php echo $form->textField($model,'login',array('size'=>45,'maxlength'=>45)); ?> <?php echo $form->error($model,'login'); ?> </div> ..... //// <div class="row"> <?php echo $form->labelEx($model,'name'); ?> <?php echo $form->textField($model,'name',array('size'=>45,'maxlength'=>45)); ?> <?php echo $form->error($model,'name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'first_name'); ?> <?php echo $form->textField($model,'first_name',array('size'=>45,'maxlength'=>45)); ?> <?php echo $form->error($model,'first_name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'description'); ?> <?php echo $form->textField($model,'description',array('size'=>45,'maxlength'=>45)); ?> <?php echo $form->error($model,'description'); ?> </div> //// <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->
<?php .... <?php $this->widget('zii.widgets.CDetailView', array( 'data'=>$model, 'attributes'=>array( 'id', 'login', 'password', 'status', //// 'name', 'first_name', 'description' //// ), )); ?>
<?php /* @var $this UserController */ /* @var $data User */ ?> <div class="view"> <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b> <?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?> <br /> ... //// <b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b> <?php echo CHtml::encode($data->name); ?> <br /> <b><?php echo CHtml::encode($data->getAttributeLabel('first_name')); ?>:</b> <?php echo CHtml::encode($data->first_name); ?> <br /> <b><?php echo CHtml::encode($data->getAttributeLabel('descrption')); ?>:</b> <?php echo CHtml::encode($data->descrption); ?> <br /> //// </div>
Source: https://habr.com/ru/post/200478/
All Articles