コード例 #1
0
 public function tearDown()
 {
     if (!class_exists('Zend\\Db\\Adapter\\Adapter')) {
         return;
     }
     // drop test tables
     if (static::$schemaCleanup) {
         static::$schemaCleanup = false;
         try {
             $adapter = $this->adapter;
             $ddl = new Ddl\DropTable('model');
             $sql = new Sql($adapter);
             $adapter->query($sql->getSqlStringForSqlObject($ddl))->execute();
         } catch (\PDOException $e) {
         }
     }
     // disconnect
     if ($this->adapter && $this->adapter->getDriver()->getConnection()->isConnected()) {
         $this->adapter->getDriver()->getConnection()->disconnect();
     }
     // reset default db adapter
     Model::setDefaultDb(null);
     MinimalModel::setDefaultDb(null);
     ZendDb::setDefaultDb(null);
     // clear instance registry
     Core::clearInstanceRegistry();
 }
コード例 #2
0
ファイル: Memory.php プロジェクト: thinkscape/activerecord
 public function save()
 {
     // Get trait level storage
     $storage =& Memory::_getInternalStorage();
     $class = get_called_class();
     // Make sure there's class-specific container
     if (!isset($storage[$class])) {
         $storage[$class] = [];
     }
     if (!$this->id) {
         // perform an INSERT operation
         // Get the data
         $updateData = $this->collectUpdateData();
         // Remember ID
         $this->id = (int) (microtime(true) * 1000);
         // Store data
         $storage[$class][$this->id] = $updateData;
         // Update object state
         $this->_dirtyData = [];
         $this->isLoaded = true;
         // Store in registry
         Core::storeInstanceInRegistry(get_called_class(), $this->id, $this);
     } else {
         // perform an UPDATE operation
         if (!$this->isLoaded) {
             throw new Exception\RuntimeException('Attempt to save() a record that has not been loaded');
         }
         // Get update data
         $updateData = $this->collectUpdateData();
         // Store in memory
         foreach ($updateData as $key => $val) {
             $storage[$class][$this->id][$key] = $val;
         }
         // Update object state
         $this->_dirtyData = [];
         $this->isLoaded = true;
     }
 }
コード例 #3
0
ファイル: ZendDb.php プロジェクト: thinkscape/activerecord
 /**
  * Store instance data in the database
  *
  * @throws \Thinkscape\ActiveRecord\Exception\DatabaseException
  * @throws \Thinkscape\ActiveRecord\Exception\RuntimeException
  * @return void
  */
 public function save()
 {
     $db = $this->getDb();
     $sql = new Sql($db);
     if (!$this->id) {
         // perform an INSERT operation
         // Get the data
         $updateData = $this->collectUpdateData();
         // Prepare and execute INSERT
         $insert = $sql->insert($this->getDbTable())->values($updateData);
         $db->query($insert->getSqlString($db->getPlatform()), $db::QUERY_MODE_EXECUTE);
         // Remember ID
         if (!($this->id = $db->getDriver()->getLastGeneratedValue())) {
             throw new Exception\DatabaseException(sprintf('Unable to retrieve INSERT id when trying to persist %s', get_class($this)));
         }
         // Store in registry
         Core::storeInstanceInRegistry(get_called_class(), $this->id, $this);
     } else {
         // perform an UPDATE operation
         if (!$this->isLoaded) {
             throw new Exception\RuntimeException('Attempt to save() a record that has not been loaded');
         }
         // Get the data
         $updateData = $this->collectUpdateData();
         if (!count($updateData)) {
             return;
             // there is nothing to update
         }
         // Prepare and execute UPDATE
         $insert = $sql->update($this->getDbTable())->set($updateData);
         $result = $db->query($insert->getSqlString($db->getPlatform()))->execute();
         // Check if successful
         if ($result->getAffectedRows() == 0) {
             throw new Exception\DatabaseException(sprintf('Unable to update record #%s in table %s (class %s)', $this->id, $this->getDbTable(), get_class($this)));
         }
     }
 }
コード例 #4
0
ファイル: Core.php プロジェクト: thinkscape/activerecord
 /**
  * Retrieve instance for given unique id, or create a completely new instance.
  *
  * @param string|int|null $data ID of the object to retrieve, or an array of properties to create a completely
  *                               new record. Use null to create a new record with default values.
  * @return static
  * @throws Exception\InvalidArgumentException
  */
 public static function factory($data = null)
 {
     if ($data !== null && !is_numeric($data) && !is_array($data) && !$data instanceof Traversable) {
         throw new Exception\InvalidArgumentException(sprintf('Cannot get new instance of %s using %s - expected a number, array or \\Traversable', get_called_class(), gettype($data)));
     }
     // Retrieve the instance by ID
     $class = get_called_class();
     if (is_numeric($data)) {
         // Try to find the object reference in the registry
         $instance = Core::getInstanceFromRegistry($class, (int) $data);
         // If it wasn't found, create a new instance for this ID and add it to registry.
         if (!$instance) {
             $instance = new static(['id' => (int) $data]);
             Core::storeInstanceInRegistry($class, (int) $data, $instance);
         }
         return $instance;
     }
     // Create completely new instance but don't store it in registry yet (as we don't know the ID)
     $instance = new static($data);
     return $instance;
 }