在搜索了一段时间后,我决定向您展示我在 Symfony2 中的映射转换器实现的问题。首先,我向您展示我的设置:
具有关系的用户表:
-- -----------------------------------------------------
-- Table `event_manager`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `event_manager`.`user` ;
CREATE TABLE IF NOT EXISTS `event_manager`.`user` (
`id` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR(255) NULL ,
`salt` VARCHAR(255) NULL ,
`password` VARCHAR(255) NULL ,
`logged_in` TINYINT(1) NULL ,
`status` ENUM('active', 'inactive', 'deleted') NULL ,
PRIMARY KEY (`id`) ,
CONSTRAINT `fk_user_user_data1`
FOREIGN KEY (`id` )
REFERENCES `event_manager`.`user_data` (`user_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `event_manager`.`user_data`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `event_manager`.`user_data` ;
CREATE TABLE IF NOT EXISTS `event_manager`.`user_data` (
`user_id` INT NOT NULL ,
`image_id` INT NULL ,
`gender` ENUM('male','female') NULL ,
`first_name` VARCHAR(255) NULL ,
`last_name` VARCHAR(255) NULL ,
`address` VARCHAR(255) NULL ,
`zip` VARCHAR(255) NULL ,
`city` VARCHAR(255) NULL ,
`phone_private` VARCHAR(255) NULL ,
`phone_mobile` VARCHAR(255) NULL ,
`phone_work` VARCHAR(255) NULL ,
`user_datacol` VARCHAR(45) NULL ,
PRIMARY KEY (`user_id`) ,
CONSTRAINT `fk_user_data_image1`
FOREIGN KEY (`image_id` )
REFERENCES `event_manager`.`image` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_user_data_image1_idx` ON `event_manager`.`user_data` (`image_id` ASC) ;
在我的数据库上,我使用以下命令的教义转换器:
php app/console doctrine:mapping:convert yml ./src/path-to-bundle/Resources/config/doctrine --from-database --force --filter=User
然后我在用户 YAML 上得到这个结果:
User:
type: entity
table: user
fields:
email:
type: string
length: 255
fixed: false
nullable: true
salt:
type: string
length: 255
fixed: false
nullable: true
password:
type: string
length: 255
fixed: false
nullable: true
loggedIn:
type: boolean
nullable: true
column: logged_in
status:
type: string
length: null
fixed: false
nullable: true
manyToMany:
userGroup:
targetEntity: UserGroup
cascade: { }
mappedBy: null
inversedBy: user
joinTable:
name: user_has_user_group
joinColumns:
-
name: user_id
referencedColumnName: id
inverseJoinColumns:
-
name: user_group_id
referencedColumnName: id
orderBy: null
oneToOne:
id:
targetEntity: UserData
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
id:
referencedColumnName: user_id
orphanRemoval: false
lifecycleCallbacks: { }
如您所见,学说删除了“id”列作为主键,并将其用作关系的名称,这最终使我想到了这样的实体方法:
/**
* Set id
*
* @param \Parella\EventManagerBundle\Entity\UserData $id
* @return User
*/
public function setId(\Parella\EventManagerBundle\Entity\UserData $id = null)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return \Parella\EventManagerBundle\Entity\UserData
*/
public function getId()
{
return $this->id;
}
这当然不是我想要的,而且我经常从数据库中一次创建许多实体,所以手动修复这不是一个真正的选择。不幸的是,我不知道是我造成了问题还是教义。我想念什么吗?
感谢您的回复。