Doctrine 2.1.6 逆向工程 1-n(一对多)关联被误解为 1-1(一对一)

Posted

技术标签:

【中文标题】Doctrine 2.1.6 逆向工程 1-n(一对多)关联被误解为 1-1(一对一)【英文标题】:Doctrine 2.1.6 Reverse Engineering 1-n (one to many) association misunderstood as 1-1 (one to one) 【发布时间】:2012-04-26 13:16:16 【问题描述】:

我发现 Doctrine 的逆向工程过程有一个奇怪的行为,只需创建两个简单的 1-n 关系绑定的简单表,看看以下 SQL 代码的快照:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

DROP SCHEMA IF EXISTS `ACME` ;
CREATE SCHEMA IF NOT EXISTS `ACME` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `ACME` ;

-- -----------------------------------------------------
-- Table `ACME`.`task`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ACME`.`task` ;

CREATE  TABLE IF NOT EXISTS `ACME`.`task` (
  `id_task` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `description` VARCHAR(45) NULL ,
  PRIMARY KEY (`id_task`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `ACME`.`tag`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `ACME`.`tag` ;

CREATE  TABLE IF NOT EXISTS `ACME`.`tag` (
  `id_tag` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(50) NULL ,
  `task_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`id_tag`) ,
  INDEX `fk_tag_task` (`task_id` ASC) ,
  CONSTRAINT `fk_tag_task`
    FOREIGN KEY (`task_id` )
    REFERENCES `ACME`.`task` (`id_task` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

我在

有一个 Symfony2 netbeans 项目

/Applications/MAMP/htdocs/Acme

从那个位置,根据

http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

在我做的终端中:

$ ./../../bin/php/php5.3.6/bin/php app/console doctrine:mapping:convert yml ./src/Acme/TaskBundle/Resources/config/doctrine/ --from-database --force
Processing entity "Tag"
Processing entity "Task"

Exporting "yml" mapping information to "/Applications/MAMP/htdocs/Acme/src/Acme/TaskBundle/Resources/config/doctrine"

$ ./../../bin/php/php5.3.6/bin/php app/console doctrine:mapping:import Acme\TaskBundle yml
Importing mapping information from "default" entity manager
  > writing /Applications/MAMP/htdocs/Acme/src/Acme/TaskBundle/Resources/config/doctrine/Tag.orm.yml
  > writing /Applications/MAMP/htdocs/Acme/src/Acme/TaskBundle/Resources/config/doctrine/Task.orm.yml

$ ./../../bin/php/php5.3.6/bin/php app/console doctrine:generate:entities Acme\TaskBundle
Generating entities for bundle "AcmeTaskBundle"
  > backing up Tag.php to Tag.php~
  > generating Acme\TaskBundle\Entity\Tag
  > backing up Task.php to Task.php~
  > generating Acme\TaskBundle\Entity\Task

事实是它看起来还可以,因为如果你看一下“Tag.orm.yml”:

Acme\TaskBundle\Entity\Tag:
  type: entity
  table: tag
  fields:
    idTag:
      id: true
      type: integer
      unsigned: false
      nullable: false
      column: id_tag
      generator:
        strategy: IDENTITY
    name:
      type: string
      length: 50
      fixed: false
      nullable: true
  oneToOne:
    task:
      targetEntity: Task
      cascade:   
      mappedBy: null
      inversedBy: null
      joinColumns:
        task_id:
          referencedColumnName: id_task
      orphanRemoval: false
  lifecycleCallbacks:   

它创建了 oneToOne 关系,而不是 oneToMany

如果您需要更多确认,这里是 Task.phpTag.php: p>

Task.php

namespace Acme\TaskBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\TaskBundle\Entity\Task
 */
class Task

    /**
     * @var integer $idTask
     */
    private $idTask;

    /**
     * @var string $description
     */
    private $description;


    /**
     * Get idTask
     *
     * @return integer 
     */
    public function getIdTask()
    
        return $this->idTask;
    

    /**
     * Set description
     *
     * @param string $description
     */
    public function setDescription($description)
    
        $this->description = $description;
    

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    
        return $this->description;
    

Tag.php

namespace Acme\TaskBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\TaskBundle\Entity\Tag
 */
class Tag

    /**
     * @var integer $idTag
     */
    private $idTag;

    /**
     * @var string $name
     */
    private $name;

    /**
     * @var Acme\TaskBundle\Entity\Task
     */
    private $task;


    /**
     * Get idTag
     *
     * @return integer 
     */
    public function getIdTag()
    
        return $this->idTag;
    

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    
        $this->name = $name;
    

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    
        return $this->name;
    

    /**
     * Set task
     *
     * @param Acme\TaskBundle\Entity\Task $task
     */
    public function setTask(\Acme\TaskBundle\Entity\Task $task)
    
        $this->task = $task;
    

    /**
     * Get task
     *
     * @return Acme\TaskBundle\Entity\Task 
     */
    public function getTask()
    
        return $this->task;
    

你们也遇到过这个问题吗? 提前谢谢你

【问题讨论】:

我认为这是一个错误,所以我将它发布在 Symfony 的问题页面中。 有人遇到过这个bug吗?我是唯一遇到它的孤独可怜人吗? 【参考方案1】:

我有一个重要的发现:在第一个命令中使用 xml 而不是 yml,那个带有教义:mapping:convert 的命令,它以正确的方式工作。

该死的愚蠢的虫子。

Linuxatico

【讨论】:

以上是关于Doctrine 2.1.6 逆向工程 1-n(一对多)关联被误解为 1-1(一对一)的主要内容,如果未能解决你的问题,请参考以下文章

Symfony 4 Doctrine逆向工程问题

Doctrine2 - 注释 vs yml / xml

CodeIgniter Doctrine2基本使用(转)

Doctrine异常 - [Doctrine ORM Mapping MappingException]

Doctrine2 关联持久性

Doctrine2 多对一关联不会使用 JOIN 查询