1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
<?php
namespace BEdita\Core\Model\Action;
use ArrayObject;
use BEdita\Core\Exception\InvalidDataException;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Association\HasOne;
use InvalidArgumentException;
class SetAssociatedAction extends UpdateAssociatedAction
{
use AssociatedTrait;
protected function update(EntityInterface $entity, $relatedEntities)
{
if ($this->Association instanceof BelongsToMany || $this->Association instanceof HasMany) {
if ($relatedEntities === null) {
$relatedEntities = [];
} elseif (!is_array($relatedEntities)) {
$relatedEntities = [$relatedEntities];
}
$res = $this->toMany($entity, $relatedEntities);
foreach ($relatedEntities as $relatedEntity) {
if (
$relatedEntity->has('_joinData') &&
($relatedEntity->get('_joinData') instanceof EntityInterface) &&
$relatedEntity->get('_joinData')->getErrors()
) {
throw new InvalidDataException(
__d('bedita', 'Error linking entities'),
(array)$relatedEntity->get('_joinData')->getErrors()
);
}
}
return $res;
}
if ($relatedEntities === []) {
$relatedEntities = null;
}
if ($relatedEntities !== null && !($relatedEntities instanceof EntityInterface)) {
throw new InvalidArgumentException(__d('bedita', 'Unable to link multiple entities'));
}
if ($this->Association instanceof BelongsTo) {
return $this->Association->getConnection()->transactional(function () use ($entity, $relatedEntities) {
return $this->belongsTo($entity, $relatedEntities);
});
}
if ($this->Association instanceof HasOne) {
return $this->Association->getConnection()->transactional(function () use ($entity, $relatedEntities) {
return $this->hasOne($entity, $relatedEntities);
});
}
throw new \RuntimeException(__d('bedita', 'Unknown association of type "{0}"', get_class($this->Association)));
}
protected function toMany(EntityInterface $entity, array $relatedEntities)
{
$relatedEntities = new ArrayObject($relatedEntities);
$this->dispatchEvent('Associated.beforeSave', compact('entity', 'relatedEntities') + ['action' => 'set', 'association' => $this->Association]);
$relatedEntities = $this->diff($entity, $relatedEntities->getArrayCopy(), true, $affectedEntities);
$count = count($affectedEntities);
if ($this->Association instanceof HasMany) {
if ($this->Association->replace($entity, $relatedEntities, ['atomic' => false]) === false) {
return false;
}
$this->dispatchEvent('Associated.afterSave', compact('entity', 'relatedEntities') + ['action' => 'set', 'association' => $this->Association]);
return $count;
}
if ($this->Association instanceof BelongsToMany) {
if ($this->Association->replaceLinks($entity, $relatedEntities, ['atomic' => false]) === false) {
return false;
}
$this->dispatchEvent('Associated.afterSave', compact('entity', 'relatedEntities') + ['action' => 'set', 'association' => $this->Association]);
return $count;
}
return false;
}
protected function belongsTo(EntityInterface $entity, ?EntityInterface $relatedEntity = null)
{
$dirty = $entity->isDirty();
$existing = $this->existing($entity);
$relatedEntities = new ArrayObject([$relatedEntity]);
$this->dispatchEvent('Associated.beforeSave', compact('entity', 'relatedEntities') + ['action' => 'set', 'association' => $this->Association]);
$relatedEntity = $relatedEntities[0];
if ($existing === null && $relatedEntity === null) {
return 0;
} elseif (!$dirty && $relatedEntity !== null) {
$bindingKey = (array)$this->Association->getBindingKey();
if ($existing !== null && $relatedEntity->extract($bindingKey) == $existing->extract($bindingKey)) {
return 0;
}
}
$entity->set($this->Association->getProperty(), $relatedEntity);
if ($this->Association->getSource()->save($entity) === false) {
return false;
}
$relatedEntities = [$relatedEntity];
$this->dispatchEvent('Associated.afterSave', compact('entity', 'relatedEntities') + ['action' => 'set', 'association' => $this->Association]);
return 1;
}
protected function hasOne(EntityInterface $entity, ?EntityInterface $relatedEntity = null)
{
$foreignKey = (array)$this->Association->getForeignKey();
$bindingKeyValue = $entity->extract((array)$this->Association->getBindingKey());
$existing = $this->existing($entity);
$relatedEntities = new ArrayObject([$relatedEntity]);
$this->dispatchEvent('Associated.beforeSave', compact('entity', 'relatedEntities') + ['action' => 'set', 'association' => $this->Association]);
$relatedEntity = $relatedEntities[0];
if ($existing === null && $relatedEntity === null) {
return 0;
}
if ($relatedEntity !== null) {
$primaryKey = (array)$this->Association->getPrimaryKey();
if ($relatedEntity->extract($primaryKey) == $existing->extract($primaryKey)) {
return 0;
}
}
$this->Association->getTarget()->updateAll(
array_combine(
$foreignKey,
array_fill(0, count($foreignKey), null)
),
array_combine(
$foreignKey,
$bindingKeyValue
)
);
if ($relatedEntity === null) {
return 0;
}
$relatedEntity->set(array_combine(
$foreignKey,
$bindingKeyValue
));
if ($this->Association->getTarget()->save($relatedEntity) === false) {
return false;
}
$relatedEntities = [$relatedEntity];
$this->dispatchEvent('Associated.afterSave', compact('entity', 'relatedEntities') + ['action' => 'set', 'association' => $this->Association]);
return 1;
}
}