Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"association refers to the inverse side field" & "mappings are inconsistent with each other"

I have two Entities, one for reservations and one for tables where the reservations can be placed on. This entities have a ManyToMany relation. When I try to validate this schema I get the following errors:

  • The mappings Test\TestBundle\Entity\Table#reservations and
    Test\TestBundle\Entity\Reservation#tables are inconsistent with each other.
  • The association Test\TestBundle\Entity\Reservation#tables
    refers to the inverse side field Test\TestBundle\Entity\Table#tables which does not exist.

Can I get some help with this? I can't figure out what I'm doing wrong.

Table Entity:

/**
*
* @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();
}

Reservation Entity:

/**
 * @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();
}
like image 661
Marco Avatar asked Sep 18 '14 09:09

Marco


1 Answers

In Reservation entity inversedBy property, you should point the appropriate field, so resevations, not tables:

/**
 * @ORM\ManyToMany(targetEntity="Table", inversedBy="reservations")
 * @ORM\JoinTable(name="reservation_table")
 **/
protected $tables; 
like image 184
Cyprian Avatar answered Sep 28 '22 09:09

Cyprian