“关联指的是反面”字段“& “映射彼此不一致”

时间:2014-09-18 09:21:53

标签: symfony orm doctrine-orm mapping

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

  • 映射Test \ TestBundle \ Entity \ Table#reservations和
    Test \ TestBundle \ Entity \ Reservation#表与每个表不一致 其他。
  • 关联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();
}

1 个答案:

答案 0 :(得分:6)

Reservation实体inversedBy属性中,您应指向相应的字段,因此resevations,而不是tables

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