1 | <?php |
||
21 | class LoaderBehavior extends Behavior |
||
22 | { |
||
23 | /** |
||
24 | * Callback, returns identifier value (will be used in filtering) |
||
25 | * @see targetAttribute |
||
26 | * @see queryFilter |
||
27 | * |
||
28 | * ```php |
||
29 | * <?php |
||
30 | * $id = function(): string { |
||
31 | * return \Yii::$app->request->post('some-post-value'); |
||
32 | * }; |
||
33 | * ``` |
||
34 | * |
||
35 | * Or string - name of get parameter |
||
36 | * |
||
37 | * @var string|callable |
||
38 | */ |
||
39 | public $id = 'id'; |
||
40 | |||
41 | /** |
||
42 | * ActiveRecord class |
||
43 | * will be used for creating query |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public $targetClass; |
||
48 | |||
49 | /** |
||
50 | * ActiveRecord attribute that will be used for auto filtering |
||
51 | * @var string |
||
52 | */ |
||
53 | public $targetAttribute = 'id'; |
||
54 | |||
55 | /** |
||
56 | * Custom query filter. |
||
57 | * Receives query as first argument and id value as second |
||
58 | * Have to return ActiveQuery: |
||
59 | * |
||
60 | * ```php |
||
61 | * <?php |
||
62 | * $queryFilter = function(\yii\db\ActiveQuery $query, string $id) { |
||
63 | * return $query->andWhere(['=', 'some_attribute', $id + 1]); |
||
64 | * }; |
||
65 | * ``` |
||
66 | * |
||
67 | * @var callable |
||
68 | */ |
||
69 | public $queryFilter; |
||
70 | |||
71 | /** |
||
72 | * Model attribute to load record to |
||
73 | * Default value will be counted from targetClass::tableName method |
||
74 | * |
||
75 | * For example: if your active record table name is `post_comment_author` |
||
76 | * this value will be set to `postCommentAuthor` |
||
77 | * |
||
78 | * @see ActiveRecord::tableName() |
||
79 | * @see Inflector::camelize() |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | public $attribute = 'dependency'; |
||
84 | |||
85 | /** |
||
86 | * Callable that receives record if it found |
||
87 | * |
||
88 | * ```php |
||
89 | * <?php |
||
90 | * $load = function(\yii\db\ActiveRecord $record) use($model) { |
||
91 | * $model->dependency = $record; |
||
92 | * }; |
||
93 | * ``` |
||
94 | * |
||
95 | * @var callable |
||
96 | */ |
||
97 | public $load; |
||
98 | |||
99 | /** @var callable */ |
||
100 | public $notFoundCallback; |
||
101 | |||
102 | 1 | public function events() |
|
108 | |||
109 | 1 | public function init() |
|
117 | |||
118 | /** |
||
119 | * @throws NotFoundHttpException |
||
120 | * @throws InvalidConfigException |
||
121 | */ |
||
122 | 1 | public function load(): void |
|
160 | |||
161 | /** |
||
162 | * @param int $id |
||
163 | * @throws NotFoundHttpException |
||
164 | */ |
||
165 | 1 | protected function notFound(int $id = null): void |
|
173 | } |
||
174 |