public function actionProfile() { YiiBase::beginProfile(1); Yii::app()->user->setFlash('infomsg', "You now editing profile"); $model = TblProfile::model()->with('photos')->findByAttributes(array('user_id' => Yii::app()->user->getId())); if (!$model) { $model = new TblProfile(); } $model->setScenario('create'); MyClass::dump($model); $img_add = new TblProfilePhotos(); // uncomment the following code to enable ajax-based validation if (isset($_POST['ajax']) && $_POST['ajax'] === 'tbl-profile-profile-form') { $model->setScenario('ajax'); echo CActiveForm::validate($model); Yii::app()->end(); } elseif (isset($_POST['TblProfile'])) { //MyClass::dump($_FILES); exit(); $model->attributes = $_POST['TblProfile']; $model->setAttribute('user_id', Yii::app()->user->getId()); if ($model->validate()) { // form inputs are valid, do something here $model->save(); } if ($_FILES['images']) { $images = CUploadedFile::getInstancesByName('images'); foreach ($images as $img => $pic) { if ($pic->saveAs(Yii::getPathOfAlias('webroot') . '/assets/files/' . $pic->name)) { $img_add = new TblProfilePhotos(); $img_add->setAttribute('image', $pic); $img_add->setAttribute('name', $pic->name); //it might be $img_add->name for you, filename is just what I chose to call it in my model $img_add->setAttribute('profile_id', $model->id); // this links your picture model to the main model (like your user, or profile model) if ($img_add->validate()) { $img_add->save(); // DONE } else { break; } } } /* вариант напрямик $builder=Yii::app()->db->schema->commandBuilder; $command=$builder->createMultipleInsertCommand('tbl_user_photos', array_map(function($arr){ return array('name' => $arr, 'profile_id' => $model->attributes->id); }, $_FILES['TblProfile']['name']['photos']) ); $command->execute(); */ } } YiiBase::endProfile(1); $this->render('profile', array('model' => $model, 'photosModel' => isset($img_add) ? $img_add : null)); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return TblProfile the loaded model * @throws CHttpException */ public function loadModel($id) { $model = TblProfile::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
$form = $this->beginWidget('CActiveForm', array('id' => 'tbl-profile-profile-form', 'action' => CHtml::normalizeUrl(array('site/profile')), 'enableAjaxValidation' => true, 'clientOptions' => array('validateOnSubmit' => true), 'htmlOptions' => array('enctype' => 'multipart/form-data'))); ?> <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, 'gender'); ?> <?php //echo $form->dropDownList($model, 'gender', TblProfile::model()->getGenderOptions()); echo $form->checkBoxList($model, 'gender', TblProfile::model()->getGenderOptions()); ?> <?php echo $form->error($model, 'gender'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'website'); ?> <?php echo $form->urlField($model, 'website'); ?> <?php echo $form->error($model, 'website');