vendor/pimcore/pimcore/models/DataObject/ClassDefinition/Data/ReverseObjectRelation.php line 115

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\DataObject\ClassDefinition\Data;
  15. use Pimcore\Db;
  16. use Pimcore\Logger;
  17. use Pimcore\Model;
  18. use Pimcore\Model\DataObject;
  19. class ReverseObjectRelation extends ManyToManyObjectRelation
  20. {
  21.     /**
  22.      * Static type of this element
  23.      *
  24.      * @internal
  25.      *
  26.      * @var string
  27.      */
  28.     public $fieldtype 'reverseObjectRelation';
  29.     /**
  30.      * @internal
  31.      *
  32.      * @var string
  33.      */
  34.     public $ownerClassName;
  35.     /**
  36.      * @internal
  37.      *
  38.      * @var string|null
  39.      */
  40.     public $ownerClassId;
  41.     /**
  42.      * @internal
  43.      *
  44.      * @var string
  45.      */
  46.     public $ownerFieldName;
  47.     /**
  48.      * ReverseObjectRelation must be lazy loading!
  49.      *
  50.      * @internal
  51.      *
  52.      * @var bool
  53.      */
  54.     public $lazyLoading true;
  55.     /**
  56.      * @param array $classes
  57.      *
  58.      * @return $this
  59.      */
  60.     public function setClasses($classes)
  61.     {
  62.         //dummy, classes are set from owner classId
  63.         return $this;
  64.     }
  65.     /**
  66.      * @param string $ownerClassName
  67.      *
  68.      * @return $this
  69.      */
  70.     public function setOwnerClassName($ownerClassName)
  71.     {
  72.         $this->ownerClassName $ownerClassName;
  73.         return $this;
  74.     }
  75.     /**
  76.      * @return string|null
  77.      */
  78.     public function getOwnerClassName()
  79.     {
  80.         //fallback for legacy data
  81.         if (empty($this->ownerClassName) && $this->ownerClassId) {
  82.             try {
  83.                 if (empty($this->ownerClassId)) {
  84.                     return null;
  85.                 }
  86.                 $class DataObject\ClassDefinition::getById($this->ownerClassId);
  87.                 $this->ownerClassName $class->getName();
  88.             } catch (\Exception $e) {
  89.                 Logger::error($e->getMessage());
  90.             }
  91.         }
  92.         return $this->ownerClassName;
  93.     }
  94.     /**
  95.      * @return string|null
  96.      */
  97.     public function getOwnerClassId()
  98.     {
  99.         if (empty($this->ownerClassId)) {
  100.             try {
  101.                 $class DataObject\ClassDefinition::getByName($this->ownerClassName);
  102.                 if (!$class instanceof DataObject\ClassDefinition) {
  103.                     Logger::error('Reverse relation '.$this->getName().' has no owner class assigned');
  104.                     return null;
  105.                 }
  106.                 $this->ownerClassId $class->getId();
  107.             } catch (\Exception $e) {
  108.                 Logger::error($e->getMessage());
  109.             }
  110.         }
  111.         return $this->ownerClassId;
  112.     }
  113.     /**
  114.      * @return string
  115.      */
  116.     public function getOwnerFieldName()
  117.     {
  118.         return $this->ownerFieldName;
  119.     }
  120.     /**
  121.      * @param  string $fieldName
  122.      *
  123.      * @return $this
  124.      */
  125.     public function setOwnerFieldName($fieldName)
  126.     {
  127.         $this->ownerFieldName $fieldName;
  128.         return $this;
  129.     }
  130.     /**
  131.      * {@inheritdoc}
  132.      */
  133.     protected function allowObjectRelation($object)
  134.     {
  135.         //only relations of owner type are allowed
  136.         $ownerClass DataObject\ClassDefinition::getByName($this->getOwnerClassName());
  137.         if ($ownerClass instanceof DataObject\ClassDefinition && $object instanceof DataObject\Concrete && $ownerClass->getId() == $object->getClassId()) {
  138.             $fd $ownerClass->getFieldDefinition($this->getOwnerFieldName());
  139.             if ($fd instanceof DataObject\ClassDefinition\Data\Relations\AbstractRelations) {
  140.                 return true;
  141.             }
  142.         }
  143.         return false;
  144.     }
  145.     /**
  146.      * {@inheritdoc}
  147.      */
  148.     public function checkValidity($data$omitMandatoryCheck false$params = [])
  149.     {
  150.         //TODO
  151.         if (!$omitMandatoryCheck && $this->getMandatory() && empty($data)) {
  152.             throw new Model\Element\ValidationException('Empty mandatory field [ '.$this->getName().' ]');
  153.         }
  154.         if (is_array($data)) {
  155.             foreach ($data as $o) {
  156.                 $allowClass $this->allowObjectRelation($o);
  157.                 if (!$allowClass || !($o instanceof DataObject\Concrete)) {
  158.                     throw new Model\Element\ValidationException('Invalid non owner object relation to object ['.$o->getId().']');
  159.                 }
  160.             }
  161.         }
  162.     }
  163.     /**
  164.      * @param DataObject\Concrete $object
  165.      * @param array $params
  166.      *
  167.      * @return array
  168.      */
  169.     public function load($object$params = [])
  170.     {
  171.         if ($this->getOwnerClassId() === null) {
  172.             return [];
  173.         }
  174.         $db Db::get();
  175.         $relations $db->fetchAllAssociative('SELECT * FROM object_relations_'.$this->getOwnerClassId()." WHERE dest_id = ? AND fieldname = ? AND ownertype = 'object'", [$object->getId(), $this->getOwnerFieldName()]);
  176.         $relations array_map(static function ($relation) {
  177.             $relation['dest_id'] = $relation['src_id'];
  178.             unset($relation['src_id']);
  179.             return $relation;
  180.         }, $relations);
  181.         $data $this->loadData($relations$object$params);
  182.         if ($object instanceof Model\Element\DirtyIndicatorInterface) {
  183.             $object->markFieldDirty($this->getName(), false);
  184.         }
  185.         return $data['data'];
  186.     }
  187.     /**
  188.      * {@inheritdoc}
  189.      */
  190.     public function getCacheTags($data, array $tags = [])
  191.     {
  192.         return $tags;
  193.     }
  194.     /**
  195.      * @param mixed $data
  196.      *
  197.      * @return array
  198.      */
  199.     public function resolveDependencies($data)
  200.     {
  201.         return [];
  202.     }
  203.     /**
  204.      * {@inheritdoc}
  205.      */
  206.     public function isOptimizedAdminLoading(): bool
  207.     {
  208.         return true;
  209.     }
  210.     /**
  211.      * {@inheritdoc}
  212.      */
  213.     public function preGetData($container$params = [])
  214.     {
  215.         return $this->load($container);
  216.     }
  217.     /**
  218.      * @return false
  219.      */
  220.     public function supportsInheritance()
  221.     {
  222.         return false;
  223.     }
  224. }
  225. //TODO remove in Pimcore 11
  226. class_alias(ReverseObjectRelation::class, 'Pimcore\Model\DataObject\ClassDefinition\Data\ReverseManyToManyObjectRelation');