Различия
Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
yii2:docs [2015/10/23 13:28] – [Submodule] mirocow | yii2:docs [2019/04/18 13:17] (текущий) – [Active Record] mirocow | ||
---|---|---|---|
Строка 1: | Строка 1: | ||
{{tag> | {{tag> | ||
- | ========= Документация по Yii 2 Framework | + | ====== Документация по Yii 2 Framework ====== |
- | + | ===== Configuration ===== | |
- | + | ||
- | ====== Configuration | + | |
<code php> | <code php> | ||
Строка 36: | Строка 34: | ||
]; | ]; | ||
</ | </ | ||
+ | ===== URL Management ===== | ||
- | ======== PHPUnit ======== | + | <code php> |
- | + | echo \Yii:: | |
- | добавить в конфигуратор композера composer.json | + | // /index.php/ |
- | <code> | + | echo \Yii::$app->urlManager-> |
- | { | + | // / |
- | " | + | echo \Yii:: |
- | " | + | // http://www.example.com/ |
- | } | + | |
- | } | + | |
</ | </ | ||
- | |||
- | * php composer.phar update | ||
- | * php vendor/ | ||
- | |||
- | ======== URL Management ======== | ||
<code php> | <code php> | ||
Строка 66: | Строка 58: | ||
</ | </ | ||
- | ====== Request ====== | + | ==== Конфигурация |
- | + | ||
- | ===== GET ===== | + | |
<code php> | <code php> | ||
- | Yii::$app->request->get(); | + | <?php |
- | Yii::$app->request->getQueryParams(); | + | return [ |
- | Yii::$app->request->getQueryParam('File'); | + | // ... |
- | Yii::$app->request->get('File'); | + | ' |
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | '< | ||
+ | |||
+ | // ... | ||
+ | |||
+ | [' | ||
+ | ], | ||
+ | ], | ||
+ | ], | ||
+ | ]; | ||
</ | </ | ||
- | ===== POST ===== | + | ==== Url ==== |
- | + | ||
- | <code php> | + | |
- | Yii:: | + | |
- | Yii:: | + | |
- | Yii:: | + | |
- | Yii:: | + | |
- | </ | + | |
- | ====== View ====== | + | |
- | + | ||
- | ===== Blocks ===== | + | |
- | + | ||
- | ====== CSS ====== | + | |
- | + | ||
- | ====== JS ====== | + | |
- | + | ||
- | ==== Вставка кода внутри представления ==== | + | |
- | + | ||
- | В примере производится вставка JS кода из assets c применением переменной в скрипте JS_FABRIC_CAR_URL | + | |
- | <code php> | + | |
- | $this = Yii:: | + | |
- | </ | + | |
- | + | ||
- | <code php> | + | |
- | <?php | + | |
- | $this-> | + | |
- | $publish = Yii:: | + | |
- | $this-> | + | |
- | ?> | + | |
- | </ | + | |
- | + | ||
- | ==== Добавление csrfToken в Ajax запрос ==== | + | |
- | + | ||
- | <code php> | + | |
- | $js_global_variables = ' | + | |
- | $.ajaxSetup({ | + | |
- | data: ' . \yii\helpers\Json:: | + | |
- | \yii:: | + | |
- | ]) . ' | + | |
- | });' . PHP_EOL; | + | |
- | $this-> | + | |
- | </ | + | |
- | + | ||
- | или через header | + | |
- | + | ||
- | <code php> | + | |
- | $js_global_variables = ' | + | |
- | $.ajaxPrefilter(function( options ) { | + | |
- | if ( !options.beforeSend) { | + | |
- | options.beforeSend = function (xhr) { | + | |
- | xhr.setRequestHeader(" | + | |
- | } | + | |
- | } | + | |
- | });' . PHP_EOL; | + | |
- | $this-> | + | |
- | </ | + | |
- | ====== Image ====== | + | |
- | ====== yii\helpers\Url:: | + | === yii\helpers\Url:: |
<code php> | <code php> | ||
Строка 151: | Строка 99: | ||
</ | </ | ||
- | ====== yii\helpers\Url:: | + | === yii\helpers\Url:: |
<code php> | <code php> | ||
Строка 181: | Строка 129: | ||
Примеры использоватия можно глянуть в тестах https:// | Примеры использоватия можно глянуть в тестах https:// | ||
- | ====== yii\helpers\Html:: | + | === yii\helpers\Html:: |
<code php> | <code php> | ||
Строка 201: | Строка 149: | ||
Примеры использоватия можно глянуть в тестах https:// | Примеры использоватия можно глянуть в тестах https:// | ||
- | ====== createUrl | + | ==== createUrl ==== |
<code php> | <code php> | ||
Строка 216: | Строка 164: | ||
echo $this-> | echo $this-> | ||
</ | </ | ||
- | ====== createAbsoluteUrl (old) ====== | + | |
+ | === createAbsoluteUrl (old) === | ||
Усли $this - это yii\web\Controller | Усли $this - это yii\web\Controller | ||
Строка 228: | Строка 177: | ||
</ | </ | ||
- | вместо старых методов нада использовать **yii\helpers:: | + | вместо старых методов надо использовать **yii\helpers:: |
Усли $this - это yii\web\Controller | Усли $this - это yii\web\Controller | ||
Строка 238: | Строка 187: | ||
</ | </ | ||
- | вместо старых методов нада использовать **yii\helpers:: | + | вместо старых методов надо использовать **yii\helpers:: |
- | ==== urlManager | + | ==== Request |
+ | === GET === | ||
<code php> | <code php> | ||
- | echo \Yii:: | + | Yii:: |
- | // / | + | Yii:: |
- | echo \Yii:: | + | Yii::$app->request-> |
- | // / | + | Yii:: |
- | echo \Yii:: | + | |
- | // http:// | + | |
</ | </ | ||
- | == Custom | + | === POST === |
<code php> | <code php> | ||
- | <?php | + | Yii:: |
- | return [ | + | Yii:: |
- | // ... | + | Yii:: |
- | | + | Yii:: |
- | 'urlManager' => [ | + | </ |
- | ' | + | |
- | ' | + | ===== View ===== |
- | ' | + | |
- | ' | + | ==== CSS ==== |
- | | + | |
- | '<action:(login|logout|about)>' => 'site/<action>', | + | <code php> |
- | + | $this-> | |
- | // ... | + | </ |
- | + | ||
- | ['class' => 'app\components\CarUrlRule', 'connectionID' => 'db', ...], | + | ==== JS ==== |
- | ], | + | |
- | | + | <code php> |
+ | $this-> | ||
+ | </ | ||
+ | |||
+ | === Вставка кода внутри представления === | ||
+ | |||
+ | В примере производится вставка JS кода из assets c применением переменной в скрипте JS_FABRIC_CAR_URL | ||
+ | <code php> | ||
+ | $this = Yii::$app->view; | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | $this-> | ||
+ | $publish | ||
+ | $this-> | ||
+ | ?> | ||
+ | </code> | ||
+ | |||
+ | ==== assetManager ==== | ||
+ | |||
+ | <code php> | ||
+ | 'components' => [ | ||
+ | | ||
+ | | ||
+ | | ||
], | ], | ||
- | ]; | + | ], |
</ | </ | ||
- | ====== Controller ====== | + | * forceCopy - Включает постоянное обновление асетов |
- | ===== Как программно вызвать контроллер? ===== | + | === через ajaxSetup |
+ | |||
+ | Добавление csrfToken | ||
<code php> | <code php> | ||
- | $className | + | $js_global_variables |
- | $controller | + | $.ajaxSetup({ |
+ | data: ' . \yii\helpers\Json:: | ||
+ | | ||
+ | ]) . ' | ||
+ | });' . PHP_EOL; | ||
+ | $this-> | ||
</ | </ | ||
- | ====== yii\helpers\Html (old) ====== | + | |
+ | или через header | ||
<code php> | <code php> | ||
- | // [' | + | $js_global_variables |
- | Html::a("Log in here", | + | $.ajaxPrefilter(function( options ) { |
- | Html::url([" | + | if ( !options.beforeSend) { |
+ | options.beforeSend | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | });' . PHP_EOL; | ||
+ | $this-> | ||
</ | </ | ||
- | **Подробно** https:// | + | или через JQuery |
+ | |||
+ | <code js> | ||
+ | $.ajax({ | ||
+ | url: ' | ||
+ | type: ' | ||
+ | data: {payload: payload, _csrf: yii.getCsrfToken()}, | ||
+ | dataType: ' | ||
+ | }).success(function(response) { | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | ==== Form ==== | ||
+ | |||
+ | * [[yii2: | ||
+ | * [[yii2: | ||
+ | |||
+ | **Подробно** https:// | ||
- | ======== Ajax ======== | + | ===== Ajax ===== |
* [[yii2: | * [[yii2: | ||
* [[yii2: | * [[yii2: | ||
- | ===== Pjax ===== | + | ==== Pjax ==== |
<code php> | <code php> | ||
Строка 350: | Строка 354: | ||
<?php Pjax:: | <?php Pjax:: | ||
</ | </ | ||
- | ======== User ======== | ||
- | ====== RBAC ====== | + | ===== Виджеты / Widgets |
- | * [[yii2:user:rbac: | + | * [[yii2:widgets:masked-input|]] - http:// |
- | * [[yii2:user: | + | |
- | **Подробно** https:// | + | ===== Model / AR / DAO / Запросы к БД ===== |
- | ======== Access ======== | + | |
+ | Как получить модель из модуля | ||
<code php> | <code php> | ||
- | ' | + | $user |
- | ' | + | $profile |
- | ' | + | |
- | [ | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | ], | + | |
- | [ | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | | + | |
- | } | + | |
- | ], | + | |
- | ], | + | |
- | ], | + | |
</ | </ | ||
+ | ==== Active Record ==== | ||
+ | <code php> | ||
+ | class Post extends ActiveRecord | ||
+ | { | ||
+ | public function getCreator() | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | public function getComments() | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | public function getTrustComments($isTrust = true) | ||
+ | { | ||
+ | return $this-> | ||
+ | -> | ||
+ | ': | ||
+ | ]) | ||
+ | -> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | <code php> | ||
+ | $price = PriceItem:: | ||
+ | -> | ||
+ | -> | ||
+ | [' | ||
+ | ['>', | ||
+ | ])-> | ||
+ | </ | ||
- | ======== | + | <code php> |
+ | // we receive an ActiveQuery instance | ||
+ | $query | ||
+ | |||
+ | // all posts | ||
+ | $posts | ||
+ | |||
+ | // looking for all the posts with a condition | ||
+ | $posts | ||
+ | -> | ||
+ | -> | ||
+ | -> | ||
+ | |||
+ | // looking for a post | ||
+ | $post = $query | ||
+ | | ||
+ | | ||
+ | |||
+ | // or easier: " | ||
+ | $post = Post:: | ||
+ | |||
+ | // having transmitted to the factory method not a massive, but a number equivalent to the primary key search | ||
+ | $post = Post:: | ||
+ | | ||
+ | | ||
+ | |||
+ | // we index the result to a necessary attribute | ||
+ | $posts | ||
+ | |||
+ | // the result as an array | ||
+ | $posts | ||
+ | </ | ||
- | ===== modules/user ===== | + | <code php> |
+ | User:: | ||
+ | User:: | ||
+ | </ | ||
+ | |||
+ | === Ключи в эллементах коллекции AR === | ||
<code php> | <code php> | ||
- | | + | $query = SparesCategory::find()->where([' |
- | $profile = Yii::$app->getModule(" | + | $query->indexBy = function($row){ // Ключ в который будет помещена возвращаемая модель |
+ | return $row->id; | ||
+ | }; | ||
+ | $categories = $query-> | ||
+ | </ | ||
- | if($user->load($_POST)){ | + | <code php> |
- | if ($user->save()) { | + | |
- | $profile->user_id | + | |
- | $profile-> | + | })->all(); |
- | | + | </ |
+ | |||
+ | === Создание модели из возвращаемы данных Command (с помощью populateRecord()) === | ||
+ | |||
+ | <code php> | ||
+ | | ||
+ | LEFT JOIN tbl_form_spares_category fc ON fc.category_id = c.id | ||
+ | WHERE fc.form_id = :form_id AND fc.category_id = : | ||
+ | $select_marks = Yii::$app->db-> | ||
+ | ': | ||
+ | ': | ||
+ | ])->queryAll(); | ||
+ | |||
+ | foreach($marks as $row){ | ||
+ | |||
+ | // Only yii2 alpha | ||
+ | //$mark = SparesCategoryForm:: | ||
+ | |||
+ | $mark = new SparesCategoryForm; | ||
+ | SparesCategoryForm:: | ||
+ | |||
+ | |||
+ | if(isset($mark-> | ||
+ | |||
+ | // do somethig | ||
+ | |||
+ | } | ||
} | } | ||
</ | </ | ||
- | * https:// | + | * [[Activedataprovider|Activedataprovider]] |
- | ====== Join ====== | + | * [[SqlDataProvider|SqlDataProvider]] |
+ | * [[ArrayDataProvider|ArrayDataProvider]] | ||
+ | ==== Join ==== | ||
- | ==== with() | + | === with() === |
Не делает SQL JOIN(), применяется не только в реляционых БД (NoSql) | Не делает SQL JOIN(), применяется не только в реляционых БД (NoSql) | ||
Применяется только для построений конструкций вида **WHERE IN()**, плюс наполняют [[yii2: | Применяется только для построений конструкций вида **WHERE IN()**, плюс наполняют [[yii2: | ||
- | ==== leftJoin() | + | === leftJoin() === |
Добавляет SQL LEFT JOIN(), применяется только в реляционыых БД, плюс наполняют [[yii2: | Добавляет SQL LEFT JOIN(), применяется только в реляционыых БД, плюс наполняют [[yii2: | ||
Строка 415: | Строка 501: | ||
</ | </ | ||
- | ==== joinWith() | + | === joinWith() === |
Добавляет SQL JOIN(), применяется только в реляционыых БД, плюс наполняют [[yii2: | Добавляет SQL JOIN(), применяется только в реляционыых БД, плюс наполняют [[yii2: | ||
Строка 442: | Строка 528: | ||
https:// | https:// | ||
- | ==== innerJoinWith() | + | === innerJoinWith() === |
- | + | ==== Validate (Rules) ==== | |
- | ====== Validate (Rules) | + | |
<code php> | <code php> | ||
Строка 455: | Строка 540: | ||
{ | { | ||
return [ | return [ | ||
- | [' | + | |
- | ' | + | |
- | // specifies in which scenario(s) this rule is active. | + | ' |
- | // if not given, it means it is active in all scenarios | + | // specifies in which scenario(s) this rule is active. |
- | ' | + | // if not given, it means it is active in all scenarios |
- | // the following name-value pairs will be used | + | ' |
- | // to initialize the validator properties | + | // the following name-value pairs will be used |
- | ' | + | // to initialize the validator properties |
- | ' | + | ' |
- | // ... | + | ' |
+ | // ... | ||
+ | | ||
+ | [ | ||
+ | [' | ||
+ | ' | ||
+ | | ||
+ | // if not given, it means it is active in all scenarios | ||
+ | ' | ||
+ | // the following name-value pairs will be used | ||
+ | // to initialize the validator properties | ||
+ | ' | ||
+ | ' | ||
+ | // ... | ||
+ | ], | ||
]; | ]; | ||
} | } | ||
Строка 505: | Строка 604: | ||
* **Подробно** https:// | * **Подробно** https:// | ||
* **Список базовых валидаторов: | * **Список базовых валидаторов: | ||
- | ====== Scenario | + | |
+ | ==== Scenario ==== | ||
<code php> | <code php> | ||
Строка 538: | Строка 638: | ||
**Подробно** https:// | **Подробно** https:// | ||
- | ====== Connection/ | ||
- | ==== Connection | + | ==== Relation |
+ | |||
+ | <code php> | ||
+ | class Customer extends \yii\db\ActiveRecord | ||
+ | { | ||
+ | // | ||
+ | public function getOrders() | ||
+ | { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | // | ||
+ | public function getBigOrders($threshold = 100) | ||
+ | { | ||
+ | return $this-> | ||
+ | -> | ||
+ | -> | ||
+ | } | ||
+ | |||
+ | // Вытащить данные из таблицы модели Item, используя связи из таблицы tbl_order_item | ||
+ | public function getItems() | ||
+ | { | ||
+ | // Получить данные через таблицу tbl_order_item | ||
+ | return $this-> | ||
+ | -> | ||
+ | } | ||
+ | |||
+ | // | ||
+ | public static function olderThan($query, | ||
+ | { | ||
+ | $query-> | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $customer = Customer:: | ||
+ | $orders = $customer-> | ||
+ | $orders = $customer-> | ||
+ | $customers = Customer:: | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $customer = Customer:: | ||
+ | $order = new Order(); | ||
+ | $order-> | ||
+ | $customer-> | ||
+ | </ | ||
+ | |||
+ | ==== Events ==== | ||
+ | |||
+ | <code php> | ||
+ | class Post extends ActiveRecord | ||
+ | { | ||
+ | public function init() | ||
+ | { | ||
+ | $this-> | ||
+ | // отменяем действие | ||
+ | $event-> | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $component-> | ||
+ | </ | ||
+ | ==== Как получить SQL и Параметры из ActiveQuery ==== | ||
+ | |||
+ | <code php> | ||
+ | $query = Post:: | ||
+ | |||
+ | // SQL | ||
+ | echo $query-> | ||
+ | |||
+ | // Params: | ||
+ | print_r($query-> | ||
+ | </ | ||
+ | ==== Объекты доступа к данным (Data Access Objects, DAO) ==== | ||
+ | |||
+ | === Соеденение | ||
<code php> | <code php> | ||
Строка 550: | Строка 729: | ||
* https:// | * https:// | ||
- | ==== Command ==== | + | === Методы запроса |
* queryOne() | * queryOne() | ||
Строка 559: | Строка 738: | ||
**Подробно** | **Подробно** | ||
+ | |||
+ | == createCommand == | ||
<code php> | <code php> | ||
Строка 599: | Строка 780: | ||
</ | </ | ||
- | ==== Query Builder and Query ==== | + | ==== Построитель запросов (Query Builder and Query) ==== |
<code php> | <code php> | ||
Строка 652: | Строка 833: | ||
* https:// | * https:// | ||
- | ====== Transactions | + | ==== Transactions ==== |
<code php> | <code php> | ||
Строка 666: | Строка 847: | ||
</ | </ | ||
- | ====== Active Record ====== | + | ==== Batch ==== |
- | <code php> | + | === Batch === |
- | class Post extends ActiveRecord | + | |
- | { | + | |
- | public function getCreator() | + | |
- | { | + | |
- | return $this-> | + | |
- | } | + | |
- | public function getComments() | + | |
- | { | + | |
- | return $this-> | + | |
- | } | + | |
- | public function getTrustComments($isTrust = true) | + | |
- | { | + | |
- | return $this-> | + | |
- | -> | + | |
- | ': | + | |
- | )) | + | |
- | -> | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | <code php> | + | |
- | // we receive an ActiveQuery instance | + | |
- | $query = Post:: | + | |
- | + | ||
- | // all posts | + | |
- | $posts = $query-> | + | |
- | + | ||
- | // looking for all the posts with a condition | + | |
- | $posts = $query | + | |
- | -> | + | |
- | -> | + | |
- | -> | + | |
- | + | ||
- | // looking for a post | + | |
- | $post = $query | + | |
- | | + | |
- | | + | |
- | + | ||
- | // or easier: " | + | |
- | $post = Post:: | + | |
- | + | ||
- | // having transmitted to the factory method not a massive, but a number equivalent to the primary key search | + | |
- | $post = Post:: | + | |
- | | + | |
- | | + | |
- | + | ||
- | // we index the result to a necessary attribute | + | |
- | $posts = $query-> | + | |
- | + | ||
- | // the result as an array | + | |
- | $posts = $query-> | + | |
- | </ | + | |
- | + | ||
- | ==== Batch ==== | + | |
Строка 736: | Строка 862: | ||
</ | </ | ||
- | ==== Each ==== | + | <code php> |
+ | $rows = []; | ||
+ | |||
+ | foreach ($models as $model) { | ||
+ | if (!$model-> | ||
+ | // At least one model has invalid data | ||
+ | |||
+ | break; | ||
+ | } | ||
+ | |||
+ | $rows[] | ||
+ | } | ||
+ | $rows = | ||
+ | $postModel | ||
+ | |||
+ | Yii:: | ||
+ | </ | ||
+ | |||
+ | === Each === | ||
<code php> | <code php> | ||
Строка 748: | Строка 892: | ||
</ | </ | ||
- | ==== Ключи в эллементах коллекции AR ==== | + | ===== Controller ===== |
- | == AR == | + | ==== Как программно вызвать контроллер? |
<code php> | <code php> | ||
- | $query = SparesCategory:: | + | $className |
- | $query-> | + | $controller |
- | return | + | |
- | }; | + | |
- | $categories = $query-> | + | |
</ | </ | ||
+ | |||
+ | ==== yii\helpers\Html (old) ==== | ||
<code php> | <code php> | ||
- | $quey = CarMark::find()->indexBy(function($row){ | + | // [' |
- | return $row->id; | + | Html::a("Log in here", [" |
- | | + | Html::url([" |
</ | </ | ||
- | ==== Создание модели из возвращаемы данных Command (с помощью populateRecord()) ==== | + | **Подробно** https:// |
+ | ===== Module ===== | ||
+ | |||
+ | ==== Submodule ==== | ||
+ | |||
+ | Для работы сабмодуля, | ||
<code php> | <code php> | ||
- | $sql = " | + | namespace mirocow\eav; |
- | LEFT JOIN tbl_form_spares_category fc ON fc.category_id = c.id | + | |
- | WHERE fc.form_id = :form_id AND fc.category_id = : | + | class Module extends \yii\base\Module { |
- | | + | |
- | ': | + | |
- | ': | + | |
- | ])-> | + | |
| | ||
- | foreach($marks as $row){ | + | // ... |
- | + | public function init() { | |
- | // Only yii2 alpha | + | |
- | // | + | // .. |
- | + | ||
- | | + | $this-> |
- | SparesCategoryForm:: | + | } |
- | + | ||
| | ||
- | if(isset($mark->id)){ | + | // Пример перенаправления роута в саб модуль admin |
+ | public function createController($route) { | ||
+ | |||
+ | return $this->getModule(' | ||
+ | |||
+ | } | ||
| | ||
- | // do somethig | + | } |
- | + | ||
- | } | + | |
- | | + | |
</ | </ | ||
+ | ==== Helpers ==== | ||
+ | * https:// | ||
- | * [[Activedataprovider|Activedataprovider ]] - Pager / Генерация страниц | + | ==== Behaviors/ |
- | * [[SqlDataProvider|SqlDataProvider ]] - Pager / Генерация страниц | + | |
- | + | ||
- | **Подробно: | + | |
- | + | ||
- | ====== Relation ====== | + | |
<code php> | <code php> | ||
- | class Customer extends \yii\db\ActiveRecord | + | public function behaviors() |
{ | { | ||
- | // | + | return array( |
- | public function getOrders() | + | 'access' => array( |
- | | + | 'class' => 'yii\web\AccessControl', |
- | return $this-> | + | 'rules' => array( |
- | } | + | array('allow' |
- | + | | |
- | // | + | ), |
- | public function getBigOrders($threshold = 100) | + | |
- | { | + | ); |
- | return $this-> | + | |
- | | + | |
- | | + | |
- | } | + | |
- | + | ||
- | // Вытащить данные из таблицы модели Item, используя связи из таблицы tbl_order_item | + | |
- | public function getItems() | + | |
- | { | + | |
- | // Получить данные через таблицу tbl_order_item | + | |
- | return $this->hasMany(Item:: | + | |
- | -> | + | |
- | } | + | |
- | + | ||
- | // | + | |
- | public static function olderThan($query, $age = 30) | + | |
- | { | + | |
- | | + | |
- | | + | |
} | } | ||
</ | </ | ||
+ | |||
+ | ==== Behaviors/ | ||
<code php> | <code php> | ||
- | $customer | + | public function behaviors() { |
- | $orders | + | return [ |
- | $orders | + | [ |
- | $customers | + | ' |
+ | ' | ||
+ | //' | ||
+ | ' | ||
+ | ], | ||
+ | ]; | ||
+ | } | ||
</ | </ | ||
+ | ===== Session ===== | ||
<code php> | <code php> | ||
- | $customer = Customer::find(1); | + | Yii::$app->session-> |
- | $order = new Order(); | + | Yii::$app->session-> |
- | $order->comment = ''; | + | |
- | $customer->link('orders', | + | |
</ | </ | ||
- | ======== Events ======== | + | ==== setFlash |
+ | В контролере | ||
<code php> | <code php> | ||
- | class Post extends ActiveRecord | + | Yii::$app->session-> |
- | { | + | return |
- | public function init() | + | |
- | { | + | |
- | | + | |
- | // отменяем действие | + | |
- | $event->isValid = false; | + | |
- | }); | + | |
- | } | + | |
- | } | + | |
</ | </ | ||
+ | return делать обязательно. инчаче после редиректа сообщение не выведется | ||
+ | Вывод стандартным способом | ||
<code php> | <code php> | ||
- | $component->on('beforeAction', $handler); | + | <?php foreach (Yii::$app->session-> |
+ | echo '<div class=" | ||
+ | } ?> | ||
</ | </ | ||
- | ======== Components ======== | + | Вывод с помощью виджета |
- | + | ||
- | Создание | + | |
<code php> | <code php> | ||
- | $object | + | <?= Wrapper::widget([ |
- | 'class' => 'MyClass', | + | 'layerClass' => 'lo\modules\noty\layers\Noty', |
- | ' | + | ]);?> |
- | ' | + | |
- | ), $param1, $param2); | + | |
</ | </ | ||
+ | Данный виджет поддерживает большое кол-во виджетов обображения таких как: | ||
+ | | Library (Layer) | ||
+ | | Bootstrap Alert | - | http:// | ||
+ | | Bootstrap Notify | ||
+ | | Growl | jquery-growl | ||
+ | | iGrowl | ||
+ | | jQuery Notify | ||
+ | | jQuery Notify Bar | jqnotifybar | ||
+ | | jQuery Toaster | ||
+ | | jQuery Toast Plugin | ||
+ | | Lobibox | ||
+ | | Notie | notie | https:// | ||
+ | | Notific8 | ||
+ | | NotifIt | ||
+ | | Notify.js | ||
+ | | Noty | noty | https:// | ||
+ | | PNotify | ||
+ | | Sweetalert | ||
+ | | Toastr | ||
- | ======== | + | |
+ | ===== Console | ||
+ | |||
+ | ==== IsConsole | ||
<code php> | <code php> | ||
- | ' | + | if(Yii::$app->request->isConsoleRequest){ |
- | ' | + | |
- | ' | + | } |
- | ' | + | |
- | ), | + | |
- | ), | + | |
</ | </ | ||
- | ====== Form ====== | + | ==== Params / Параметры |
- | * [[yii2: | + | <code php> |
- | * [[yii2: | + | <?php |
- | **Подробно** https:// | + | namespace console\controllers; |
- | ====== Assets ====== | + | |
+ | use yii\console\Controller; | ||
- | ======== Console ======== | + | class QueueController extends Controller |
+ | { | ||
+ | public $pid_file | ||
- | ======== Module ======== | + | public function __get($name) |
+ | { | ||
+ | $name = str_replace(' | ||
- | ====== Submodule ====== | + | return $this-> |
+ | } | ||
- | Для работы сабмодуля, необходимо выполнить его подключение, | + | public function __set($name, $value) |
- | <code php> | + | { |
- | namespace mirocow\eav; | + | $name = str_replace(' |
- | class Module extends \yii\base\Module { | + | $this-> |
- | | + | |
- | public function init() { | + | |
- | // .. | ||
- | | + | public function options($actionID) |
+ | { | ||
+ | return ['pid-file']; | ||
+ | } | ||
+ | |||
+ | public function actionRun() | ||
+ | { | ||
} | } | ||
- | | ||
- | // Пример перенаправления роута в саб модуль admin | ||
- | public function createController($route) { | ||
- | | ||
- | return $this-> | ||
- | | ||
- | } | ||
} | } | ||
</ | </ | ||
+ | ===== User ===== | ||
- | ======== Helpers ======== | + | ==== RBAC ==== |
- | * https:// | + | * [[yii2: |
- | ======== Behaviors/Filters | + | **Подробно** https:// |
+ | |||
+ | ==== Access | ||
<code php> | <code php> | ||
- | public function behaviors() | + | |
- | { | + | ' |
- | return array( | + | ' |
- | | + | [ |
- | ' | + | ' |
- | ' | + | |
- | | + | |
- | | + | ], |
- | ), | + | [ |
- | ), | + | |
- | | + | ' |
- | } | + | ' |
+ | return \Yii:: | ||
+ | } | ||
+ | ], | ||
+ | ], | ||
+ | | ||
</ | </ | ||
- | ====== Статьи ====== | ||
+ | ===== Components ===== | ||
- | * http:// | + | Создание объекта компанента |
+ | <code php> | ||
+ | $object = Yii:: | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), $param1, $param2); | ||
+ | </code> | ||
- | + | ===== Theme ===== | |
- | ======== Локализация - i18N/intl ======== | + | |
- | + | ||
- | apt=get install php5-intl | + | |
- | + | ||
- | + | ||
- | ======== Session ======== | + | |
<code php> | <code php> | ||
- | Yii:: | + | ' |
- | Yii:: | + | |
+ | ' | ||
+ | | ||
+ | | ||
+ | ), | ||
</ | </ | ||
- | ====== Cache ====== | + | ===== Cache ===== |
<code php> | <code php> | ||
Строка 993: | Строка 1152: | ||
* [[yii\caching\TagDependency]]: | * [[yii\caching\TagDependency]]: | ||
- | ===== Кэширование запросов | + | ==== Кэширование запросов ==== |
<code php> | <code php> | ||
Строка 1057: | Строка 1216: | ||
}); | }); | ||
</ | </ | ||
- | ===== Статьи ===== | ||
- | * https:// | + | ===== Локализация |
+ | apt=get install php5-intl | ||
+ | |||
+ | ==== Перевод (локализация проекта) ==== | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | ===== PHPUnit ===== | ||
+ | |||
+ | добавить в конфигуратор композера composer.json | ||
+ | < | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * php composer.phar update | ||
+ | * php vendor/ | ||
+ | |||
+ | ====== Статьи ====== | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | * http:// | ||
====== Описание ====== | ====== Описание ====== | ||
Строка 1067: | Строка 1250: | ||
* http:// | * http:// | ||
* http:// | * http:// | ||
+ |