【问题标题】:"association refers to the inverse side field" & "mappings are inconsistent with each other"“关联是指反边场”&“映射相互不一致”
【发布时间】:2014-09-18 09:21:52
【问题描述】:

我有两个实体,一个用于预订,一个用于可以进行预订的桌子。 该实体具有多对多关系。当我尝试验证此架构时,我收到以下错误:

  • 映射 Test\TestBundle\Entity\Table#reservations 和
    Test\TestBundle\Entity\Reservation#tables 与每个不一致 其他。
  • 关联 Test\TestBundle\Entity\Reservation#tables
    指反边字段 Test\TestBundle\Entity\Table#tables 不存在。

我能得到一些帮助吗?我不知道我做错了什么。

表实体:

/**
*
* @ORM\Entity
* @ORM\Table(name="tables")
* @ORM\Entity(repositoryClass="Test\TestBundle\Repository\TableRepository")
*/
class Table {    
 /**
 * @var integer $id
 * @ORM\ID
 * @ORM\Column(name="id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="integer", length=3)
 */
protected $nmbr;

/**
 * @ORM\ManyToMany(targetEntity="Reservation", mappedBy="tables", cascade={"persist"})
 */
private $reservations;


public function addReservation($reservation) {
    $reservation->addTable($this);
    $this->reservations[] = $reservation;
}



public function getId() {
    return $this->id;
}

public function getNmbr() {
    return $this->nmbr;
}

public function getReservations() {
    return $this->reservations;
}

public function setId($id) {
    $this->id = $id;
}

public function setNmbr($nmbr) {
    $this->nmbr = $nmbr;
}

/**
 * overrides the array of reservations belonging to this tabke in $reservations
 * with the given array of reservations.
 * Also adds this table to every reservations in the array.
 *
 */
public function setReservations($reservations) {
    foreach ($reservations as $reservation) {
        $reservation->addTable($this);
    }
    $this->reservations = $reservation;
}

public function __toString() {
    return (string)$this->getNmbr();
}

public function __construct() {
    parent::__construct();
    $this->reservations = new ArrayCollection();
}

预订实体:

/**
 * @ORM\Entity(repositoryClass="Test\TestBundle\Repository\ReservationRepository")
 * @ORM\Table(name="reservations")
 * @ORM\HasLifecycleCallbacks()
 */
class Reservation {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;    

 /**
 * @ORM\ManyToMany(targetEntity="Table", inversedBy="tables")
 * @ORM\JoinTable(name="reservation_table")
 **/
protected $tables;    

/**
 * @ORM\Column(type="string", length=32)
 */
protected $firstname;

/**
 * @ORM\Column(type="string", length=32)
 */
protected $lastname;

/**
 * @ORM\Column(type="string", length=32)
 */
protected $phone;

/**
 * @ORM\Column(type="string", length=32)
 */
protected $email;

/**
 * @ORM\Column(type="text",  nullable=true)
 */
protected $message;

/**
 * @ORM\Column(type="date")
 */
protected $date;

 /**
 * @ORM\Column(type="string", length=32)
 */
protected $timeFrom;

 /**
 * @ORM\Column(type="string", length=32)
 */
protected $timeTo;

public function addTable($table) {
    $this->tables[] = $table;
}
public function getTables() {
    return $this->tables;
}

public function getId() {
    return $this->id;
}


public function getFirstname() {
    return $this->firstname;
}

public function getLastname() {
    return $this->lastname;
}

public function getPhone() {
    return $this->phone;
}

public function getEmail() {
    return $this->email;
}

public function getMessage() {
    return $this->message;
}

public function getDate() {
    return $this->date;
}

public function getTimeFrom() {
    return $this->timeFrom;
}

public function getTimeTo() {
    return $this->timeTo;
}

public function setId($id) {
    $this->id = $id;
}

public function setTables($tables) {
    $this->tables = $tables;
}

public function setFirstname($firstname) {
    $this->firstname = $firstname;
}

public function setLastname($lastname) {
    $this->lastname = $lastname;
}

public function setPhone($phone) {
    $this->phone = $phone;
}

public function setEmail($email) {
    $this->email = $email;
}

public function setMessage($message) {
    $this->message = $message;
}

public function setDate($date) {
    $this->date = $date;
}

public function setTimeFrom($timeFrom) {
    $this->timeFrom = $timeFrom;
}

public function setTimeTo($timeTo) {
    $this->timeTo = $timeTo;
}


public function __construct() {
    $this->tables = new \Doctrine\Common\Collections\ArrayCollection();
}

【问题讨论】:

    标签: symfony orm doctrine-orm mapping


    【解决方案1】:

    Reservation实体inversedBy属性中,你应该指向相应的字段,所以resevations,而不是tables

    /**
     * @ORM\ManyToMany(targetEntity="Table", inversedBy="reservations")
     * @ORM\JoinTable(name="reservation_table")
     **/
    protected $tables; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多