php doctrine 使用,php – Doctrine2迁移使用DBAL而不是$this-> addSql

所以我做了一堆Doctrine2迁移(https://

github.com/doctrine/migrations),但是我有一个新的迁移问题我试图做.

我一直在挖掘图书馆,我看到$this-> addsql()用于构建要执行的sql列表,然后稍后执行.

我想做一些我选择一些数据的东西,迭代行,插入新的数据,然后删除我选择的数据.这很容易使DBAL库变得简单,但我想知道,我可以安全地在迁移中使用受保护的$连接吗?或者是坏的,因为它会在我的$this-> addsql()sql执行之前执行语句?而且,似乎这样会从我在代码中看到的干法运行设置中断.有没有人有过这种类型的迁移的经验?有没有最佳做法?

以下是我想做的迁移,但我不确定这是否受到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)

// for each image:

// insert into article_enclosures

// update image set enclosure_id = new ID

// delete from article_image_scrape where id...

//

// insert into article_enclosures select article_image_scrapes...

$sql = "SELECT i.id img_id,e.* FROM images i JOIN article_image_scrapes e ON i.scrape_id = e.id";

$stmt = $this->connection->prepare($sql);

$stmt->execute();

$scrapesToDelete = array();

while ($row = $stmt->fetch()) {

$scrapeArticle = $row['article_id'];

$scrapeOldId = $row['id'];

$scrapeUrl = $row['url'];

$scrapeExtension = $row['extension'];

$scrapeUrlHash = $row['url_hash'];

$imageId = $row['image_id'];

$this->connection->insert('article_enclosures',array(

'url' => $scrapeUrl,'extension' => $scrapeExtension,'url_hash' => $scrapeUrlHash

));

$scrapeNewId = $this->connection->lastInsertId();

$this->connection->update('images',array(

'enclosure_id' => $scrapeNewId,'scrape_id' => null

),array(

'id' => $imageId

));

$scrapesToDelete[] = $scrapeOldId;

}

foreach ($scrapesToDelete as $id) {

$this->connection->delete('article_image_scrapes',array('id' => $id));

}

$this->addsql("INSERT INTO article_scrapes (article_id,url,extension,url_hash) "

."SELECT s.id,s.url,s.extension,s.url_hash"

."FROM article_image_scrapes s");

$this->addsql("DROP INDEX IDX_E01FBE6AA536AAC7 ON images");

$this->addsql("ALTER TABLE images DROP scrape_id,CHANGE enclosure_id enclosure_id INT NOT NULL");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值