"关联是指反面字段"&"映射是否相互矛盾"

Mar*_*rco 4 mapping orm symfony doctrine-orm

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

  • 映射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();
}
Run Code Online (Sandbox Code Playgroud)

预订实体:

/**
 * @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();
}
Run Code Online (Sandbox Code Playgroud)

Cyp*_*ian 6

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

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

  • @Marco:这不是一招,它是如何运作的:D (5认同)