Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 推进1.6.9迁移(数据)-可能实现';向下';仅使用PDO的命令?_Php_Pdo_Propel - Fatal编程技术网

Php 推进1.6.9迁移(数据)-可能实现';向下';仅使用PDO的命令?

Php 推进1.6.9迁移(数据)-可能实现';向下';仅使用PDO的命令?,php,pdo,propel,Php,Pdo,Propel,我刚刚将一个遗留项目中的推进从1.4升级到1.6.9,并尝试使用迁移功能。在做一些测试时,我意识到在进行数据迁移(无模式更改)时,可以使用PDO类来管理插入/更新新数据,但对于同一迁移的“停止”部分,情况并非如此 或者更确切地说,它将执行preDown()方法的PDO方面,但是由于getDownSQL()方法没有执行任何操作,因此它不会更改存储在数据库中的当前迁移版本。这显然是一个问题,因为它不会让我再次应用迁移,除非我手动更改版本号,我不想养成这样做的习惯 例如,这里是迁移的“up”部分:(尽

我刚刚将一个遗留项目中的推进从1.4升级到1.6.9,并尝试使用迁移功能。在做一些测试时,我意识到在进行数据迁移(无模式更改)时,可以使用PDO类来管理插入/更新新数据,但对于同一迁移的“停止”部分,情况并非如此

或者更确切地说,它将执行
preDown()
方法的PDO方面,但是由于
getDownSQL()
方法没有执行任何操作,因此它不会更改存储在数据库中的当前迁移版本。这显然是一个问题,因为它不会让我再次应用迁移,除非我手动更改版本号,我不想养成这样做的习惯

例如,这里是迁移的“up”部分:(尽管
getUpSQL()

下面是我想在我的
postDown()
:(我意识到使用
preDown()
,因为这是一个坏主意-我不希望更改与版本号不同步)

此时未执行down,因为
getDownSQL()
未返回任何内容。我可以让它工作使用SQL为向下的方向,就像这样

    public function getDownSQL()
    {
        return array(
            'source' => "
DELETE FROM properties WHERE name = 'blahblah' LIMIT 1;
            "
        );
    }

…但如果可能的话,我不想这么做。那么,有人知道我是否可以强迫它认为它已经做了更改,即使没有更改,这样我就可以使用
postDown()
方式,并相应地更改版本号?谢谢。

我自己设法解决了这个问题。我已经编写了一个控制台命令,该命令基于自定义模板生成一个空的迁移,这样我就可以进行仅数据的迁移。我只是这样做的,这些生成的迁移类在其他地方扩展了一个通用的
BaseMigration
类,该类包含以下方法:(加上一些其他方法,但它们在这里并不重要)

/**
*将版本强制为此迁移的版本。仅对使用preUp()的数据迁移有用
*
*@param PropelMigrationManager$manager
*/
受保护的函数updateToCurrentVersion(PropelMigrationManager$manager)
{
$manager->updateLatestMigrationTimestamp($this->getCurrentDatasource($manager),$this->getCurrentVersion());
}
/**
*如果在preDown()方法中使用PDO实现down功能,则应调用此方法
*
*@param PropelMigrationManager$manager
*/
受保护的函数UpdatePreviousVersion(PropelMigrationManager$manager)
{
$datasource=$this->getCurrentDatasource($manager);
$currentVersion=$this->getCurrentVersion();
$timestamps=$manager->getAlreadyExecutedMigrationTimestamps();
rsort(时间戳);
$previousVersion=0;
foreach($timestamp作为$timestamp){
如果($timestamp<$currentVersion){
$previousVersion=$timestamp;
打破
}
}
$manager->UpdateTestMigrationTimestamp($datasource,$previousVersion);
}
/**
*@return int
*/
受保护的函数getCurrentVersion()
{
$class=get_class($this);
返回(int)substr($class,strpos($class,'.')+1);
}
现在,我只需要从
preUp()
方法(见下面的注释)的末尾调用
$this->updateToCurrentVersion($manager)
,并在preDown()方法的末尾调用
$this->updateToPreviousVersion($manager)
,以强制正确更新版本号


注意:我注意到,当运行“up”迁移命令仅执行一次迁移时,版本没有更新,而当运行“migrate”时,版本会更新……即使这是应用相同的迁移!因此,强制始终更新当前版本更安全。

我自己设法解决了这个问题。我已经编写了一个控制台命令,该命令基于自定义模板生成一个空的迁移,这样我就可以进行仅数据的迁移。我只是这样做的,这些生成的迁移类在其他地方扩展了一个通用的
BaseMigration
类,该类包含以下方法:(加上一些其他方法,但它们在这里并不重要)

/**
*将版本强制为此迁移的版本。仅对使用preUp()的数据迁移有用
*
*@param PropelMigrationManager$manager
*/
受保护的函数updateToCurrentVersion(PropelMigrationManager$manager)
{
$manager->updateLatestMigrationTimestamp($this->getCurrentDatasource($manager),$this->getCurrentVersion());
}
/**
*如果在preDown()方法中使用PDO实现down功能,则应调用此方法
*
*@param PropelMigrationManager$manager
*/
受保护的函数UpdatePreviousVersion(PropelMigrationManager$manager)
{
$datasource=$this->getCurrentDatasource($manager);
$currentVersion=$this->getCurrentVersion();
$timestamps=$manager->getAlreadyExecutedMigrationTimestamps();
rsort(时间戳);
$previousVersion=0;
foreach($timestamp作为$timestamp){
如果($timestamp<$currentVersion){
$previousVersion=$timestamp;
打破
}
}
$manager->UpdateTestMigrationTimestamp($datasource,$previousVersion);
}
/**
*@return int
*/
受保护的函数getCurrentVersion()
{
$class=get_class($this);
返回(int)substr($class,strpos($class,'.')+1);
}
现在,我只需要从
preUp()
方法(见下面的注释)的末尾调用
$this->updateToCurrentVersion($manager)
,并在preDown()方法的末尾调用
$this->updateToPreviousVersion($manager)
,以强制正确更新版本号

注意:我注意到,在运行“up”迁移命令以仅执行一次迁移时,版本没有更新,而在运行“migrate”时,版本会更新……即使这是
public function postDown(PropelMigrationManager $manager)
{
    $query = new PropertyQuery();
    $property = $query->findOneByName('blahblah');
    $property->delete();
}
    public function getDownSQL()
    {
        return array(
            'source' => "
DELETE FROM properties WHERE name = 'blahblah' LIMIT 1;
            "
        );
    }
/**
 * Force the version to the version of this migration. Useful for data migrations only using preUp()
 *
 * @param PropelMigrationManager $manager
 */
protected  function updateToCurrentVersion(PropelMigrationManager $manager)
{
    $manager->updateLatestMigrationTimestamp($this->getCurrentDatasource($manager), $this->getCurrentVersion());
}

/**
 * This method should be called if implementing the down functionality using PDO in the preDown() method
 *
 * @param PropelMigrationManager $manager
 */
protected function updateToPreviousVersion(PropelMigrationManager $manager)
{
    $datasource = $this->getCurrentDatasource($manager);
    $currentVersion = $this->getCurrentVersion();

    $timestamps = $manager->getAlreadyExecutedMigrationTimestamps();
    rsort($timestamps);

    $previousVersion = 0;
    foreach ($timestamps as $timestamp) {
        if ($timestamp < $currentVersion) {
            $previousVersion = $timestamp;
            break;
        }
    }

    $manager->updateLatestMigrationTimestamp($datasource, $previousVersion);
}

/**
 * @return int
 */
protected function getCurrentVersion()
{
    $class = get_class($this);

    return (int) substr($class, strpos($class, '_') + 1);
}