vendor/dachcom-digital/members/src/MembersBundle/Restriction/Dao.php line 53

Open in your IDE?
  1. <?php
  2. namespace MembersBundle\Restriction;
  3. use Pimcore\Model;
  4. class Dao extends Model\Dao\AbstractDao
  5. {
  6.     /**
  7.      * @var string
  8.      */
  9.     protected $tableName 'members_restrictions';
  10.     /**
  11.      * @var string
  12.      */
  13.     protected $tableRelationName 'members_group_relations';
  14.     /**
  15.      * @var Restriction
  16.      */
  17.     protected $model;
  18.     /**
  19.      * @param int $id
  20.      *
  21.      * @throws \Exception
  22.      */
  23.     public function getById($id)
  24.     {
  25.         $data $this->db->fetchRow('SELECT * FROM ' $this->tableName ' WHERE id  = ?', [$id]);
  26.         if ($data === false) {
  27.             throw new \Exception('Object with the ID ' $id ' doesn\'t exists');
  28.         } else {
  29.             $data $this->addRelationData($data);
  30.         }
  31.         $this->assignVariablesToModel($data);
  32.     }
  33.     /**
  34.      * @param string $field
  35.      * @param null   $value
  36.      * @param string $cType
  37.      *
  38.      * @throws \Exception
  39.      */
  40.     public function getByField($field$value null$cType 'page')
  41.     {
  42.         $data $this->db->fetchRow('SELECT * FROM ' $this->tableName ' WHERE ' $field ' = ? AND ctype = ?', [
  43.             $value,
  44.             $cType
  45.         ]);
  46.         if ($data === false) {
  47.             throw new \Exception('Object (Type: ' $cType ') with the ' $field ' ' $value ' doesn\'t exists');
  48.         } else {
  49.             $data $this->addRelationData($data);
  50.         }
  51.         $this->assignVariablesToModel($data);
  52.     }
  53.     /**
  54.      * @return bool
  55.      *
  56.      * @throws \Doctrine\DBAL\DBALException
  57.      * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
  58.      */
  59.     public function save()
  60.     {
  61.         $saveData = [
  62.             'targetId'    => $this->model->getTargetId(),
  63.             'ctype'       => $this->model->getCtype(),
  64.             'isInherited' => (int) $this->model->isInherited(),
  65.             'inherit'     => (int) $this->model->getInherit()
  66.         ];
  67.         if ($this->model->getId() !== null) {
  68.             $this->db->update($this->tableName$saveData, ['id' => $this->model->getId()]);
  69.         } else {
  70.             $this->db->insert($this->tableName$saveData);
  71.             $this->model->setId($this->db->lastInsertId());
  72.         }
  73.         $this->saveRelations();
  74.         return true;
  75.     }
  76.     /**
  77.      * @param array $data
  78.      *
  79.      * @return mixed
  80.      */
  81.     private function addRelationData($data)
  82.     {
  83.         $relations $this->db->fetchAll('SELECT * FROM ' $this->tableRelationName ' WHERE restrictionId  = ?', [$data['id']]);
  84.         if ($relations !== false) {
  85.             foreach ($relations as $relation) {
  86.                 $data['relatedGroups'][] = $relation['groupId'];
  87.             }
  88.         }
  89.         return $data;
  90.     }
  91.     /**
  92.      * @return bool
  93.      *
  94.      * @throws \Doctrine\DBAL\DBALException
  95.      * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
  96.      */
  97.     public function saveRelations()
  98.     {
  99.         $groups $this->model->getRelatedGroups();
  100.         //first, delete all!
  101.         $this->db->delete($this->tableRelationName, ['restrictionId' => $this->model->getId()]);
  102.         //set related Groups
  103.         if (empty($groups)) {
  104.             return false;
  105.         }
  106.         foreach ($groups as $groupId) {
  107.             $saveData = [
  108.                 'restrictionId' => $this->model->getId(),
  109.                 'groupId'       => (int) $groupId,
  110.             ];
  111.             $this->db->insert($this->tableRelationName$saveData);
  112.         }
  113.         return true;
  114.     }
  115.     /**
  116.      * @throws \Doctrine\DBAL\DBALException
  117.      * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
  118.      */
  119.     public function delete()
  120.     {
  121.         $this->db->delete($this->tableName, ['id' => $this->model->getId()]);
  122.         $this->db->delete($this->tableRelationName, ['restrictionId' => $this->model->getId()]);
  123.     }
  124. }