vendor/pimcore/output-data-config-toolkit-bundle/src/OutputDefinition/Dao.php line 47

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 OutputDataConfigToolkitBundle\OutputDefinition;
  15. use OutputDataConfigToolkitBundle\OutputDefinition;
  16. class Dao extends \Pimcore\Model\Dao\AbstractDao
  17. {
  18.     const TABLE_NAME 'bundle_outputdataconfigtoolkit_outputdefinition';
  19.     /**
  20.      * Contains all valid columns in the database table
  21.      *
  22.      * @var array
  23.      */
  24.     protected $validColumns = [];
  25.     /**
  26.      * Get the valid columns from the database
  27.      *
  28.      * @return void
  29.      */
  30.     public function init()
  31.     {
  32.         $this->validColumns $this->getValidTableColumns(self::TABLE_NAME);
  33.     }
  34.     /**
  35.      * @return void
  36.      *
  37.      * @throws \Doctrine\DBAL\DBALException
  38.      * @throws \Exception
  39.      */
  40.     public function getByO_IdClassIdChannel($o_id$classId$channel)
  41.     {
  42.         $outputDefinitionRaw $this->db->fetchRow('SELECT * FROM ' self::TABLE_NAME ' WHERE o_id=? AND o_classId = ? AND ' $this->db->quoteIdentifier('channel') . ' = ?', [$o_id$classId$channel]);
  43.         if (empty($outputDefinitionRaw)) {
  44.             throw new \Exception('OutputDefinition ' $o_id ' - ' $classId  ' - ' $channel ' not found.');
  45.         }
  46.         $this->assignVariablesToModel($outputDefinitionRaw);
  47.     }
  48.     /**
  49.      * @return void
  50.      *
  51.      * @throws \Doctrine\DBAL\DBALException
  52.      * @throws \Exception
  53.      */
  54.     public function getById($id)
  55.     {
  56.         $outputDefinitionRaw $this->db->fetchRow('SELECT * FROM ' self::TABLE_NAME ' WHERE id=?', [$id]);
  57.         if (empty($outputDefinitionRaw)) {
  58.             throw new \Exception('OutputDefinition-Id ' $id ' not found.');
  59.         }
  60.         $this->assignVariablesToModel($outputDefinitionRaw);
  61.     }
  62.     /**
  63.      * Create a new record for the object in database
  64.      *
  65.      * @return void
  66.      */
  67.     public function create()
  68.     {
  69.         $this->db->insert(self::TABLE_NAME, []);
  70.         $this->model->setId($this->db->lastInsertId());
  71.         $this->save();
  72.     }
  73.     /**
  74.      * Save object to database
  75.      *
  76.      * @return void
  77.      */
  78.     public function save()
  79.     {
  80.         $other OutputDefinition::getByO_IdClassIdChannel($this->model->getO_Id(), $this->model->getO_ClassId(), $this->model->getChannel());
  81.         if ($other) {
  82.             $this->model->setId($other->getId());
  83.         }
  84.         if ($this->model->getId()) {
  85.             return $this->update();
  86.         }
  87.         return $this->create();
  88.     }
  89.     /**
  90.      * @return void
  91.      */
  92.     public function update()
  93.     {
  94.         $class get_object_vars($this->model);
  95.         foreach ($class as $key => $value) {
  96.             if (in_array($key$this->validColumns)) {
  97.                 if (is_array($value) || is_object($value)) {
  98.                     $value serialize($value);
  99.                 } elseif (is_bool($value)) {
  100.                     $value = (int)$value;
  101.                 }
  102.                 $data[$key] = $value;
  103.             }
  104.         }
  105.         $this->db->updateWhere(self::TABLE_NAME$data'id=' $this->db->quote($this->model->getId()));
  106.     }
  107.     /**
  108.      * Deletes object from database
  109.      *
  110.      * @return void
  111.      */
  112.     public function delete()
  113.     {
  114.         $this->db->deleteWhere(self::TABLE_NAME'id=' $this->db->quote($this->model->getId()));
  115.     }
  116. }