vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/PricingManager/Rule/Dao.php line 63

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\Bundle\EcommerceFrameworkBundle\PricingManager\Rule;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Rule;
  16. use Pimcore\Model\Dao\AbstractDao;
  17. use Pimcore\Model\Exception\NotFoundException;
  18. /**
  19.  * @internal
  20.  *
  21.  * @property Rule $model
  22.  */
  23. class Dao extends AbstractDao
  24. {
  25.     const TABLE_NAME 'ecommerceframework_pricing_rule';
  26.     /**
  27.      * Contains all valid columns in the database table
  28.      *
  29.      * @var array
  30.      */
  31.     protected $validColumns = [];
  32.     /**
  33.      * @var array
  34.      */
  35.     protected $fieldsToSave = ['name''label''description''behavior''active''prio''condition''actions'];
  36.     /**
  37.      * @var array
  38.      */
  39.     protected $localizedFields = ['label''description'];
  40.     /**
  41.      * Get the valid columns from the database
  42.      *
  43.      * @return void
  44.      */
  45.     public function init()
  46.     {
  47.         $this->validColumns $this->getValidTableColumns(self::TABLE_NAME);
  48.     }
  49.     /**
  50.      * @param int $id
  51.      *
  52.      * @throws NotFoundException
  53.      */
  54.     public function getById($id)
  55.     {
  56.         $classRaw $this->db->fetchAssociative('SELECT * FROM ' self::TABLE_NAME ' WHERE id=' $this->db->quote($id));
  57.         if (empty($classRaw)) {
  58.             throw new NotFoundException('pricing rule ' $id ' not found.');
  59.         }
  60.         $this->assignVariablesToModel($classRaw);
  61.     }
  62.     /**
  63.      * Create a new record for the object in database
  64.      */
  65.     public function create()
  66.     {
  67.         $this->db->insert(self::TABLE_NAME, []);
  68.         $this->model->setId((int) $this->db->lastInsertId());
  69.     }
  70.     /**
  71.      * Save object to database
  72.      *
  73.      * @return void
  74.      */
  75.     public function save()
  76.     {
  77.         if (!$this->model->getId()) {
  78.             $this->create();
  79.         }
  80.         $this->update();
  81.     }
  82.     /**
  83.      * @return void
  84.      */
  85.     public function update()
  86.     {
  87.         $data = [];
  88.         foreach ($this->fieldsToSave as $field) {
  89.             if (in_array($field$this->validColumns)) {
  90.                 $getter 'get' ucfirst($field);
  91.                 if (in_array($field$this->localizedFields)) {
  92.                     // handle localized Fields
  93.                     $localizedValues = [];
  94.                     foreach (\Pimcore\Tool::getValidLanguages() as $lang) {
  95.                         $localizedValues[$lang] = $value $this->model->$getter($lang);
  96.                     }
  97.                     $value $localizedValues;
  98.                 } else {
  99.                     // normal case
  100.                     $value $this->model->$getter();
  101.                 }
  102.                 if (is_array($value) || is_object($value)) {
  103.                     $value serialize($value);
  104.                 } elseif (is_bool($value)) {
  105.                     $value = (int)$value;
  106.                 }
  107.                 $data[$field] = $value;
  108.             }
  109.         }
  110.         $this->db->update(self::TABLE_NAME$data, ['id' => $this->model->getId()]);
  111.     }
  112.     /**
  113.      * Deletes object from database
  114.      *
  115.      * @return void
  116.      */
  117.     public function delete()
  118.     {
  119.         $this->db->delete(self::TABLE_NAME, ['id' => $this->model->getId()]);
  120.     }
  121.     /**
  122.      * @param array $fields
  123.      */
  124.     public function setFieldsToSave(array $fields)
  125.     {
  126.         $this->fieldsToSave $fields;
  127.     }
  128. }