/**
  * create new device
  *
  * @param Syncroton_Model_IDevice $_device
  * @return Syncroton_Model_IDevice
  */
 public function create($model)
 {
     if (!$model instanceof $this->_modelInterfaceName) {
         throw new InvalidArgumentException('$model must be instanace of ' . $this->_modelInterfaceName);
     }
     $data = $this->_convertModelToArray($model);
     $data['id'] = sha1(mt_rand() . microtime());
     //$this->_db->insert($this->_tablePrefix . $this->_tableName, $data);
     $this->_db->insertMulti($this->_tablePrefix . $this->_tableName, array_keys($data), array(0 => $data));
     return $this->get($data['id']);
 }
Exemple #2
0
 /**
  * Saves model hasAndBelongsToMany data to the database.
  *
  * @param array $joined Data to save
  * @param mixed $id ID of record in this model
  * @param DataSource $db
  * @return void
  */
 protected function _saveMulti($joined, $id, $db)
 {
     foreach ($joined as $assoc => $data) {
         if (isset($this->hasAndBelongsToMany[$assoc])) {
             list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']);
             $keyInfo = $this->{$join}->schema($this->{$join}->primaryKey);
             $isUUID = !empty($this->{$join}->primaryKey) && ($keyInfo['length'] == 36 && ($keyInfo['type'] === 'string' || $keyInfo['type'] === 'binary'));
             $newData = $newValues = array();
             $primaryAdded = false;
             $fields = array($db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']), $db->name($this->hasAndBelongsToMany[$assoc]['associationForeignKey']));
             $idField = $db->name($this->{$join}->primaryKey);
             if ($isUUID && !in_array($idField, $fields)) {
                 $fields[] = $idField;
                 $primaryAdded = true;
             }
             foreach ((array) $data as $row) {
                 if (is_string($row) && (strlen($row) == 36 || strlen($row) == 16) || is_numeric($row)) {
                     $values = array($id, $row);
                     if ($isUUID && $primaryAdded) {
                         $values[] = String::uuid();
                     }
                     $newValues[] = $values;
                     unset($values);
                 } elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
                     $newData[] = $row;
                 } elseif (isset($row[$join]) && isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
                     $newData[] = $row[$join];
                 }
             }
             if ($this->hasAndBelongsToMany[$assoc]['unique']) {
                 $conditions = array($join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id);
                 if (!empty($this->hasAndBelongsToMany[$assoc]['conditions'])) {
                     $conditions = array_merge($conditions, (array) $this->hasAndBelongsToMany[$assoc]['conditions']);
                 }
                 $links = $this->{$join}->find('all', array('conditions' => $conditions, 'recursive' => empty($this->hasAndBelongsToMany[$assoc]['conditions']) ? -1 : 0, 'fields' => $this->hasAndBelongsToMany[$assoc]['associationForeignKey']));
                 $associationForeignKey = "{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
                 $oldLinks = Set::extract($links, "{n}.{$associationForeignKey}");
                 if (!empty($oldLinks)) {
                     $conditions[$associationForeignKey] = $oldLinks;
                     $db->delete($this->{$join}, $conditions);
                 }
             }
             if (!empty($newData)) {
                 foreach ($newData as $data) {
                     $data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
                     $this->{$join}->create($data);
                     $this->{$join}->save();
                 }
             }
             if (!empty($newValues)) {
                 $db->insertMulti($this->{$join}, $fields, $newValues);
             }
         }
     }
 }