1

I Have three Class:

  1. Product
  2. Component
  3. 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
}
Andy
  • 49,085
  • 60
  • 166
  • 233
goq123
  • 59
  • 1
  • 10
  • 1
    Possible duplicate of [Doctrine2: Best way to handle many-to-many with extra columns in reference table](http://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle-many-to-many-with-extra-columns-in-reference-table) – Vamsi Krishna B Mar 11 '16 at 20:15

1 Answers1

0

Check The docs on associations for more info on the matter. In your case you have invalid references:

class ProductComponent
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="components")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
     */
    private $product;

    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Component", inversedBy="componentProducts")
     * @ORM\JoinColumn(name="component_id", referencedColumnName="id", nullable=false)
     */
    private $component;

Doctrine is looking for the Product::$product_component property, which doesn't exist.

Fyntasia
  • 1,133
  • 6
  • 19
  • so i have to create private $product_component and what annotation should I add ? – goq123 Mar 10 '16 at 13:18
  • No, just edit your ProductComponent inversion like I did in my example. (and maybe read up on the documentation :3) – Fyntasia Mar 10 '16 at 13:19
  • ok i do it, but what should I do to get in Product Object, components which are in related with product? – goq123 Mar 10 '16 at 13:46