MigrationVersion::setVersion PHP Méthode

setVersion() public méthode

Set current version for given type
public setVersion ( integer $version, string $type, boolean $migrated = true ) : boolean
$version integer Current version
$type string Can be 'app' or a plugin name
$migrated boolean If true, will add the record to the database If false, will remove the record from the database
Résultat boolean
    public function setVersion($version, $type, $migrated = true)
    {
        if ($this->dry) {
            return true;
        }
        if ($type !== 'app') {
            $type = Inflector::camelize($type);
        }
        $mapping = $this->getMapping($type);
        // For BC, 002 was not applied yet.
        $bc = $this->Version->schema('class') === null;
        $field = $bc ? 'version' : 'class';
        $value = $bc ? $version : $mapping[$version]['class'];
        if ($migrated) {
            $this->Version->create();
            $result = $this->Version->save(array($field => $value, 'type' => $type));
        } else {
            $conditions = array($this->Version->alias . '.' . $field => $value, $this->Version->alias . '.type' => $type);
            $result = $this->Version->deleteAll($conditions);
        }
        // Clear mapping cache
        unset($this->_mapping[$type]);
        return $result;
    }

Usage Example

 /**
  * testSetGetVersion method
  *
  * @return void
  */
 public function testSetGetVersion()
 {
     $this->Version = $this->getMock('MigrationVersion', array('getMapping'), array(array('connection' => 'test')));
     // Checking current
     $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping()));
     $result = $this->Version->getVersion('inexistent_plugin');
     $expected = 0;
     $this->assertEqual($result, $expected);
     // Setting as 1
     $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping()));
     $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1)));
     $setResult = $this->Version->setVersion(1, 'inexistent_plugin');
     $this->assertTrue(!empty($setResult));
     $result = $this->Version->getVersion('inexistent_plugin');
     $expected = 1;
     $this->assertEqual($result, $expected);
     // Setting as 2
     $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1)));
     $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 2)));
     $setResult = $this->Version->setVersion(2, 'inexistent_plugin');
     $this->assertTrue(!empty($setResult));
     $result = $this->Version->getVersion('inexistent_plugin');
     $expected = 2;
     $this->assertEqual($result, $expected);
     // Setting as 1
     $this->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue($this->__mapping(1, 2)));
     $this->Version->expects($this->at(1))->method('getMapping')->will($this->returnValue($this->__mapping(1, 1)));
     $setResult = $this->Version->setVersion(2, 'inexistent_plugin', false);
     $this->assertTrue(!empty($setResult));
     $result = $this->Version->getVersion('inexistent_plugin');
     $expected = 1;
     $this->assertEqual($result, $expected);
 }
All Usage Examples Of MigrationVersion::setVersion