2017-01-08 171 views
0

標籤實體:學說許多一對多

<?php 

namespace App\Entity; 

use App\Entity; 
use Doctrine\ORM\Mapping; 

/** 
* @Entity 
* @Table(name="tags", options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"}) 
*/ 
class Tags extends Entity { 

    /** 
    * Many Tags have Many HolidayPackages. 
    * @ManyToMany(targetEntity="HolidayPackages", mappedBy="tags") 
    * @JoinTable(name="holiday_tags", 
    *  joinColumns={@JoinColumn(name="tid", referencedColumnName="id")}, 
    *  inverseJoinColumns={@JoinColumn(name="hpid", referencedColumnName="id")} 
    *  ) 
    */ 
    protected $holiday_packages; 

    /** 
    * @Column(type="string", length=255) 
    * @var string 
    */ 
    protected $tags; 

    /** 
    * @return mixed 
    */ 
    public function getHolidayPackages() { 
     return $this->holiday_packages; 
    } 

    /** 
    * @param mixed $holiday_packages 
    */ 
    public function setHolidayPackages($holiday_packages) { 
     $this->holiday_packages = $holiday_packages; 
    } 

    /** 
    * @return string 
    */ 
    public function getTags() { 
     return $this->tags; 
    } 

    /** 
    * @param string $tags 
    */ 
    public function setTags($tags) { 
     $this->tags = $tags; 
    } 
} 

自由行:

<?php 

namespace App\Entity; 

use App\Entity; 
use Doctrine\ORM\Mapping; 

/** 
* @Entity 
* @Table(name="holiday_packages", options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"}) 
*/ 
class HolidayPackages extends Entity { 

    /** 
    * Many HolidayPackages have Many Tags. 
    * @ManyToMany(targetEntity="Tags", inversedBy="holiday_packages") 
    * @JoinTable(name="holiday_tags", 
    *  joinColumns={@JoinColumn(name="hpid", referencedColumnName="id")}, 
    *  inverseJoinColumns={@JoinColumn(name="tid", referencedColumnName="id")} 
    *  ) 
    */ 
    protected $tags; 

    /** 
    * @return mixed 
    */ 
    public function getTags() { 
     return $this->tags; 
    } 

    /** 
    * @param mixed $tags 
    */ 
    public function setTags($tags) { 
     $this->tags = $tags; 
    } 
} 

我想創造出許多一對多ASSOCATION映射。我按照這個鏈接: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html來做這個映射。

但是,當我嘗試更新的教義,錯誤occure:

[主義\ DBAL \ DBALException]執行 'ALTER TABLE holiday_tags DROP PRIMARY KEY' 發生
例外:
SQLSTATE [42000]:語法錯誤或訪問衝突:1075不正確的表定義; SQLSTATE [42000]:語法錯誤或訪問衝突:1075不正確的表定義;錯誤的表定義;錯誤的表定義;錯誤的表定義;錯誤的表定義;錯誤的表定義。只能有一個自動柱,它必須被定義爲一個鍵

ORM:架構工具:更新[--complete] [--dump-SQL] [--force]

UPDATE

實體類:

<?php 

namespace App; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @MappedSuperclass 
* @HasLifecycleCallbacks() 
*/ 
abstract class Entity 
{ 
    /** 
    * @var integer 
    * 
    * @Column(name="id", type="integer") 
    * @Id 
    * @GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @Column(type="datetime") 
    * @var \DateTime 
    */ 
    protected $created_at; 

    /** 
    * @Column(type="datetime", nullable=true) 
    * @var \DateTime 
    */ 
    protected $updated_at; 

    /** 
    * Constructor 
    */ 
    public function __construct() { 
     $this->setCreatedAt(new \DateTime()); 
     $this->setUpdatedAt(new \DateTime()); 
    } 

    /** 
    * @PreUpdate 
    */ 
    public function setUpdatedValue() { 
     $this->setUpdatedAt(new \DateTime()); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() { 
     return $this->id; 
    } 

    /** 
    * @param $created_at 
    */ 
    public function setCreatedAt($created_at) { 
     $this->created_at = $created_at; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function getCreatedAt() { 
     return $this->created_at->format('d/m/Y H:i'); 
    } 

    /** 
    * @param $updated_at 
    */ 
    public function setUpdatedAt($updated_at) { 
     $this->updated_at = $updated_at; 
    } 

    /** 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() { 
     return $this->updated_at; 
    } 
} 

它創建一個類holidaypackages_tags但還是給錯誤,我指定的名稱作爲holiday_tags但它命名holidaypackages_tags ...

+0

這兩個領域具有@ManyToMany註釋,必須在與'ArrayCollection'初始化構造函數。 –

回答

0

您正在嘗試兩次創建相同的表格"holiday_tags"

試試這個:

class Tags extends Entity { 

    /** 
    * Many Tags have Many HolidayPackages. 
    * @ManyToMany(targetEntity="HolidayPackages", mappedBy="tags") 
    */ 
    protected $holiday_packages; 

//... 
} 


class HolidayPackages extends Entity { 

    /** 
    * Many HolidayPackages have Many Tags. 
    * @ManyToMany(targetEntity="Tags", inversedBy="holiday_packages") 
    * @JoinTable(name="holiday_tags", 
    *  joinColumns={@JoinColumn(name="hpid", referencedColumnName="id")}, 
    *  inverseJoinColumns={@JoinColumn(name="tid", referencedColumnName="id")} 
    *  ) 
    */ 
    protected $tags; 
//... 
} 

注意,沒有註解來$holiday_packages場再次創建相同的表