Maslosoft Mangan API
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
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Decorators;
use InvalidArgumentException;
use Maslosoft\Mangan\Criteria;
use Maslosoft\Mangan\EntityManager;
use Maslosoft\Mangan\Finder;
use Maslosoft\Mangan\Helpers\PkManager;
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Sort;
/**
* RelatedRecorator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class RelatedDecorator implements DecoratorInterface
{
public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
{
$fieldMeta = ManganMeta::create($model)->field($name);
$relMeta = $fieldMeta->related;
$relModel = new $relMeta->class;
$criteria = new Criteria(null, $relModel);
if (empty($relMeta->join) && empty($relMeta->condition))
{
throw new InvalidArgumentException(sprintf('Parameter `join` or `condition` is required for Related annotation, model `%s`, field `%s`', get_class($model), $name));
}
if (!empty($relMeta->join))
{
foreach ($relMeta->join as $source => $rel)
{
assert($model->$source !== null);
$criteria->addCond($rel, '==', $model->$source);
}
}
if (!empty($relMeta->condition))
{
foreach ($relMeta->condition as $field => $value)
{
$criteria->addCond($field, '==', $value);
}
}
$criteria->setSort(new Sort($relMeta->sort));
if ($relMeta->single)
{
$model->$name = (new Finder($relModel))->find($criteria);
}
else
{
$model->$name = (new Finder($relModel))->findAll($criteria);
}
}
public function write($model, $name, &$dbValues, $transformatorClass = TransformatorInterface::class)
{
if (!empty($model->$name))
{
// Store empty field to trigger decorator read
$dbValues[$name] = null;
$fieldMeta = ManganMeta::create($model)->field($name);
$relMeta = $fieldMeta->related;
if ($relMeta->single)
{
$models = [
$model->$name
];
}
else
{
$models = $model->$name;
}
$order = 0;
foreach ($models as $relModel)
{
$fields = [];
if (!empty($relMeta->join))
{
foreach ($relMeta->join as $source => $rel)
{
$fields[] = $rel;
assert(isset($model->$source));
$relModel->$rel = $model->$source;
}
}
if (!empty($relMeta->condition))
{
foreach ($relMeta->condition as $field => $value)
{
$fields[] = $field;
$relModel->$field = $value;
}
}
if (!empty($relMeta->orderField))
{
$fields[] = $relMeta->orderField;
$fields = array_unique($fields);
$relModel->order = $order;
$order++;
}
$em = new EntityManager($relModel);
if ($relMeta->updatable)
{
// Update whole model
$em->upsert();
}
else
{
// Update only relation info
$criteria = PkManager::prepareFromModel($relModel);
$em->updateOne($criteria, $fields);
}
}
}
}
}
API documentation generated by ApiGen