Skip to content

Commit c403d6b

Browse files
committed
Improve working with relations
1 parent 498b52d commit c403d6b

File tree

9 files changed

+130
-105
lines changed

9 files changed

+130
-105
lines changed

api/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* [filePath](#filepath)
1010
* [fileUrl](#fileurl)
1111
* [fileExtraFields](#fileextrafields)
12-
* [allFiles](#allfiles)
12+
* [files](#files)
1313
* [file](#file)
1414
* [fileRules](#filerules)
1515
* [fileState](#filestate)
@@ -25,7 +25,7 @@
2525

2626

2727
* Full name: \rkit\filemanager\behaviors\FileBehavior
28-
* Parent class:
28+
* Parent class:
2929

3030

3131
### fileRelation
@@ -172,12 +172,12 @@ FileBehavior::fileExtraFields( string $attribute ): array
172172

173173
---
174174

175-
### allFiles
175+
### files
176176

177177
Get files
178178

179179
```php
180-
FileBehavior::allFiles( string $attribute ): array<mixed,\ActiveRecord>
180+
FileBehavior::files( string $attribute ): array<mixed,\ActiveRecord>
181181
```
182182

183183

guide/README.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ Let's do it.
4848
4. **Declaring a relation**
4949

5050
```php
51-
public function getFiles($callable = null)
51+
public function getPreviewFile()
5252
{
5353
return $this
5454
->hasMany(File::className(), ['id' => 'file_id'])
55-
->viaTable('news_files', ['news_id' => 'id'], $callable);
55+
->viaTable('news_files', ['news_id' => 'id']);
5656
}
5757
```
5858

@@ -101,7 +101,7 @@ Let's do it.
101101
// file type (default `image`)
102102
'type' => 'image',
103103
// relation name
104-
'relation' => 'files',
104+
'relation' => 'previewFile',
105105
// save file path in attribute (default `false`)
106106
'saveFilePathInAttribute' => true,
107107
// a callback for generating file path
@@ -161,7 +161,7 @@ Let's do it.
161161
// path to template for upload response (the default is an array with id and path of file)
162162
'template' => null,
163163
// a relation name
164-
'relation' => 'files',
164+
'relation' => 'previewFile',
165165
// save file path in attribute (default `false`)
166166
'saveFilePathInAttribute' => true,
167167
// save file id in attribute (default `false`)
@@ -196,11 +196,6 @@ Let's do it.
196196
'position' => $positions[$file->id], // if you want to set sort order
197197
];
198198
},
199-
// a callback for customizing the relation associated with the junction table
200-
// `function(ActiveQuery $query): ActiveQuery`
201-
'relationQuery' => function ($query) {
202-
return $query->andWhere(['type' => 1]); // to select a specific type of file
203-
},
204199
// core validators
205200
'rules' => [
206201
'imageSize' => ['minWidth' => 300, 'minHeight' => 300],
@@ -232,10 +227,16 @@ To get file
232227
$model->file('preview');
233228
```
234229

230+
or by relation name
231+
232+
```php
233+
$model->previewFile;
234+
```
235+
235236
If multiple files
236237

237238
```php
238-
$model->allFiles('gallery');
239+
$model->files('gallery');
239240
```
240241

241242
To get file full path

src/behaviors/FileBehavior.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function afterSave()
9393
}
9494

9595
if ($files === [] || $files === '') {
96-
$this->fileBind->delete($this->owner, $attribute, $this->allFiles($attribute));
96+
$this->fileBind->delete($this->owner, $attribute, $this->files($attribute));
9797
continue;
9898
}
9999

@@ -119,7 +119,7 @@ public function afterSave()
119119
public function beforeDelete()
120120
{
121121
foreach ($this->attributes as $attribute => $options) {
122-
$this->fileBind->delete($this->owner, $attribute, $this->allFiles($attribute));
122+
$this->fileBind->delete($this->owner, $attribute, $this->files($attribute));
123123
}
124124
}
125125

@@ -294,7 +294,7 @@ public function fileExtraFields($attribute)
294294
* @param string $attribute The attribute name
295295
* @return \ActiveRecord[] The file models
296296
*/
297-
public function allFiles($attribute)
297+
public function files($attribute)
298298
{
299299
return $this->fileBind->files($this->owner, $attribute);
300300
}

src/behaviors/FileBind.php

+16-16
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,25 @@ private function updateRelations($model, $attribute, $rows)
142142
private function updateFiles($model, $attribute, $files)
143143
{
144144
$handlerUpdateFile = $model->fileOption($attribute, 'updateFile');
145-
if ($handlerUpdateFile) {
146-
foreach ($files as $file) {
145+
$relation = $model->fileOption($attribute, 'relation');
146+
$isMultiple = $model->fileOption($attribute, 'multiple');
147+
148+
$relationValue = [];
149+
foreach ($files as $file) {
150+
$relationValue[] = $file;
151+
if ($handlerUpdateFile) {
147152
$fileUpd = $handlerUpdateFile($file);
148153
$dirtyAttributes = $fileUpd->getDirtyAttributes();
149154
if (count($dirtyAttributes)) {
150155
$fileUpd->updateAttributes($dirtyAttributes);
151156
}
152157
}
153158
}
159+
160+
if (!$isMultiple) {
161+
$relationValue = array_shift($relationValue);
162+
}
163+
$model->populateRelation($relation, $relationValue);
154164
}
155165

156166
public function delete($model, $attribute, $files)
@@ -169,6 +179,7 @@ public function delete($model, $attribute, $files)
169179
$storage->delete($filePath);
170180
}
171181
}
182+
172183
if ($file->delete()) {
173184
$db->delete($relation->via->from[0], [
174185
current($relation->link) => $file->getPrimaryKey()
@@ -184,15 +195,8 @@ public function delete($model, $attribute, $files)
184195
public function relations($model, $attribute)
185196
{
186197
$relation = $model->fileRelation($attribute);
187-
$handlerRelationQuery = $model->fileOption($attribute, 'relationQuery');
188-
$query = null;
189-
190-
if ($handlerRelationQuery) {
191-
$query = Query::create($handlerRelationQuery($model->find()));
192-
}
193198

194-
$query = $query ?: new Query();
195-
return $query
199+
return (new Query())
196200
->from($relation->via->from[0])
197201
->andWhere([key($relation->via->link) => $model->getPrimaryKey()])
198202
->indexBy(current($relation->link))
@@ -203,18 +207,14 @@ public function files($model, $attribute)
203207
{
204208
$relation = $model->fileRelation($attribute);
205209
$relationName = $model->fileOption($attribute, 'relation');
206-
$handlerRelationQuery = $model->fileOption($attribute, 'relationQuery');
207210

208-
$query = call_user_func_array([$model, 'get' . $relationName], [$handlerRelationQuery]);
211+
$query = call_user_func([$model, 'get' . $relationName]);
209212
return $query->indexBy(key($relation->link))->all();
210213
}
211214

212215
public function file($model, $attribute)
213216
{
214217
$relation = $model->fileOption($attribute, 'relation');
215-
$handlerRelationQuery = $model->fileOption($attribute, 'relationQuery');
216-
217-
$query = call_user_func_array([$model, 'get' . $relation], [$handlerRelationQuery]);
218-
return $query->one();
218+
return $model->$relation;
219219
}
220220
}

tests/MultipleUploadTest.php

+10-35
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public function setUp()
2222

2323
public function testUpload()
2424
{
25-
$model = $this->createObject($this->modelClass, [
26-
'field' => 'gallery',
27-
'multiple' => true
28-
]);
29-
25+
$model = $this->createObject($this->modelClass);
3026
$response = $this->runUploadAction([
3127
'modelObject' => $model,
3228
'attribute' => 'gallery',
@@ -47,8 +43,6 @@ public function testUpload()
4743
public function testUploadWithTemplate()
4844
{
4945
$model = $this->createObject($this->modelClass, [
50-
'field' => 'gallery',
51-
'multiple' => true,
5246
'template' => '@tests/data/views/gallery-item.php',
5347
]);
5448

@@ -68,8 +62,6 @@ public function testUploadWithTemplate()
6862
public function testUploadAndBind()
6963
{
7064
$model = $this->createObject($this->modelClass, [
71-
'field' => 'gallery',
72-
'multiple' => true,
7365
'rules.maxFiles' => 2,
7466
]);
7567

@@ -88,7 +80,7 @@ public function testUploadAndBind()
8880
$model->gallery = [$response1['id'], $response2['id']];
8981
$model->save();
9082

91-
$files = $model->allFiles('gallery');
83+
$files = $model->files('gallery');
9284
$this->assertCount(2, $files);
9385

9486
foreach ($files as $file) {
@@ -100,8 +92,6 @@ public function testUploadAndBind()
10092
public function testAddFile()
10193
{
10294
$model = $this->createObject($this->modelClass, [
103-
'field' => 'gallery',
104-
'multiple' => true,
10595
'rules.maxFiles' => 2,
10696
]);
10797

@@ -123,7 +113,7 @@ public function testAddFile()
123113
$model->gallery = [$response1['id'], $response2['id']];
124114
$model->save();
125115

126-
$files = $model->allFiles('gallery');
116+
$files = $model->files('gallery');
127117
$this->assertCount(2, $files);
128118

129119
foreach ($files as $file) {
@@ -135,8 +125,6 @@ public function testAddFile()
135125
public function testUpdateLinks()
136126
{
137127
$model = $this->createObject($this->modelClass, [
138-
'field' => 'gallery',
139-
'multiple' => true,
140128
'extraFields' => function () {
141129
return [
142130
'type' => 2,
@@ -158,8 +146,6 @@ public function testUpdateLinks()
158146
$this->assertEquals($fields[$response['id']]['position'], 1);
159147

160148
$model = $this->createObject($this->modelClass, [
161-
'field' => 'gallery',
162-
'multiple' => true,
163149
'extraFields' => function () {
164150
return [
165151
'type' => 2,
@@ -179,8 +165,6 @@ public function testUpdateLinks()
179165
public function testMaxFiles()
180166
{
181167
$model = $this->createObject($this->modelClass, [
182-
'field' => 'gallery',
183-
'multiple' => true,
184168
'rules.maxFiles' => 1,
185169
]);
186170

@@ -198,15 +182,13 @@ public function testMaxFiles()
198182
$model->gallery = [$response1['id'], $response2['id']];
199183
$model->save();
200184

201-
$files = $model->allFiles('gallery');
185+
$files = $model->files('gallery');
202186
$this->assertCount(1, $files);
203187
}
204188

205189
public function testClearFiles()
206190
{
207191
$model = $this->createObject($this->modelClass, [
208-
'field' => 'gallery',
209-
'multiple' => true,
210192
'rules.maxFiles' => 2,
211193
]);
212194

@@ -224,22 +206,18 @@ public function testClearFiles()
224206
$model->gallery = [$response1['id'], $response2['id']];
225207
$model->save();
226208

227-
$files = $model->allFiles('gallery');
209+
$files = $model->files('gallery');
228210
$this->assertCount(2, $files);
229211

230212
$model->gallery = [];
231213
$model->save();
232214

233-
$this->assertCount(0, $model->allFiles('gallery'));
215+
$this->assertCount(0, $model->files('gallery'));
234216
}
235217

236218
public function testDeleteWrongItem()
237219
{
238-
$model = $this->createObject($this->modelClass, [
239-
'field' => 'gallery',
240-
'multiple' => true
241-
]);
242-
220+
$model = $this->createObject($this->modelClass);
243221
$response = $this->runUploadAction([
244222
'modelObject' => $model,
245223
'attribute' => 'gallery',
@@ -249,19 +227,16 @@ public function testDeleteWrongItem()
249227
$model->gallery = [$response['id'], 9999];
250228
$model->save();
251229

252-
$this->assertCount(1, $model->allFiles('gallery'));
230+
$this->assertCount(1, $model->files('gallery'));
253231
}
254232

255233
public function testDeleteWrongGallery()
256234
{
257-
$model = $this->createObject($this->modelClass, [
258-
'field' => 'gallery',
259-
'multiple' => true
260-
]);
235+
$model = $this->createObject($this->modelClass);
261236

262237
$model->gallery = [9999];
263238
$model->save();
264239

265-
$this->assertCount(0, $model->allFiles('gallery'));
240+
$this->assertCount(0, $model->files('gallery'));
266241
}
267242
}

tests/SingleUploadTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function testUpdateLinks()
133133
$model = $this->createObject($this->modelClass, [
134134
'extraFields' => function () {
135135
return [
136-
'type' => 2,
136+
'type' => 1,
137137
'position' => 1,
138138
];
139139
}
@@ -154,7 +154,7 @@ public function testUpdateLinks()
154154
$model = $this->createObject($this->modelClass, [
155155
'extraFields' => function () {
156156
return [
157-
'type' => 2,
157+
'type' => 1,
158158
'position' => 2,
159159
];
160160
}
@@ -203,9 +203,11 @@ public function testDeleteModel()
203203

204204
$model->image = $response['id'];
205205
$model->save();
206+
207+
$this->assertFileExists($model->filePath('image'));
208+
206209
$model->delete();
207210

208-
$this->assertNull($model->file('image'));
209211
$this->assertFileNotExists($model->filePath('image'));
210212
}
211213

0 commit comments

Comments
 (0)