问题
我正在使用 Symfony 开发一个新项目,并尝试在我的本地环境中设置数据库。我正在使用 Symfony v. 2.3.7、PropelBundle 1.2.13 和 Propel 1.7、Mac OS X 10.9 和 PHP 5.4.17。
什么有效
我可以使用 propel:database:create
创建数据库并使用 propel:database:drop
删除它。我也可以使用propel:sql:build
生成的SQL直接插入数据库,然后使用propel:tables:drop
删除表。
什么不是
我的问题是 propel:sql:insert --force
和 propel:build --insert-sql
没有向数据库中插入任何内容。控制台没有给出错误,而是指出:
Use connection named default in dev environment.
All SQL statements have been inserted.
代码
架构.xml
<?xml version="1.0" encoding="UTF-8" ?>
<database name="portfolio" namespace="KristianRandall\Bundle\PortfolioBundle\Model" defaultIdMethod="native">
<table name="category">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar" primaryString="true" size="100" required="true"/>
<column name="description" type="longvarchar"/>
</table>
<table name="project">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar" primaryString="true" size="100" required="true"/>
<column name="content" type="longvarchar"/>
<column name="date" type="date"/>
</table>
<table name="page">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
<column name="name" type="varchar" primaryString="true" size="100" required="true"/>
<column name="content" type="longvarchar"/>
</table>
</database>
portfolio.sql(通过propel:sql:build
生成)
# This is a fix for InnoDB in MySQL >= 4.1.x
# It "suspends judgement" for fkey relationships until are tables are set.
SET FOREIGN_KEY_CHECKS = 0;
-- ---------------------------------------------------------------------
-- category
-- ---------------------------------------------------------------------
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`description` TEXT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
-- ---------------------------------------------------------------------
-- project
-- ---------------------------------------------------------------------
DROP TABLE IF EXISTS `project`;
CREATE TABLE `project`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`content` TEXT,
`date` DATE,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
-- ---------------------------------------------------------------------
-- page
-- ---------------------------------------------------------------------
DROP TABLE IF EXISTS `page`;
CREATE TABLE `page`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`content` TEXT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
总结
我的开发日志没有输出,我的数据库没有变化,控制台也没有错误。
我正在尝试通过 propel:sql:insert --force
或 propel:build --insert-sql
命令插入 SQL,但都不起作用,但是所有其他命令都是。我尝试删除推进代码并重新安装(通过 Composer )但无济于事。有没有人遇到过这个问题,如何解决的?
最佳答案
我终于分析了 propel 的代码并找到了一个避免这种情况的肮脏技巧:
当您在 config.yml 中声明您的连接时:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
user: root
password: mysql
dsn: mysql:host=127.0.0.1;dbname=propeltest
将“默认”更改为您的数据库名称。在这里,推进测试:
dbal:
default_connection: propeltest
connections:
propeltest:
driver: pdo_mysql
user: root
password: mysql
dsn: mysql:host=127.0.0.1;dbname=propeltest
您现在在/vendor/propel/propel1/generator/lib/util/PropelSqlManager.php 第 51 行的 setConnections($connections) 函数的 arg $connections 中有两个连接。
var_dump 示例:
array(2) {
["propeltest"]=>
array(8) {
["dsn"]=>
string(38) "mysql:host=127.0.0.1;dbname=propeltest"
["user"]=>
string(4) "root"
["password"]=>
string(5) "mysql"
["classname"]=>
string(8) "DebugPDO"
["options"]=>
array(0) {
}
["attributes"]=>
array(0) {
}
["settings"]=>
array(0) {
}
["model_paths"]=>
array(2) {
[0]=>
string(4) "src/"
[1]=>
string(7) "vendor/"
}
}
["default"]=>
array(8) {
["dsn"]=>
string(38) "mysql:host=127.0.0.1;dbname=propeltest"
["user"]=>
string(4) "root"
["password"]=>
string(5) "mysql"
["classname"]=>
string(8) "DebugPDO"
["options"]=>
array(0) {
}
["attributes"]=>
array(0) {
}
["settings"]=>
array(0) {
}
["model_paths"]=>
array(2) {
[0]=>
string(4) "src/"
[1]=>
string(7) "vendor/"
}
}
}
我不明白为什么 propel 会创建一个“默认连接”,但这个技巧可以在不更改 propel 源代码的情况下使用。
关于php - Symfony2 命令行 "propel:sql:insert --force"失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20579791/