大佬教程收集整理的这篇文章主要介绍了php – Doctrine2迁移使用DBAL而不是$this-> addSql,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在挖掘图书馆,我看到$this-> addsql()用于构建要执行的sql列表,然后稍后执行.
我想做一些我选择一些数据的东西,迭代行,插入新的数据,然后删除我选择的数据.这很容易使DBAL库变得简单,但我想知道,我可以安全地在迁移中使用受保护的$连接吗?或者是坏的,因为它会在我的$this-> addsql()sql执行之前执行语句?而且,似乎这样会从我在代码中看到的干法运行设置中断.有没有人有过这种类型的迁移的经验?有没有最佳做法?
以下是我想做的迁移,但我不确定这是否受到Doctrine Migrations的@L_618_10@:
public function up(scheR_359_11845@a $scheR_359_11845@a) { // 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"); }
$result = $this->connection->fetchAssoc('SELECT id,name FROM table1 WHERE id = 1'); $this->abortIf(!$result,'row with id not found'); $this->abortIf($result['name'] != 'jo','id 1 is not jo'); // etc..
您只应该读取数据库,而不是使用连接进行更新/删除,因此不会破坏干运行选项.
在你的例子中,你应该做两次迁移.第一个会做两个alter table.第二个将执行“刮刮图像并将刮刀转换为外壳”程序.如果出现问题,使用多次迁移更容易恢复.
以上是大佬教程为你收集整理的php – Doctrine2迁移使用DBAL而不是$this-> addSql全部内容,希望文章能够帮你解决php – Doctrine2迁移使用DBAL而不是$this-> addSql所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。