Yii1: validation examples
Match
['request_column ' , 'match ' , 'pattern ' => '/[a-zA-Z0-9]/i ' , 'message ' => 'Допускаются только латинские символы и цифры ' ],
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 );
}
}
$ 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 ));
'columns ' => [
['class ' => 'ClearFiltersColumn ' ],
'filter ' =>
CHtml ::activeDropDownList (
$ dataProvider ->getSearchModel (),
'brand_is_mapped ' ,
OzonBrandSearch ::getBrandIsMappedStatuses ()
),
// 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 ;
}
}
Yii ::app ()->clientScript ->registerScriptFile (
Yii ::app ()->assetManager ->publish (__DIR__ . '/../assets/saveChatComment.js ' ),
CClientScript ::POS_END
);