php – Symfony2 Doctrine2 – 通过doctrine从现有数据库生成一对多注释:mapping:import
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Symfony2 Doctrine2 – 通过doctrine从现有数据库生成一对多注释:mapping:import,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5030字,纯文字阅读大概需要8分钟。
内容图文
![php – Symfony2 Doctrine2 – 通过doctrine从现有数据库生成一对多注释:mapping:import](/upload/InfoBanner/zyjiaocheng/889/2959cc4ba8874ed5bdcf2bfa4e6e40d8.jpg)
我想通过使用Doctrine工具进行逆向工程来从现有数据库生成实体
/*
* SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `country`
-- ----------------------------
DROP TABLE IF EXISTS `country`;
CREATE TABLE `country` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `provider`
-- ----------------------------
DROP TABLE IF EXISTS `provider`;
CREATE TABLE `provider` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `provider_country`
-- ----------------------------
DROP TABLE IF EXISTS `provider_country`;
CREATE TABLE `provider_country` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`providerId` int(11) unsigned NOT NULL,
`countryId` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_provider_has_id0_idx` (`providerId`),
KEY `fk_user_country_idx` (`countryId`),
CONSTRAINT `fk_rss_has_id` FOREIGN KEY (`providerId`) REFERENCES `provider` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_user_country` FOREIGN KEY (`countryId`) REFERENCES `country` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS=1;
*/
您可以通过执行以下两个命令,让Doctrine导入模式并构建相关的实体类.
1 $php app / console doctrine:mapping:import AcmeBlogBu??ndle annotation
2 $php app / console doctrine:generate:entities AcmeBlogBu??ndle
但是现在的学说只检测了ManyToOne关系,在很多方面只有“ProviderCountry”表
如果我需要添加OneToMany关系,我必须通过添加下面的注释来添加注释
在Provider.php中添加
/**
* @ORM\OneToMany(targetEntity="ProviderCountry", mappedBy="providerId", cascade={"persist"})
*/
private $datas;
在ProviderCountry.php中添加
/**
* @var Provider
*
* @ORM\ManyToOne(targetEntity="Provider", inversedBy = "datas")
* @ORM\JoinColumn(name="providerId", referencedColumnName="id")
*/
private $userid;
那么如何通过doctrine命令生成一对多注释
解决方法:
这只是在学说orm lib中的变化,
你可以通过改变来解决这个问题
供应商/教义/ ORM / LIB /学说/ ORM /映射/驱动器/ DatabaseDriver.php
替换以下语句
foreach ($foreignKeys as $foreignKey) {
$foreignTable = $foreignKey->getForeignTableName();
$cols = $foreignKey->getColumns();
$fkCols = $foreignKey->getForeignColumns();
$localColumn = current($cols);
$associationMapping = array();
$associationMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $localColumn, true);
$associationMapping['targetEntity'] = $this->getClassNameForTable($foreignTable);
if ($primaryKeyColumns && in_array($localColumn, $primaryKeyColumns)) {
$associationMapping['id'] = true;
}
for ($i = 0; $i < count($cols); $i++) {
$associationMapping['joinColumns'][] = array(
'name' => $cols[$i],
'referencedColumnName' => $fkCols[$i],
);
}
//Here we need to check if $cols are the same as $primaryKeyColums
if (!array_diff($cols, $primaryKeyColumns)) {
$metadata->mapOneToOne($associationMapping);
} else {
$metadata->mapManyToOne($associationMapping);
}
}
改成
foreach ($foreignKeys as $foreignKey) {
$foreignTable = $foreignKey->getForeignTableName();
$cols = $foreignKey->getColumns();
$fkCols = $foreignKey->getForeignColumns();
$localColumn = current($cols);
$associationMapping = array();
$associationMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $localColumn, true);
$associationMapping['targetEntity'] = $this->getClassNameForTable($foreignTable);
for ($i = 0; $i < count($cols); $i++) {
$associationMapping['joinColumns'][] = array(
'name' => $cols[$i],
'referencedColumnName' => $fkCols[$i],
);
}
$metadata->mapManyToOne($associationMapping);
}
foreach ($this->tables as $tableCandidate) {
if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$foreignKeysCandidate = $tableCandidate->getForeignKeys();
} else {
$foreignKeysCandidate = array();
}
foreach ($foreignKeysCandidate as $foreignKey) {
$foreignTable = $foreignKey->getForeignTableName();
if ($foreignTable == $tableName && !isset($this->manyToManyTables[$tableCandidate->getName()])) {
$fkCols = $foreignKey->getForeignColumns();
$cols = $foreignKey->getColumns();
$localColumn = current($cols);
$associationMapping = array();
$associationMapping['fieldName'] = $this->getFieldNameForColumn($tableCandidate->getName(), $tableCandidate->getName(), true);
$associationMapping['targetEntity'] = $this->getClassNameForTable($tableCandidate->getName());
$associationMapping['mappedBy'] = $this->getFieldNameForColumn($tableCandidate->getName(), $localColumn, true);
try {
$primaryKeyColumns = $tableCandidate->getPrimaryKey()->getColumns();
if (count($primaryKeyColumns) == 1) {
$indexColumn = current($primaryKeyColumns);
$associationMapping['indexBy'] = $indexColumn;
}
} catch (SchemaException $e) {
}
$metadata->mapOneToMany($associationMapping);
}
}
}
现在如果你运行doctrine:mapping:import
你会发现OneToMany注释.
然后运行doctrine:generate:entities
你会发现双方的单向关系.
内容总结
以上是互联网集市为您收集整理的php – Symfony2 Doctrine2 – 通过doctrine从现有数据库生成一对多注释:mapping:import全部内容,希望文章能够帮你解决php – Symfony2 Doctrine2 – 通过doctrine从现有数据库生成一对多注释:mapping:import所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。