protected function _set($key, $value) { parent::_set($key, $value); return $this; }
public function insert($pkValue = null, $skipBeforeInsert = false) { if (method_exists($this, 'beforeInsert') && $skipBeforeInsert === false) { $this->beforeInsert(); } $pk = $this->getTable()->getPK(); if (isset($pkValue)) { $this->set($pk, $pkValue); } else { $this->set($pk, null); } $sqlFields = []; $sqlValues = []; $values = []; // encryption foreach ($this->getEncryptedFields() as $field) { if ($this->has($field)) { parent::_set($field, $this->encrypt($field)); } } foreach ($this->getTable()->getFieldNames() as $field) { // Don't save a field is it's empty or not set if ($this->has($field) && $this->getTable()->getFieldProperties($field, 'type') === Ajde_Db::FIELD_TYPE_DATE && parent::_get($field) instanceof DateTime) { $sqlFields[] = $field; $sqlValues[] = '?'; $values[] = parent::_get($field)->format('Y-m-d h:i:s'); } else { if ($this->has($field) && !$this->isEmpty($field)) { if ($this->get($field) instanceof Ajde_Db_Function) { $sqlFields[] = $field; $sqlValues[] = (string) $this->get($field); } elseif ($this->getTable()->getFieldProperties($field, 'type') === Ajde_Db::FIELD_TYPE_SPATIAL) { $sqlFields[] = $field; $pointValues = explode(' ', (string) parent::_get($field)); $sqlValues[] = 'PointFromWKB(POINT(' . str_replace(',', '.', (double) $pointValues[0]) . ',' . str_replace(',', '.', (double) $pointValues[1]) . '))'; } else { $sqlFields[] = $field; $sqlValues[] = '?'; $value = parent::_get($field); if (is_float($value)) { $values[] = $value; } else { $values[] = (string) $value; } } } else { if ($this->has($field) && ($this->get($field) === 0 || $this->get($field) === '0')) { $sqlFields[] = $field; $sqlValues[] = '?'; $values[] = (string) parent::_get($field); } else { parent::_set($field, null); } } } } $sql = 'INSERT INTO ' . $this->_table . ' (' . implode(', ', $sqlFields) . ') VALUES (' . implode(', ', $sqlValues) . ')'; $statement = $this->getConnection()->prepare($sql); $return = $statement->execute($values); if (!isset($pkValue)) { $this->set($pk, $this->getConnection()->lastInsertId()); } if ($this->_hasMeta === true) { $this->saveMeta(); } if (method_exists($this, 'afterInsert')) { $this->afterInsert(); } return $return; }