Example #1
0
 /**
  * (non-PHPdoc)
  * @see BaseEntityAbstract::preSave()
  */
 public function preSave()
 {
     if (intVal($this->getActive()) === 1) {
         $sku = trim($this->getSku());
         $where = array('sku = ?');
         $params = array($sku);
         if (($id = trim($this->getId())) !== '') {
             $where[] = 'id != ?';
             $params[] = $id;
         }
         $exsitingSKU = self::countByCriteria(implode(' AND ', $where), $params);
         if ($exsitingSKU > 0) {
             throw new EntityException('The SKU(=' . $sku . ') is already exists!');
         }
         if ($this->attributeSet instanceof ProductAttributeSet && $this->attributeSet->getId() === '' || !$this->attributeSet instanceof ProductAttributeSet) {
             $this->setAttributeSet(ProductAttributeSet::get(ProductAttributeSet::ID_DEFAULT_ATTR_SET));
         }
     }
     if (($id = trim($this->getId())) !== '') {
         if (self::countByCriteria('id = ? and isKit = 1 and isKit != ?', array($id, $this->getIsKit())) > 0) {
             //changing isKit flag to be not a KIT
             if (count($kits = Kit::getAllByCriteria('productId = ?', array($id), true, 1, 1)) > 0) {
                 throw new EntityException('Can NOT change the flag IsKit, as there are kits like [' . $kits[0]->getBarcode() . '] for this product: ' . $this->getSku());
             }
         }
     }
 }