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
<?php
namespace BEdita\API\Model\Action;
use BEdita\Core\Model\Action\BaseAction;
use Cake\Database\Expression\QueryExpression;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\ORM\Association;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
use Cake\ORM\Association\HasOne;
use Cake\Utility\Hash;
class UpdateAssociatedAction extends BaseAction
{
protected $Action;
protected $request;
protected function initialize(array $data)
{
$this->Action = $this->getConfig('action');
$this->request = $this->getConfig('request');
}
public function execute(array $data = [])
{
$association = $this->Action->getConfig('association');
if (!($association instanceof Association)) {
throw new \LogicException(__d('bedita', 'Unknown association type'));
}
$entity = $association->getSource()->get($data['primaryKey']);
$requestData = $this->request->getData();
if (!Hash::numeric(array_keys($requestData))) {
$requestData = [$requestData];
}
$relatedEntities = $this->getTargetEntities($requestData, $association);
$count = count($relatedEntities);
if ($count === 0) {
$relatedEntities = [];
} elseif ($count === 1 && ($association instanceof BelongsTo || $association instanceof HasOne)) {
$relatedEntities = reset($relatedEntities);
}
return $this->Action->execute(compact('entity', 'relatedEntities'));
}
protected function getTargetEntities(array $data, Association $association)
{
$target = $association->getTarget();
$primaryKeyField = $target->getPrimaryKey();
$targetPKField = $target->aliasField($primaryKeyField);
$targetPrimaryKeys = array_unique(Hash::extract($data, '{*}.id'));
if (empty($targetPrimaryKeys)) {
return [];
}
$targetEntities = $target->find()
->where(function (QueryExpression $exp) use ($targetPKField, $targetPrimaryKeys) {
return $exp->in($targetPKField, $targetPrimaryKeys);
});
$targetEntities = $targetEntities->all()->indexBy($primaryKeyField)->toArray();
uksort(
$targetEntities,
function ($a, $b) use ($targetPrimaryKeys) {
return array_search($a, $targetPrimaryKeys) - array_search($b, $targetPrimaryKeys);
}
);
foreach ($data as $datum) {
$id = Hash::get($datum, 'id');
$type = Hash::get($datum, 'type');
if (!isset($targetEntities[$id]) || ($targetEntities[$id]->has('type') && $targetEntities[$id]->get('type') !== $type)) {
throw new RecordNotFoundException(__('Record not found in table "{0}"', $type ?: $target->getTable()));
}
$meta = Hash::get($datum, '_meta.relation');
if (!$this->request->is('delete') && $association instanceof BelongsToMany && $meta !== null) {
$targetEntities[$id]->_joinData = $meta;
}
}
return $targetEntities;
}
}