使用 sqlite 内存的 Phinx 迁移似乎在 0.9.2 中不起作用,我有一个非常简单的应用程序,只有一个表(产品)。运行迁移后,产品表不存在:
use Symfony\Component\Yaml\Yaml;
use Phinx\Config\Config;
use Phinx\Migration\Manager;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
$pdo = new PDO('sqlite::memory:', null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$settings = Yaml::parseFile('../phinx.yml');
$settings['environments']['testing'] = [
'adapter' => 'sqlite',
'connection' => $pdo
];
$config = new Config($settings);
$manager = new Manager($config, new StringInput(' '), new NullOutput());
$manager->migrate('testing');
$manager->seed('testing');
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
// This line creates an exception of table doesn't exist
$pdo->query("SELECT * FROM product");
最后一行查询产生以下异常的产品表:
PDOException: SQLSTATE[HY000]: 一般错误: 1 no such table: product in /home/vagrant/code/ecommerce/public/index.php on line 43
为了完整起见,这里是与开发 mysql 环境完美配合的产品迁移:
use Phinx\Migration\AbstractMigration;
class Product extends AbstractMigration
{
public function change()
{
$table = $this->table('product');
$table->addColumn('name', 'string', ['limit' => 100, 'null' => false])
->addColumn('price', 'integer')
->create();
}
}