Tuesday, July 12, 2016

Yii - Multiple Insert Command

New method CDbCommandBuilder::createMultipleInsertCommand()

There's now CDbCommandBuilder::createMultipleInsertCommand() to support insertion of multiple records in a single query:

$builder=Yii::app()->db->schema->commandBuilder;

$command=$builder->createMultipleInsertCommand('tbl_post', array(
  array('title' => 'record 1', 'text' => 'text1'),
  array('title' => 'record 2', 'text' => 'text2'),
));

$command->execute();

Example:

 Use Like this....

 $alertStatus[] = array(
                        'db_field_name1' => $value1,
                        'db_field_name1' => $value2,
                        'created_on' => new CDbExpression('NOW()'),
                        'modified_on' => new CDbExpression('NOW()')
                    );

 $connection = Yii::app()->db->getSchema()->getCommandBuilder();
 $command   = $connection->createMultipleInsertCommand('table_name', $alertStatus);
$command->execute();

Friday, July 08, 2016

Yii - Give attribute values to each option in dropDownList

How can we give some attribute values to each option. For Example I need to build a dropdownlist looks like below.

<select>
    <option value="a" myAttribute="xyz">Calendar</option>
    <option value="n" myAttribute="PQR">Shopping Cart</option>
    <option value="c" myAttribute="ABC">CD</option>
    <option value="d" myAttribute="HMN">Email</option>  
</select>

values and myAttributes's value are coming from table. How can I implement this by altering the following?


Yeah have a solution to implement..Here the below solution.

In View:

<?php
     echo $form->dropDownList($model,'media_ids',
               'data' => CHtml::listData(Media::model()->findAll(), 'id', 'name'),
                array(
                     'options' =>  CHtml::listData(Media::model()->findAll(), 'id', 'option_array')
                     )
     );
?>

In Model:

public function getOption_array() {
        return array('myAttribute' => $this->media_title);
    }


Cheers !!!