簡體   English   中英

Symfony2 Doctrine2-通過doctrine:mapping:import從現有數據庫生成一對多注釋

[英]Symfony2 Doctrine2 - generate One-To-Many annotation from existing database by doctrine:mapping:import

我想通過使用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 AcmeBlogBundle annotation
2 $ php app/console doctrine:generate:entities AcmeBlogBundle

但是現在該學說僅在多方“ ProviderCountry”表中檢測到ManyToOne關系

如果我需要添加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庫內部的變化,

您可以通過更改

供應商/教義/ 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

您會發現雙方都是單向關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM