相关疑难解决方法(0)

在迁移文件中使用EntityManager

我有这个代码,但不起作用:

<?php

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration,
    Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated Migration: Please modify to your need!
 */
class Version20131021150555 extends AbstractMigration
{

    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");

        $this->addSql("ALTER TABLE person ADD tellphone LONGTEXT DEFAULT NULL");

        $em = $em = $this->getDoctrine()->getEntityManager();
        $persons = $em->getRepository('AutogestionBundle:Person')->fetchAll();

        foreach($persons as $person){
            $person->setTellPhone($person->getCellPhone());
            $em->persist($person);                                                                            
        }
        $em->flush(); 
    }

    public function down(Schema $schema)
    {
        // …
Run Code Online (Sandbox Code Playgroud)

migration symfony doctrine-orm

21
推荐指数
3
解决办法
1万
查看次数

Doctrine2迁移使用DBAL而不是$ this-> addSql

所以我已经完成了一堆Doctrine2迁移(https://github.com/doctrine/migrations),但我对我正在尝试进行的新迁移有疑问.

我一直在深入挖掘库,我看到它$this->addSql()用于构建要执行的SQL列表,然后稍后执行.

我想做一些事情,我选择一些数据,迭代行,插入新数据,然后删除我选择的数据.这很适合DBAL库,但我想知道,我可以protected $connection安全地使用迁移吗?或者是那么糟糕,因为它会在我的任何$this->addSql()SQL 执行之前执行语句?此外,它似乎会破坏dry-run我在代码中看到的设置.有没有人有这种迁移的经验?有没有最佳做法?

以下是我想要进行的迁移,但我不相信Doctrine Migrations支持这一点:

public function up(Schema $schema)
{
    // this up() migration is autogenerated, please modify it to your needs
    $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");

    $this->addSql("ALTER TABLE article_enclosures ADD is_scrape TINYINT(1) NOT NULL");
    $this->addSql("ALTER TABLE images DROP FOREIGN KEY FK_E01FBE6AA536AAC7");

    // now lets take all images with a scrape and convert the scrape to an enclosure
    // 
    // Select all images where not scrape_id is null (join on article_image_scrape)
    // …
Run Code Online (Sandbox Code Playgroud)

php database-migration doctrine-orm

13
推荐指数
2
解决办法
9669
查看次数