Search code examples
symfonyormone-to-manymany-to-one

The mappings and are inconsistent with each other?


Query Warning message from Symfony Profiler:

Klient Entity

The mappings AdminBundle\Entity\Klient#contacts and AdminBundle\Entity\KlientContact#fkKlient are inconsistent with each other.

KlientContact Entity

The association AdminBundle\Entity\KlientContact#fkKlient refers to the inverse side field AdminBundle\Entity\Klient#nip which is not defined as association. The association AdminBundle\Entity\KlientContact#fkKlient refers to the inverse side field AdminBundle\Entity\Klient#nip which does not exist.

What did I do wrong ? I assume that there is something that I am missing on Klient#nip (which is mapped as Id field)

[Klient Entity]

<?php

namespace AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AdminBundle\Entity\KlientContact;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Klient
 *
 * @ORM\Table(name="klient")
 * @ORM\Entity(repositoryClass="AdminBundle\Repository\KlientRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Klient
{
    public function __construct()
    {
        $this->contacts = new ArrayCollection();
    }

    public function __toString() {
        return "NIP: ".$this->nip .", NAZWA: ". $this->name .", TEL: ". $this->tel;
    }

    /**
     * @var string
     * @ORM\Id
     * @ORM\Column(name="nip", type="string", length=255)
     * 
     */
    private $nip;

    /**
     * @var string
     *
     * @ORM\Column(name="regon", type="string", length=255)
     */
    private $regon;

    /**
     * @var \DateTime
     * @ORM\Column(name="inserted_at", type="datetime", nullable=true)  
     */
    private $insertedAt;

    /**
     * @var \DateTime
     * @ORM\Column(name="last_edited_at", type="datetime", nullable=true)  
     */
    private $lastEditedAt;    

    /**
     * @var string
     *
     * @ORM\Column(name="krs", type="string", length=255)
     */
    private $krs;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=255)
     */
    private $address;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="tel", type="string", length=255, nullable=true)
     */
    private $tel;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true)
     */
    private $email;

    /**
     * @var type
     * 
     * @ORM\OneToMany(targetEntity="KlientContact", mappedBy="fkKlient") 
     */
    private $contacts; // optional properyty holds an array of contact objects - this is not a fild in database


    /**
     * Set nip
     *
     * @param string $nip
     *
     * @return Klient
     */
    public function setNip($nip)
    {
        $this->nip = $nip;

        return $this;
    }

    /**
     * Get nip
     *
     * @return string
     */
    public function getNip()
    {
        return $this->nip;
    }

    /**
     * Set regon
     *
     * @param string $regon
     *
     * @return Klient
     */
    public function setRegon($regon)
    {
        $this->regon = $regon;

        return $this;
    }

    /**
     * Get regon
     *
     * @return string
     */
    public function getRegon()
    {
        return $this->regon;
    }

    /**
     * Set krs
     *
     * @param string $krs
     *
     * @return Klient
     */
    public function setKrs($krs)
    {
        $this->krs = $krs;

        return $this;
    }

    /**
     * Get krs
     *
     * @return string
     */
    public function getKrs()
    {
        return $this->krs;
    }

    /**
     * Set address
     *
     * @param string $address
     *
     * @return Klient
     */
    public function setAddress($address)
    {
        $this->address = $address;

        return $this;
    }

    /**
     * Get address
     *
     * @return string
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Klient
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set tel
     *
     * @param string $tel
     *
     * @return Klient
     */
    public function setTel($tel)
    {
        $this->tel = $tel;

        return $this;
    }

    /**
     * Get tel
     *
     * @return string
     */
    public function getTel()
    {
        return $this->tel;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Klient
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Add contact
     *
     * @param \AdminBundle\Entity\KlientContact $contact
     *
     * @return Klient
     */
    public function addContact(\AdminBundle\Entity\KlientContact $contact)
    {
        $this->contacts[] = $contact;

        return $this;
    }

    /**
     * Remove contact
     *
     * @param \AdminBundle\Entity\KlientContact $contact
     */
    public function removeContact(\AdminBundle\Entity\KlientContact $contact)
    {
        $this->contacts->removeElement($contact);
    }

    /**
     * Get contacts
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getContacts()
    {
        return $this->contacts;
    }

    /**
     * Set insertedAt
     *
     * @param \DateTime $insertedAt
     *
     * @return Klient
     */
    public function setInsertedAt(\DateTime $insertedAt)
    {
        $this->insertedAt = $insertedAt;

        return $this;
    }

    /**
     * Get insertedAt
     *
     * @return \DateTime
     */
    public function getInsertedAt()
    {
        return $this->insertedAt;
    }

    /**
     * Set lastEditedAt
     *
     * @param \DateTime $lastEditedAt
     *
     * @return Klient
     */
    public function setLastEditedAt(\DateTime $lastEditedAt)
    {
        $this->lastEditedAt = $lastEditedAt;

        return $this;
    }

    /**
     * Get lastEditedAt
     *
     * @return \DateTime
     */
    public function getLastEditedAt()
    {
        return $this->lastEditedAt;
    }

    /**
    *
    * @ORM\PrePersist
    * @ORM\PreUpdate
    */
   public function updatedTimestamps()
   {
       $this->setLastEditedAt(new \DateTime('now'));

       if ($this->getInsertedAt() == null) {
           $this->setInsertedAt(new \DateTime('now'));
       }
   }
}

[KlientContact Entity]

<?php

namespace AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AdminBundle\Entity\Klient;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * KlientContact
 *
 * @ORM\Table(name="klient_contact")
 * @ORM\Entity(repositoryClass="AdminBundle\Repository\KlientContactRepository")
 * @ORM\HasLifecycleCallbacks
 */
class KlientContact implements UserInterface
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \DateTime
     * 
     * @ORM\Column(name="inserted_at", type="datetime", nullable=true)  
     */
    private $insertedAt;

    /**
     * @var \DateTime
     * 
     * @ORM\Column(name="last_edited_at", type="datetime", nullable=true)  
     */
    private $lastEditedAt;      

    /**
     * @ORM\ManyToOne(targetEntity="Klient", inversedBy="nip")
     * @ORM\JoinColumn(name="fk_klient", referencedColumnName="nip")
     */
    private $fkKlient;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="surname", type="string", length=255)
     */
    private $surname;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    private $email;



    /**
     * @var string
     *
     * @ORM\Column(name="phone", type="string", length=255)
     */
    private $phone;

    /**
     * @var string
     *
     * @ORM\Column(name="login", type="string", length=255, unique=true)
     */
    private $login;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="text")
     */
    private $password;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return KlientContact
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set surname
     *
     * @param string $surname
     *
     * @return KlientContact
     */
    public function setSurname($surname)
    {
        $this->surname = $surname;

        return $this;
    }

    /**
     * Get surname
     *
     * @return string
     */
    public function getSurname()
    {
        return $this->surname;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return KlientContact
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set phone
     *
     * @param string $phone
     *
     * @return KlientContact
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Set login
     *
     * @param string $login
     *
     * @return KlientContact
     */
    public function setLogin($login)
    {
        $this->login = $login;

        return $this;
    }

    /**
     * Get login
     *
     * @return string
     */
    public function getLogin()
    {
        return $this->login;
    }

    /**
     * Set password
     *
     * @param string $password
     *
     * @return KlientContact
     */
    public function setPassword($password)
    {
        $hash = password_hash($password, PASSWORD_BCRYPT , ['cost' => 5]);
        $this->password = $hash;

        return $this;
    }

    /**
     * Get password
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set fkKlient
     *
     * @param \AdminBundle\Entity\Klient $fkKlient
     *
     * @return KlientContact
     */
    public function setFkKlient(\AdminBundle\Entity\Klient $fkKlient = null)
    {
        $this->fkKlient = $fkKlient;

        return $this;
    }

    /**
     * Get fkKlient
     *
     * @return \AdminBundle\Entity\Klient
     */
    public function getFkKlient()
    {
        return $this->fkKlient;
    }

    public function eraseCredentials() {

    }

    public function getRoles() {
        return ['ROLE_USER'];
    }

    public function getSalt() {
        return null;
    }

    public function getUsername() {

    }


    /**
     * Set insertedAt
     *
     * @param \DateTime $insertedAt
     *
     * @return KlientContact
     */
    public function setInsertedAt(\DateTime $insertedAt)
    {
        $this->insertedAt = $insertedAt;

        return $this;
    }

    /**
     * Get insertedAt
     *
     * @return \DateTime
     */
    public function getInsertedAt()
    {
        return $this->insertedAt;
    }

    /**
     * Set lastEditedAt
     *
     * @param \DateTime $lastEditedAt
     *
     * @return KlientContact
     */
    public function setLastEditedAt(\DateTime $lastEditedAt)
    {
        $this->lastEditedAt = $lastEditedAt;

        return $this;
    }

    /**
     * Get lastEditedAt
     *
     * @return \DateTime
     */
    public function getLastEditedAt()
    {
        return $this->lastEditedAt;
    }

    /**
    *
    * @ORM\PrePersist
    * @ORM\PreUpdate
    */
   public function updatedTimestamps()
   {
       $this->setLastEditedAt(new \DateTime('now'));

       if ($this->getInsertedAt() == null) {
           $this->setInsertedAt(new \DateTime('now'));
       }
   }
}

Solution

  • The inverse side is wrong. Should be:

    [KlientContact Entity]

    /**
     * @ORM\ManyToOne(targetEntity="Klient", inversedBy="contacts")
     * @ORM\JoinColumn(name="fk_klient", referencedColumnName="nip")
     */
     private $fkKlient;