Pop\Db\Record\Prepared::save PHP Метод

save() публичный Метод

Save the database record.
public save ( array $columnsPassed, integer $type = Pop\Db\Record::INSERT ) : void
$columnsPassed array
$type integer
Результат void
    public function save($columnsPassed, $type = \Pop\Db\Record::INSERT)
    {
        $this->columns = $columnsPassed;
        if (null === $this->primaryId) {
            if ($type == \Pop\Db\Record::UPDATE) {
                if (count($this->finder) > 0) {
                    $columns = array();
                    $params = $this->columns;
                    $i = 1;
                    foreach ($this->columns as $key => $value) {
                        if (!array_key_exists($key, $this->finder)) {
                            $columns[$key] = $this->getPlaceholder($key, $i);
                            $i++;
                        }
                    }
                    foreach ($this->finder as $key => $value) {
                        if (isset($params[$key])) {
                            $val = $params[$key];
                            unset($params[$key]);
                            $params[$key] = $val;
                        }
                    }
                    $this->sql()->update((array) $columns);
                    $this->sql->update()->where(true);
                    $i = 1;
                    foreach ($this->finder as $key => $value) {
                        if (null === $value) {
                            $this->sql()->update()->where()->isNull($key);
                        } else {
                            $this->sql()->update()->where()->equalTo($key, $this->getPlaceholder($key, $i));
                            $i++;
                        }
                    }
                    $realParams = array();
                    foreach ($params as $key => $value) {
                        if (null !== $value) {
                            $realParams[$key] = $value;
                        }
                    }
                    $this->sql->adapter()->prepare($this->sql->render(true));
                    $this->sql->adapter()->bindParams($realParams);
                } else {
                    $columns = array();
                    $i = 1;
                    foreach ($this->columns as $key => $value) {
                        $columns[$key] = $this->getPlaceholder($key, $i);
                        $i++;
                    }
                    $this->sql()->update((array) $columns);
                    $this->sql->adapter()->prepare($this->sql->render(true));
                    $this->sql->adapter()->bindParams((array) $this->columns);
                }
                // Execute the SQL statement
                $this->sql->adapter()->execute();
            } else {
                $columns = array();
                $i = 1;
                foreach ($this->columns as $key => $value) {
                    $columns[$key] = $this->getPlaceholder($key, $i);
                    $i++;
                }
                $this->sql->insert((array) $columns);
                $this->sql->adapter()->prepare($this->sql->render(true));
                $this->sql->adapter()->bindParams((array) $this->columns);
                $this->sql->adapter()->execute();
            }
        } else {
            if ($this->auto == false) {
                $action = $type == \Pop\Db\Record::INSERT ? 'insert' : 'update';
            } else {
                if (is_array($this->primaryId)) {
                    $isset = true;
                    foreach ($this->primaryId as $value) {
                        if (!isset($this->columns[$value])) {
                            $isset = false;
                        }
                    }
                    $action = $isset ? 'update' : 'insert';
                } else {
                    $action = isset($this->columns[$this->primaryId]) ? 'update' : 'insert';
                }
            }
            if ($action == 'update') {
                $columns = array();
                $params = $this->columns;
                $i = 1;
                foreach ($this->columns as $key => $value) {
                    if (is_array($this->primaryId)) {
                        if (!in_array($key, $this->primaryId)) {
                            $columns[$key] = $this->getPlaceholder($key, $i);
                            $i++;
                        }
                    } else {
                        if ($key != $this->primaryId) {
                            $columns[$key] = $this->getPlaceholder($key, $i);
                            $i++;
                        }
                    }
                }
                $this->sql->update((array) $columns);
                $this->sql->update()->where(true);
                if (is_array($this->primaryId)) {
                    foreach ($this->primaryId as $key => $value) {
                        if (isset($params[$value])) {
                            $id = $params[$value];
                            unset($params[$value]);
                        } else {
                            $id = $params[$value];
                        }
                        $params[$value] = $id;
                        if (null === $this->columns[$value]) {
                            $this->sql->update()->where()->isNull($value);
                            unset($params[$value]);
                        } else {
                            $this->sql->update()->where()->equalTo($value, $this->getPlaceholder($value, $i + $key));
                        }
                    }
                    $realParams = $params;
                } else {
                    if (isset($params[$this->primaryId])) {
                        $id = $params[$this->primaryId];
                        unset($params[$this->primaryId]);
                    } else {
                        $id = $params[$this->primaryId];
                    }
                    $params[$this->primaryId] = $id;
                    $this->sql()->update()->where()->equalTo($this->primaryId, $this->getPlaceholder($this->primaryId, $i));
                    $realParams = $params;
                }
                $this->sql->adapter()->prepare($this->sql->render(true));
                $this->sql->adapter()->bindParams((array) $realParams);
                $this->sql->adapter()->execute();
            } else {
                $columns = array();
                $i = 1;
                foreach ($this->columns as $key => $value) {
                    $columns[$key] = $this->getPlaceholder($key, $i);
                    $i++;
                }
                $this->sql->insert((array) $columns);
                $this->sql->adapter()->prepare($this->sql->render(true));
                $this->sql->adapter()->bindParams((array) $this->columns);
                $this->sql->adapter()->execute();
                if ($this->auto) {
                    $this->columns[$this->primaryId] = $this->sql->adapter()->lastId();
                    $this->rows[0][$this->primaryId] = $this->sql->adapter()->lastId();
                }
            }
        }
        if (count($this->columns) > 0) {
            $this->rows[0] = $this->columns;
        }
    }