Skip to content

Instantly share code, notes, and snippets.

@dozer111
Last active January 12, 2022 12:48
Show Gist options
  • Save dozer111/8ddcc40b482ea9d2f69804cd8dc57569 to your computer and use it in GitHub Desktop.
Save dozer111/8ddcc40b482ea9d2f69804cd8dc57569 to your computer and use it in GitHub Desktop.
Yii1 tricks

Actions


Migration



Yii1: validation examples

Match

['request_column', 'match', 'pattern' => '/[a-zA-Z0-9]/i', 'message' => 'Допускаются только латинские символы и цифры'],

Yii1: batchInsert

Yii::app()->db->getCommandBuilder()
->createMultipleInsertCommand(self::TABLENAME, $data)
->execute();

Yii1 cdbCommand addSelect

use CDbCommand;

trait CDbCommandTrait
{
    protected function addSelect(CDbCommand $command, string $select): void
    {
        $commandSelect = $command->getSelect();
        $commandSelect = explode(',',$commandSelect);
        $commandSelect[] = $select;

        $commandSelect = preg_replace('/`/','',$commandSelect);
        $commandSelect = array_map('trim',$commandSelect);
        
        $command->setSelect($commandSelect);
    }
}

Yii1 пример миграции

 $this->createTable('OzonOrder', [
            'ozon_id' => 'string NOT NULL',
            'crm_id' => 'bigint',
            'items' => 'JSON NOT NULL',
            'status' => 'string NOT NULL',
            'customer_id' => 'bigint NOT NULL',
            'delivery_address' => 'varchar(500) NOT NULL',
            'delivery_addressee_name' => 'varchar(255) NOT NULL',
            'delivery_addressee_contact' => 'varchar(255) NOT NULL',
            'shipment_date' => 'varchar(100) NOT NULL',
            'PRIMARY KEY (ozon_id)',
        ],
            'Engine=InnoDB DEFAULT CHARSET=utf8'
        );

Yii1 return file from action

Yii::app()->request->sendFile(
            "ean_report.csv",
            file_get_contents('/web_root/backend/modules/yandexMarket/files/EanCodeRequired.csv'),
        );

Yii1 return JSON from action

Y::endJson(['status' => 'ok']);

Yii1 set model attributes in controller

$attributes = Yii::app()->getRequest()->getQuery(CHtml::modelName($model));
$attributes = Yii::app()->getRequest()->getPost(CHtml::modelName($model));

Yii1 TbExtendedGridView

'columns' => [
            ['class' => 'ClearFiltersColumn'],
'ajaxUpdate' => false,
'filter' =>
                    CHtml::activeDropDownList(
                        $dataProvider->getSearchModel(),
                        'brand_is_mapped',
                        OzonBrandSearch::getBrandIsMappedStatuses()
                    ),

Yii1 AR

Yii1 AR boolean

// migration
$this->addColumn('VendorPrice', 'marked', 'tinyint(1) NOT NULL DEFAULT 0');


// model
['marked', 'boolean', 'allowEmpty' => false],
['marked', 'default', 'value' => 0],

Yii1 migration on front db

public function __construct()
{
    $this->dbConnection = Yii::app()->frontendDb;
}

Yii1 worker with transaction

public function run(): void
{
    $transaction = Yii::app()->getDb()->beginTransaction();

    try {
        $this->indexer->index();
        $transaction->commit();
    } catch (Throwable $e) {
        $transaction->rollback();
        throw $e;
    }
}

Yii1 register asset file

Yii::app()->clientScript->registerScriptFile(
    Yii::app()->assetManager->publish(__DIR__ . '/../assets/saveChatComment.js'), 
    CClientScript::POS_END
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment