/** * Constructs a MPDOQuery object using the SQL <i>$query</i> and the database * <i>$connection</i>. If <i>$connection</i> is not specified, or is invalid, * the application's default database is used. <br /> * If query is not an empty string, it will be executed. * * @param null $query * @param \PDO|null $connection * @param MObject|null $parent */ public function __construct($query = null, \PDO $connection = null, MObject $parent = null) { parent::__construct($parent); $this->setQuery($query)->setConnection($connection); if ($this->getConnection() == null) { $this->setConnection(MDbConnection::getDbConnection()); } }
/** * Returns the QSqlQuery associated with this model. * * @param string $query * @param \PDO|null $db * @throws \Exception */ public function setQuery($query, $db = null) { if ($db == null) { $db = MDbConnection::getDbConnection(); } if ($db instanceof \PDO) { $this->query = new MPDOQuery($query, $db); } else { throw new \Exception("Database connection not supported."); } $this->query->exec(); }
/** * Return the content of a cache record with <i>$key</i>. * * @param string $key * @return string|null * @throws \Exception */ public function fetch($key) { $query = "SELECT `Key`, `Value`, `Expired` FROM `" . $this->cacheTableName . "` WHERE `key`=?;"; /* @var $connection \PDO */ $connection = MDbConnection::dbConnection(); /* @var $stmt \PDOStatement */ $stmt = $connection->prepare($query); /* @var $result bool */ $result = $stmt->execute(array($key)); if ($result === false) { throw new \Exception(json_encode($stmt->errorInfo())); } $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); $stmt->closeCursor(); if (count($rows) <= 0) { return null; } $key = $rows[0]['Key']; $value = $rows[0]['Value']; $expired = $rows[0]['Expired']; if ($expired == -1 || $expired > time()) { return unserialize($value); } $this->delete($key); return null; }