Hey guys i have an error (i think that i do a really idiot error but i don't see were) trying to waste time i ask you the question. The error is that my foreign keys is incorrectly formed. Cannot be more precise ... Thx to all people who will try to answer or search :p
My UserInterestEntity :
/**
* UserInterest
*
* @ORM\Table(name="user_interest", indexes {@ORM\Index(name="userInterest_category_id_fk", columns={"category_id"})})
* @ORM\Entity
*/
class UserInterest
{
/**
* @var \AppBundle\Entity\Category
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Category")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* })
*/
private $category;
}
My Category Entity :
/**
* UserInterest
*
* @ORM\Table(name="Category", uniqueConstraints {@ORM\UniqueConstraint(name="category_id_uindex", columns={"id"})})
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\OneToOne(targetEntity="UserInterest", inversedBy="Category")
*/
protected $id;
The error is that when i do : php bin/console doctrine:schema:update --force tat tells me : Foreign key constraint is incorrectly formed
The relation must not be mapped on the ID but on a separate property. On the category class you can create an userIntrests property defining the userIntrests that have this category. In example:
class Category
{
/**
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="UserInterest", mappedBy="category")
*/
protected $intrests;
}
class UserIntrest
{
/**
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="intrests")
*/
protected $category;
}
Use this for reference https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-bidirectional
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments