Php Symfony2 Doctrine2-按条令:映射:导入从现有数据库生成一对多注释
我希望通过使用用于逆向工程的条令工具从现有数据库生成实体Php Symfony2 Doctrine2-按条令:映射:导入从现有数据库生成一对多注释,php,database,symfony,orm,doctrine-orm,Php,Database,Symfony,Orm,Doctrine Orm,我希望通过使用用于逆向工程的条令工具从现有数据库生成实体 /* * SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `country` -- ---------------------------- DROP TABLE IF EXISTS `country`; CREATE TABLE `country` ( `id` int(11) unsigned N
/*
* 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应用程序/控制台原则:映射:导入AcmeBlogBundle注释
2$php应用程序/控制台原则:生成:实体AcmeBlogBundle
但现在,该学说只在多个仅“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;
那么,我如何通过条令命令生成一对多注释呢?这是条令orm库中的更改
您可以通过更改
供应商/doctor/orm/lib/doctor/orm/Mapping/Driver/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($foreignKey作为$foreignKey){
$foreignTable=$foreignKey->getForeignTableName();
$cols=$foreignKey->getColumns();
$fkCols=$foreignKey->getForeignColumns();
$localColumn=当前($cols);
$associationMapping=array();
$associationMapping['fieldName']=$this->getFieldNameForColumn($tableName,$localColumn,true);
$associationMapping['targetEntity']=$this->getClassNameForTable($foreignTable);
if($primaryKeyColumns&&in_数组($localColumn,$primaryKeyColumns)){
$associationMapping['id']=true;
}
对于($i=0;$i$cols[$i],
“referencedColumnName”=>$fkCols[$i],
);
}
//这里我们需要检查$cols是否与$primarykeycolms相同
if(!array_diff($cols,$primaryKeyColumns)){
$metadata->mapOneToOne($associationMapping);
}否则{
$metadata->MAPMANYTONE($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);
}
}
}
foreach($foreignKey作为$foreignKey){
$foreignTable=$foreignKey->getForeignTableName();
$cols=$foreignKey->getColumns();
$fkCols=$foreignKey->getForeignColumns();
$localColumn=当前($cols);
$associationMapping=array();
$associationMapping['fieldName']=$this->getFieldNameForColumn($tableName,$localColumn,true);
$associationMapping['targetEntity']=$this->getClassNameForTable($foreignTable);
对于($i=0;$i$cols[$i],
“referencedColumnName”=>$fkCols[$i],
);
}
$metadata->MAPMANYTONE($associationMapping);
}
foreach($this->tables as$tableCandidate){
如果($this->\u sm->getDatabasePlatform()->supportsForeignKeyConstraints()){
$foreignKeysCandidate=$tableCandidate->getForeignKeys();
}否则{
$foreignKeysCandidate=array();
}
foreach($foreignKeysCandidate作为$foreignKey){
$foreignTable=$foreignKey->getForeignTableName();
if($foreignTable==$tableName&&!isset($this->manyToManyTables[$tableCandidate->getName())){
$fkCols=$foreignKey->getForeignColumns();
$cols=$foreignKey->getColumns();
$localColumn=当前($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);
试一试{
$primaryKeyColumns=$tableCandidate->getPrimaryKey()->getColumns();
if(计数($primaryKeyColumns)==1){
$indexColumn=当前($primaryKeyColumns);
$associationMapping['indexBy']=$indexColumn;
}
}捕获(SchemaException$e){
}
$metadata->mapOneToMany($associationMapping);
}
}
}
现在,如果运行条令:映射:导入
您将找到一个omany注释
然后运行条令:生成:实体
你会发现双方的关系都是单向的 非常感谢,伙计,这正是我想要的!