如何在Yii Migrations中保存sql语句的返回值

时间:2022-02-12 13:46:00

I am trying to build a Migration, where the output of one SQL statement needs to be parsed and then piped to the following migration statement .

我正在尝试构建一个Migration,其中需要解析一个SQL语句的输出,然后通过管道传递给以下迁移语句。

CDbMigration::execute() does not have a return option, any ideas how to do this? See Class Reference

CDbMigration :: execute()没有返回选项,任何想法如何做到这一点?请参阅类参考

While I can run the query via ActiveRecord and parse the returned output, using active records in CDbMigration is not recommended, any better solution would be helpful

虽然我可以通过ActiveRecord运行查询并解析返回的输出,但不推荐使用CDbMigration中的活动记录,任何更好的解决方案都会有所帮助

1 个解决方案

#1


3  

As far as I know, the execute command will never return any results because it is designed to perform an execution, not a query - i.e. it sets some data in a table, rather than getting it.

据我所知,execute命令永远不会返回任何结果,因为它被设计为执行执行而不是查询 - 即它在表中设置一些数据,而不是获取它。

Could you perhaps try this in either your up or down method as required:

你可以根据需要在你的up或down方法中尝试这个:

$results = $this->getDBConnection()->createCommand($sql)->query();

though I would comment that if you need to do this, migration may not be the best tool for the job.

虽然我会评论说如果你需要这样做,迁移可能不是这项工作的最佳工具。

#1


3  

As far as I know, the execute command will never return any results because it is designed to perform an execution, not a query - i.e. it sets some data in a table, rather than getting it.

据我所知,execute命令永远不会返回任何结果,因为它被设计为执行执行而不是查询 - 即它在表中设置一些数据,而不是获取它。

Could you perhaps try this in either your up or down method as required:

你可以根据需要在你的up或down方法中尝试这个:

$results = $this->getDBConnection()->createCommand($sql)->query();

though I would comment that if you need to do this, migration may not be the best tool for the job.

虽然我会评论说如果你需要这样做,迁移可能不是这项工作的最佳工具。