Различия
Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
yii2:docs [2017/01/04 22:35] – [Model] mirocow | yii2:docs [2019/04/18 13:17] (текущий) – [Active Record] mirocow | ||
---|---|---|---|
Строка 290: | Строка 290: | ||
}); | }); | ||
</ | </ | ||
+ | |||
+ | ==== Form ==== | ||
+ | |||
+ | * [[yii2: | ||
+ | * [[yii2: | ||
+ | |||
+ | **Подробно** https:// | ||
===== Ajax ===== | ===== Ajax ===== | ||
Строка 363: | Строка 370: | ||
==== Active Record ==== | ==== 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 = Post:: | ||
+ | |||
+ | // all posts | ||
+ | $posts = $query-> | ||
+ | |||
+ | // looking for all the posts with a condition | ||
+ | $posts = $query | ||
+ | -> | ||
+ | -> | ||
+ | ->all(); | ||
+ | |||
+ | // 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-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | User:: | ||
+ | User:: | ||
+ | </ | ||
+ | |||
+ | === Ключи в эллементах коллекции AR === | ||
+ | |||
+ | <code php> | ||
+ | $query = SparesCategory:: | ||
+ | $query-> | ||
+ | return $row-> | ||
+ | }; | ||
+ | $categories = $query-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $quey = CarMark:: | ||
+ | return $row-> | ||
+ | })-> | ||
+ | </ | ||
+ | |||
+ | === Создание модели из возвращаемы данных Command (с помощью populateRecord()) === | ||
+ | |||
+ | <code php> | ||
+ | $sql = " | ||
+ | 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:: | ||
+ | ': | ||
+ | ': | ||
+ | ])-> | ||
+ | | ||
+ | foreach($marks as $row){ | ||
+ | | ||
+ | // Only yii2 alpha | ||
+ | //$mark = SparesCategoryForm:: | ||
+ | | ||
+ | $mark = new SparesCategoryForm; | ||
+ | SparesCategoryForm:: | ||
+ | | ||
+ | | ||
+ | if(isset($mark-> | ||
+ | | ||
+ | // do somethig | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * [[Activedataprovider|Activedataprovider]] | ||
+ | * [[SqlDataProvider|SqlDataProvider]] | ||
+ | * [[ArrayDataProvider|ArrayDataProvider]] | ||
==== Join ==== | ==== Join ==== | ||
Строка 417: | Строка 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 | ||
+ | ' | ||
+ | ' | ||
+ | // ... | ||
+ | ], | ||
]; | ]; | ||
} | } | ||
Строка 502: | Строка 639: | ||
**Подробно** https:// | **Подробно** https:// | ||
- | ==== DAO ==== | + | ==== 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, | ||
=== Соеденение === | === Соеденение === | ||
Строка 524: | Строка 739: | ||
**Подробно** | **Подробно** | ||
- | == Query Builder and Query == | + | == createCommand == |
+ | |||
+ | <code php> | ||
+ | $command = $connection-> | ||
+ | $post = $command-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $connection-> | ||
+ | ' | ||
+ | ' | ||
+ | ])-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $command = $connection-> | ||
+ | $command-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $connection-> | ||
+ | [' | ||
+ | [' | ||
+ | [' | ||
+ | ])-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $connection-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $connection-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $command = $connection-> | ||
+ | $command-> | ||
+ | $post = $command-> | ||
+ | </ | ||
+ | |||
+ | ==== Построитель запросов (Query Builder and Query) ==== | ||
<code php> | <code php> | ||
Строка 577: | Строка 833: | ||
* https:// | * https:// | ||
- | == createCommand | + | ==== Transactions ==== |
<code php> | <code php> | ||
- | $command | + | $transaction |
- | $post = $command->queryOne(); | + | try { |
+ | | ||
+ | $connection-> | ||
+ | // ... executing other SQL statements ... | ||
+ | $transaction-> | ||
+ | } catch(Exception $e) { | ||
+ | $transaction-> | ||
+ | } | ||
</ | </ | ||
- | <code php> | + | ==== Batch ==== |
- | $connection-> | + | |
- | ' | + | === Batch === |
- | ' | + | |
- | ])-> | + | |
- | </ | + | |
<code php> | <code php> | ||
- | $command = $connection-> | + | foreach (Customer:: |
- | $command->execute(); | + | |
- | </ | + | |
- | <code php> | + | } |
- | $connection-> | + | |
- | | + | foreach (Customer:: |
- | | + | |
- | [' | + | } |
- | ])-> | + | |
</ | </ | ||
<code php> | <code php> | ||
- | $connection-> | + | $rows = []; |
- | </code> | + | |
+ | foreach ($models as $model) { | ||
+ | if (!$model->validate()) { | ||
+ | // At least one model has invalid data | ||
+ | |||
+ | break; | ||
+ | } | ||
- | <code php> | + | $rows[] = $model->attributes; |
- | $connection-> | + | } |
+ | $rows = \yii\helpers\ArrayHelper:: | ||
+ | $postModel = new Post; | ||
+ | |||
+ | Yii:: | ||
</ | </ | ||
+ | |||
+ | === Each === | ||
<code php> | <code php> | ||
- | $command = $connection-> | + | foreach |
- | $command->bindValue(': | + | |
- | $post = $command-> | + | |
- | </ | + | |
- | ==== Transactions ==== | + | } |
- | <code php> | + | foreach |
- | $transaction = $connection-> | + | // $customers — массив из 10 или менее объектов Customer |
- | try { | + | |
- | $connection-> | + | |
- | | + | |
- | // ... executing other SQL statements ... | + | |
- | | + | |
- | } catch(Exception $e) { | + | |
- | $transaction-> | + | |
} | } | ||
</ | </ | ||
Строка 651: | Строка 911: | ||
**Подробно** https:// | **Подробно** https:// | ||
+ | ===== Module ===== | ||
+ | |||
+ | ==== Submodule ==== | ||
+ | |||
+ | Для работы сабмодуля, | ||
+ | <code php> | ||
+ | namespace mirocow\eav; | ||
+ | |||
+ | class Module extends \yii\base\Module { | ||
+ | | ||
+ | // ... | ||
+ | public function init() { | ||
+ | |||
+ | // .. | ||
+ | |||
+ | $this-> | ||
+ | } | ||
+ | | ||
+ | // Пример перенаправления роута в саб модуль admin | ||
+ | public function createController($route) { | ||
+ | | ||
+ | return $this-> | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Helpers ==== | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | ==== Behaviors/ | ||
+ | |||
+ | <code php> | ||
+ | public function behaviors() | ||
+ | { | ||
+ | return array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | array(' | ||
+ | array(' | ||
+ | ), | ||
+ | ), | ||
+ | ); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Behaviors/ | ||
+ | |||
+ | <code php> | ||
+ | public function behaviors() { | ||
+ | return [ | ||
+ | [ | ||
+ | ' | ||
+ | ' | ||
+ | //' | ||
+ | ' | ||
+ | ], | ||
+ | ]; | ||
+ | } | ||
+ | </ | ||
===== Session ===== | ===== Session ===== | ||
Строка 710: | Строка 1033: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ==== Params / Параметры ==== | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | namespace console\controllers; | ||
+ | |||
+ | use yii\console\Controller; | ||
+ | |||
+ | class QueueController extends Controller | ||
+ | { | ||
+ | public $pid_file = ''; | ||
+ | |||
+ | public function __get($name) | ||
+ | { | ||
+ | $name = str_replace(' | ||
+ | |||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | public function __set($name, | ||
+ | { | ||
+ | $name = str_replace(' | ||
+ | |||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | |||
+ | public function options($actionID) | ||
+ | { | ||
+ | return [' | ||
+ | } | ||
+ | |||
+ | public function actionRun() | ||
+ | { | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
===== User ===== | ===== User ===== | ||
Строка 741: | Строка 1105: | ||
</ | </ | ||
+ | |||
+ | ===== Components ===== | ||
+ | |||
+ | Создание объекта компанента | ||
+ | <code php> | ||
+ | $object = Yii:: | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), $param1, $param2); | ||
+ | </ | ||
+ | |||
+ | ===== Theme ===== | ||
+ | |||
+ | <code php> | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), | ||
+ | ), | ||
+ | </ | ||
===== Cache ===== | ===== Cache ===== | ||
Строка 834: | Строка 1220: | ||
apt=get install php5-intl | apt=get install php5-intl | ||
+ | |||
+ | ==== Перевод (локализация проекта) ==== | ||
+ | |||
+ | * https:// | ||
===== PHPUnit ===== | ===== PHPUnit ===== | ||
Строка 849: | Строка 1239: | ||
* php vendor/ | * php vendor/ | ||
- | ===== Статьи ===== | + | ====== Статьи |
* https:// | * https:// | ||
Строка 855: | Строка 1245: | ||
* http:// | * http:// | ||
- | ===== Описание ===== | + | ====== Описание |
* https:// | * https:// |