The mappings are inconsistent with each other

2019-06-18 16:35发布

问题:

I have problem with inconsistent mappings. I have in my application two entities - Contact (entity with contacts...) and Information, entities with informations to this contact (phones, emails, fax, websites etc.).

And In my Contact entity I made variables for each type, I need it in my application because this way is much easier:

/**
 * @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
 */
protected $contactInformations;

/**
 * @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
 */
protected $contactPhone;

/**
 * @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
 */
protected $contactFax;

/**
 * @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
 */
protected $contactWebsite;

/**
 * @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
 */
protected $contactEmail;

/**
 * @ORM\OneToMany( targetEntity = "RelationInformations" , mappedBy = "objectID", cascade={"persist"} )
 */
protected $contactCommunicator;

And for example getter for phones looks like:

/**
 * Get contactPhone
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getContactPhone()
{
    if ($this->contactPhone !== null) {
        foreach ($this->contactPhone->toArray() as &$info) {
            if ($info->getType() !== RelationInformations::TYPE_TELEPHONE) {
                $this->contactPhone->removeElement($info);
            }
        }
    }

    return $this->contactPhone;
}

This way i got only phones from my informations only by using this function so it's much easier in other places in application to get what I want.

RelationInformation Entity:

   /**
     * @var integer
     * @ORM\Column( name = "rnis_id" , type = "integer" , nullable = false );
     * @ORM\Id
     * @ORM\GeneratedValue( strategy = "AUTO")
     */
    private $id;

/**
 * @var integer
 * @ORM\ManyToOne( targetEntity = "RelationContact" , inversedBy = "contactInformations" )
 * @ORM\JoinColumn( name = "rnis_object_id" , referencedColumnName="rnct_id", nullable = false );
 */
private $objectID;

/**
 * @var string
 * @ORM\Column( name = "rnis_value" , type = "string" , nullable = false  )
 */
private $value;

/**
 * @var string
 * @ORM\Column( name = "rnis_type" , type = "string" , nullable = false , length = 1 )
 */
private $type;

/**
 * @var boolean
 * @ORM\Column( name = "rnis_active" , type = "boolean" , nullable = false )
 */
private $active;

/**
 * @var boolean
 * @ORM\Column( name = "rnis_default" , type = "boolean" , nullable = false )
 */
private $default;

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

/**
 * @var integer
 * @ORM\Column( name = "rnis_type_private_business" , type = "integer" , nullable = true )
 */
private $typePrivateBusiness;

The problem is that in my profiler i can see errors like bellow. Application works correctly but I want to solve this issue.

The mappings RelationContact#contactPhone and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactFax and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactWebsite and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactEmail and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactCommunicator and RelationInformations#objectID are inconsistent with each other.
The mappings RelationContact#contactBrand and RelationInformations#objectID are inconsistent with each other.

回答1:

You can't map the same OneToMany relations on the same mappedby key, so the beahviour of the doctrine mapping validation is to correctly pass the first contactInformations reference and fail on the other.

Try to map your entities as One-To-Many, Unidirectional with Join Table as described in the Doctrine2 doc reference

Hope this help