I Have three Class:
- Product
- Component
- ProductComponent
And I want to create relation many to many with extra fields (quantity of component), product is build by components so i created three Class like:
Product:
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="Justart\ProductsBundle\Repository\ProductRepository")
*
* @GRID\Source(columns="id,name,code,category.name,active")
*/
class Product
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*
*/
private $name;
/**
* @var array
* @ORM\OneToMany(targetEntity="ProductComponent", mappedBy="product")
*
*/
private $components;
}
Component:
/**
* Component
*
* @ORM\Table(name="component")
* @ORM\Entity(repositoryClass="Justart\ProductsBundle\Repository\ComponentRepository")
*
*/
class Component
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @GRID\Column(title="ID", type="number")
*/
private $id;
/**
* @var string
*
* @Assert\NotBlank()
* @ORM\Column(name="name", type="string", length=255)
*
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="ProductComponent", mappedBy="component")
*/
private $componentProducts;
}
ProductComponent:
/**
* ProductComponent
*
* @ORM\Table(name="product_component")
* @ORM\Entity(repositoryClass="Justart\ProductsBundle\Repository\ProductComponentRepository")
*/
class ProductComponent
{
/**
* @var int
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Product", inversedBy="product_component")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
*/
private $product;
/**
* @var int
*
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Component", inversedBy="product_component")
* @ORM\JoinColumn(name="component_id", referencedColumnName="id", nullable=false)
*/
private $component;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer")
*/
private $quantity;
/**
* @var string
*
* @ORM\Column(name="unit", type="string")
*/
private $unit;
}
I get schema like this :
but if I get Product and use method getComponents i get empty array(), additionaly i get two doctrine errors:
First:
...\Entity\Product
The mappings ...\Entity\Product#components and ...\Entity\ProductComponent#product are inconsistent with each other.
Second:
...\Entity\ProductComponent
The association ...\Entity\ProductComponent#product refers to the inverse side field ...\Entity\Product#product_component which does not exist.
The association ...\Entity\ProductComponent#component refers to the inverse side field ...\Entity\Component#product_component which does not exist.
What do i do wrong ?
So I change property name's to match with mapping and i have no error but still I don't have components in product Object
public function testAction(Request $request, Product $product = null){
return $this->render(
'ProductsBundle:Product:add.html.twig', array(
'product' => $product,
)
);
}
when i dumped product in view i have empty collection of col1: elements[]
Product {#439 ▼
-id: 1
-name: "test"
-code: "rerer"
-category: ProductCategory {#446 ▶}
-product_component: PersistentCollection {#462 ▼
-snapshot: []
-owner: Product {#439}
-association: array:15 [ …15]
-em: EntityManager {#91 …10}
-backRefFieldName: "product"
-typeClass: ClassMetadata {#444 …}
-isDirty: false
-initialized: false
-coll: ArrayCollection {#463 ▼
-elements: []
}
}
-active: true
}