问题
我正在使用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.
守则
schema.xml
<?xml version="1.0" encoding="UTF-8" ?>
<database name="portfolio" namespace="KristianRandallBundlePortfolioBundleModel" 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),但没有成功。以前有人遇到过这个问题吗?这个问题是如何解决的?
我最终分析了推进的代码,发现了一个避免这种情况的肮脏技巧:
当您在config.yml:中声明连接时
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
user: root
password: mysql
dsn: mysql:host=127.0.0.1;dbname=propeltest
将"default"更改为数据库名称。这里,propltest:
dbal:
default_connection: propeltest
connections:
propeltest:
driver: pdo_mysql
user: root
password: mysql
dsn: mysql:host=127.0.0.1;dbname=propeltest
现在,您在第51行/vvendor/powere/propel1/generator/lib/util/PropelSqlManager.php中的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/"
}
}
}
我不明白为什么推进会创建一个"默认连接",但这个技巧在不更改推进源代码的情况下有效。
我也遇到了同样的问题,我查看了PropelBundle的源代码。PropelBundle中似乎有一个错误。
更具体地说,在vendor/prowle/PropelBundle/PPropelBundle/Command/SqlInsertCommand.php文件中,doSqlInsert()方法使用连接名称调用propel的insertSql()。
但是,Propel的insertSql()方法需要实际SQL文件的名称,而不是连接名称作为参数。因此,insertSql()文件找不到要执行的SQL语句,并且不执行任何操作。
我不知道怎么能解决这个问题。