多对多原则



标签实体:

<?php
namespace AppEntity;
use AppEntity;
use DoctrineORMMapping;
/**
* @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 AppEntity;
use AppEntity;
use DoctrineORMMapping;
/**
* @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;
}
}

我正在尝试创建多对多关联映射。我关注此链接:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html以进行此映射。

但当我试图更新学说时,出现了错误:

[DDoctrine\DBAL\DBALException]
执行"ALTER TABLE holiday_tags DROP PRIMARY KEY"时发生异常:
SQLSTATE[42000]:语法错误或访问冲突:1075表定义不正确;只能有一个自动列,并且必须将其定义为关键

[PDOException]
SQLSTATE[42000]:语法错误或访问冲突:1075表定义不正确;只能有一个自动列,并且必须将其定义为关键

orm:schema工具:更新[-complete][-dump-sql][--force]

更新

实体类别:

<?php
namespace App;
use DoctrineORMMapping 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,但是它将其命名为holidayppackages_tag。。。

您正试图创建两次相同的表"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字段上没有用于再次创建相同表的注释

相关内容

  • 没有找到相关文章

最新更新