|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-element-lists/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-element-lists/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\element\lists\actions\source; |
|
10
|
|
|
|
|
11
|
|
|
use craft\base\Field; |
|
12
|
|
|
use flipbox\craft\ember\actions\ManageTrait; |
|
13
|
|
|
use flipbox\craft\ember\helpers\SiteHelper; |
|
14
|
|
|
use flipbox\craft\element\lists\actions\ResolverTrait; |
|
15
|
|
|
use flipbox\craft\element\lists\records\Association; |
|
16
|
|
|
use yii\base\Action; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Flipbox Factory <[email protected]> |
|
20
|
|
|
* @since 2.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Associate extends Action |
|
23
|
|
|
{ |
|
24
|
|
|
use ManageTrait, |
|
25
|
|
|
ResolverTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $field |
|
29
|
|
|
* @param string $source |
|
30
|
|
|
* @param string $target |
|
31
|
|
|
* @param int|null $siteId |
|
32
|
|
|
* @param int|null $sortOrder |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
* @throws \yii\web\HttpException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function run( |
|
37
|
|
|
string $field, |
|
38
|
|
|
string $source, |
|
39
|
|
|
string $target, |
|
40
|
|
|
int $siteId = null, |
|
41
|
|
|
int $sortOrder = null |
|
42
|
|
|
) { |
|
43
|
|
|
|
|
44
|
|
|
// Resolve |
|
45
|
|
|
$field = $this->resolveField($field); |
|
46
|
|
|
$source = $this->resolveElement($source); |
|
47
|
|
|
$target = $this->resolveElement($target); |
|
48
|
|
|
|
|
49
|
|
|
/** @var Field $field */ |
|
50
|
|
|
|
|
51
|
|
|
$siteId = SiteHelper::ensureSiteId($siteId ?: $source->siteId); |
|
52
|
|
|
|
|
53
|
|
|
$record = Association::find() |
|
54
|
|
|
->fieldId($field->id) |
|
55
|
|
|
->sourceId($source->getId() ?: false) |
|
56
|
|
|
->targetId($target->getId() ?: false) |
|
57
|
|
|
->siteId($siteId) |
|
58
|
|
|
->one(); |
|
59
|
|
|
|
|
60
|
|
|
if (!$record) { |
|
61
|
|
|
$record = new Association([ |
|
62
|
|
|
'fieldId' => $field->id, |
|
63
|
|
|
'sourceId' => $source->getId(), |
|
64
|
|
|
'targetId' => $target->getId(), |
|
65
|
|
|
'siteId' => $siteId |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($sortOrder) { |
|
70
|
|
|
$record->sortOrder = $sortOrder; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->runInternal($record); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
* @param Association $record |
|
79
|
|
|
* @throws \Exception |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function performAction(Association $record): bool |
|
82
|
|
|
{ |
|
83
|
|
|
return $record->save(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.