/** * Crear registro * * @param array $data * @return boolean * @throw PDOException */ public function create(array $data = array()) { $this->dump($data); // Callback antes de crear if ($this->callback('_beforeCreate') === false) { return false; } $sql = QueryGenerator::insert($this, $data); if (!self::prepare($sql)->execute($data)) { return false; } // Verifica si la PK es autogenerada $pk = static::getPK(); if (!isset($this->{$pk})) { $this->{$pk} = QueryGenerator::query(static::getDriver(), 'last_insert_id', self::dbh(), $pk, static::getTable(), static::getSchema()); } // Callback despues de crear $this->callback('_afterCreate'); return true; }
/** * Constructor * * @param string $model nombre de clase de modelo * @param string $sql consulta select sql * @param int $page numero de pagina * @param int $perPage cantidad de items por pagina * @param mixed $values valores */ public function __construct($model, $sql, $page, $perPage, $values = null) { $this->perPage = (int) $perPage; $this->page = (int) $page; /*validacion*/ $this->validPage(); $this->_model = $model; // Valores para consulta $this->_values = $values !== null && !is_array($values) ? array_slice(func_get_args(), 4) : $values; $this->count = $this->countQuery($model, $sql); $this->totalPages = (int) max(1, ceil($this->count / $this->perPage)); $this->validCurrent(); // Establece el limit y offset $this->_sql = QueryGenerator::query($model::getDriver(), 'limit', $sql, $perPage, ($page - 1) * $perPage); $this->items = $model::query($this->_sql, $this->_values)->fetchAll(); }