使用sqlite内存的Phinx迁移在0.9.2中似乎不起作用,我有一个非常简单的应用程序,只有一个表(产品)。运行迁移后,product表不存在:
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");最后一行查询product表,生成以下异常:
PDOException: SQLSTATEHY000: General error: 1没有这样的表:第43行的/home/vagrant/code/ecommerce/public/index.php
为了完整起见,下面是与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();
}
}发布于 2018-06-05 14:31:24
当您从命令行正常使用phinx时,Phinx\配置的属性configFilePath已正确设置为phinx.yml的完整路径
但是,在为phpunit测试创建sqlite内存数据库的phinx文档(http://docs.phinx.org/en/latest/commands.html#using-phinx-with-phpunit)中的示例中,它使用了一个php数组,因为必须手动输入一个pdo实例。
因为没有设置phinx.yml的路径,所以Phinx\Config的replaceTokens方法通过调用以下代码来创建PHINX_CONFIG_DIR:
$tokens['%%PHINX_CONFIG_DIR%%'] = dirname($this->getConfigFilePath());Phinx使用%%PHINX_CONFIG_DIR%%来确定其迁移和种子文件夹的位置,当没有使用phinx.yml时,这将不再起作用。
解决方案是在手动创建Config类时提供路径:
$config = new Config($settings, './');https://stackoverflow.com/questions/50686998
复制相似问题