I want to connect two entities
Dish that is in some DishCategory
Dish (category_id) with DishCategory (id)
There is an error:
The association AppBundle\Entity\Dish#categoryId refers to the inverse side field AppBundle\Entity\DishCategory#category_id which does not exist.
These are my entity classes
Dish Entity
class Dish
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*@ORM\ManyToOne(targetEntity = "DishCategory",inversedBy="category_id",cascade={"persist"})
* @ORM\JoinColumn(name="id",referencedColumnName="id")
*/
private $categoryId;
}
DishCategory Entity
class DishCategory
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToMany(targetEntity="Dish", mappedBy="category_id")
* @ORM\JoinColumn(name="category_id",referencedColumnName="id")
*/
private $id;
}
In DishController I run this function to repository
$dishes = $em->getRepository('AppBundle:Dish')->findAllAsArray();
And that how DishRepository looks
public function findAllAsArray()
{
return $q = $this->createQueryBuilder('d')
->join('d.categoryId','c')
->select('d.id as id_dish','c.id as id_cat','d.price')
->orderBy('c.position', 'asc')
->orderBy('d.position', 'asc')
->getQuery()
->getResult(Query::HYDRATE_ARRAY);
}
I have read many tutorials about OneToMany but still I cant find where is the problem :(
Still getting error:
The association AppBundle\Entity\Dish#categoryId refers to the inverse side field AppBundle\Entity\DishCategory#category_id which does not exist.
:(
I guess category_id is a foreign key. Don't map foreign keys to fields in an entit because:
your DishCategory entity is the Owning side because it holds the foreign key. By the way update your code as below:
Dish entity
DishCategory entity
Then in your DishRepository
But I don't see the point of doing a double orderBy because multiple calls to orderBy do not stack, use addOrderBy if you want to achieve this. And also Query::HYDRATE_ARRAY is not necessary, I guess, because you will have an array due to the custom query, not and entity.