Posts Tagged with create

Displaying 1-8 of 8 results.
Resolved: Yii2 Migration multiple insert
posted by admin on February 20, 2017
I want to insert multiple rows in migration

$this->insert //insert once

I want multipleRead more
Resolved: Yii cactiverecord not saving model
posted by admin on September 6, 2016
My model:


* This is the model class for table "tbl_test".
* The followings are the available columns in table 'tbl_test':
* @property integer $id
* @property string $name
* @property integer $status_id
* The followings are the available model relations:
* @property TblQuestion[] $tblQuestions
* @property TblStatus $status
class Test extends CActiveRecord
public $status_list;
public $status_listdata;
* @return string the associated database table name
public function tableName()
return 'tbl_test';

* @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('name, status_id', 'required'),
//array('status_id', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>255),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, name, status_id', '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(
'question' => array(self::HAS_MANY, 'Question', 'test_id'),
'status' => array(self::BELONGS_TO, 'Status', 'status_id'),

* @return array customized attribute labels (name=>label)
public function attributeLabels()
return array(
'id' => 'ID',
'name' => 'Name',
'status_id' => 'Status',

* Retrieves a list of models based on the current search/filter conditions.
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
public function search()
// @todo Please modify the following code to remove attributes that should not be searched.

$criteria=new CDbCriteria;


return new CActiveDataProvider($this, array(

* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Test the static model class
public static function model($className=__CLASS__)
return parent::model($className);

public function __construct() {
$this->status_list = Status::model()->findAll();
$this->status_listdata = CHtml::listData($this->status_list,'id','name');

My Controller actionCreate:

public function actionCreate()
$model=new Test;

}// Uncomment the following line if AJAX validation is needed



Read more
Wiki: Yii on update, detect if a specific AR property has been changed on beforeSave()wiki
posted by admin on April 23, 2016
You need to store the old attributes in a local property in the AR class so that you can compare the current attributes to those old ones at any time.

Step 1. Add a new property to the AR class:

// Stores old attributes on afterFind() so we can compare
// against them before/after save
protected $oldAttributes;

Step 2. Override Yii's afterFind() and store the original attributes immediately after they are retrieved.

public function afterFind(){
$this->oldAttributes = $this->attributes;
return parent::afterFind();

Step 3. Compare the old and new attributes in beforeSave/afterSave or anywhere else you like inside the AR class. In the example below we are checking if the property called 'level' is changed.

public function beforeSave()
if(isset($this->oldAttributes['level']) && $this->level != $this->oldAttributes['level']){

// The attribute is changed. Do something here...


return parent::beforeSave();
Read more
Resolved: Yii sum criteria select condition
posted by admin on April 15, 2016
I try to count my views sum count, by this way

$criteria=new CDbCriteria;
$criteria->select='SUM(visits) as SUMvisits';
$sBalance = Views::model()->find($criteria)->getAttribute('SUMvisits');

Something like this, but it is not workingRead more
Resolved: Yii file save error Directory must be writable
posted by admin on April 11, 2016
I have form, and I create directory by mkdir

mkdir($mydirectory, 0777, true);

And got an error during save my file, which I try to upload from my _form.php

Directory must be writable
Read more
Resolved: Yii check action create and update in view _form
posted by admin on February 2, 2016
I want to check is action create or update and write specific view form row

<?php if (action check syntax here like isUpdate) : ?>
<?php echo $form->labelEx($model,'newpassword'); ?>
<?php echo $form->passwordField($model,'newpassword',array('size'=>55,'maxlength'=>255)); ?>
<?php echo $form->error($model,'newpassword'); ?>
<?php endif; ?>
Read more
Resolved: Yii form dropDownList create and update support example with empty first option
posted by admin on January 22, 2016
I have form on yii. It is news form. I want to change news category by select box and in update form have selected option.Read more
Resolved: CWidget should have a createUrl implementation?
posted by admin on December 26, 2015
After some research in the source and reading the api I'd like to discuss if the CWidget class shouldn't have a createUrl() implementation like CController but appending to the action automatically the $prefixUrl. I think it could be very useful

What do you think?Read more