public function save()
 {
     $db_handler = Suricate::Database(true);
     if ($this->parent_id != '') {
         // 1st step : delete all records for current parent_id
         $sql = "DELETE FROM `" . static::SQL_RELATION_TABLE_NAME . "`";
         $sql .= " WHERE";
         $sql .= "   " . static::PARENT_ID_NAME . "=:parent_id";
         $sqlParams = array();
         $sqlParams['parent_id'] = $this->parent_id;
         //echo "--> delete old items with : $sql<br/>";
         $db_handler->query($sql, $sqlParams);
         // 2nd step : create items that are not saved in db
         foreach ($this->items as &$current_item) {
             if ($current_item->{$current_item::TABLE_INDEX} == '') {
                 // 2nd step : create items that are not saved in db
                 //echo "Item missing id, saving id<br/>";
                 /*foreach ($this->additionalMappingFieldList as $additionalField) {
                       $current_item->$additionalField
                   }*/
                 $current_item->save();
             }
             //3rd step : create the mapping
             $sqlParams = array();
             $sql = "INSERT INTO `" . static::SQL_RELATION_TABLE_NAME . "`";
             $sql .= " (`" . static::PARENT_ID_NAME . "`, `" . static::MAPPING_ID_NAME . "`";
             if (count($this->additionalMappingFieldList)) {
                 $sql .= ', ' . implode(",", array_map(function ($s) {
                     return '`' . $s . '`';
                 }, $this->additionalMappingFieldList));
             }
             $sql .= ")";
             $sql .= " VALUES";
             $sql .= "(:parent_id, :id";
             if (count($this->additionalMappingFieldList)) {
                 foreach ($this->additionalMappingFieldList as $additionalField) {
                     $sql .= ',:' . $additionalField;
                     $sqlParams[$additionalField] = $current_item->{$additionalField};
                 }
             }
             $sql .= ")";
             $sqlParams['parent_id'] = $this->parent_id;
             $sqlParams['id'] = $current_item->id;
             $db_handler->query($sql, $sqlParams);
         }
     }
 }
Example #2
0
 public function save()
 {
     // 1st step : delete all records for current parentId
     $sql = "DELETE FROM `" . static::TABLE_NAME . "`";
     if (static::PARENT_ID_NAME != '') {
         $sql .= " WHERE";
         $sql .= "   " . static::PARENT_ID_NAME . "=:parent_id";
         $sqlParams = array('parent_id' => $this->parentId);
     } else {
         $sqlParams = array();
     }
     Suricate::Database()->query($sql, $sqlParams);
     // 2nd step : save all current items
     foreach ($this->items as $currentItem) {
         $currentItem->save(true);
         // Force insert
     }
 }
Example #3
0
 protected function connectDB()
 {
     if (!$this->dbLink) {
         $this->dbLink = Suricate::Database();
         if (static::DB_CONFIG != '') {
             $this->dbLink->setConfig(static::DB_CONFIG);
         }
     }
 }