0

My webapp has the following: ProductsEntity which can have parts that are stored in the PartsEntity, but each Part has it's own value..

Like: Product can have a Part "Length", and that length is different for each Product, so I want to store that in the ProductPartValue table.

For each Product, I want to show all the Parts that are stored in the Parts table. And that you can add to each Part for the Product a Value.

But how do I achieve that in Symfony2, with help of Doctrine?

It is a ManyToMany relation of course (or am I even wrong with that?). All done with Annotations for Doctrine. But I can't get all info the right way, without ugly hacking the code. Any tips? So that I can fill in when editing a Product the Values in the fields list that is based on the PartsTable?

CODE:

/**
 * @ORM\Table()
 * @ORM\Entity
 */
class ProductPartValue
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Part", inversedBy="parts")
     * @ORM\JoinColumn(name="part_id", referencedColumnName="id")
     */
    protected $part;

     /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="products")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    protected $product;

    /**
     * @var string
     * @ORM\Column(name="value", type="string", length=255, nullable=true)
     */
    protected $value;
}


class Parts
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    public $name;

     /**
     * @ORM\OneToMany(targetEntity="ProductPartValue" , mappedBy="parts" , cascade={"all"})
     * */
    protected $parts;
}


class Products
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="info", type="string", length=255)
     */
    protected $info;

    /**
     * @ORM\OneToMany(targetEntity="ProductPartValue", mappedBy="products",  cascade={"all"})
     */
    protected $productsvalues;
}

For the Product Form Type I have this:

$builder->add('productPartValue','collection', array(
                'type' => new \xx\TheBundle\Form\productPartValueType(), 
                'allow_add' => false, 
                'by_reference' => false
        ));

$builder->add('parts','entity', array(
    'class' => 'xxTheBundle:Parts',
    'property' => 'name',
)); 

But then there is only a select box shown with the Parts, but not a possibility to add values to it. See image: http://i.stack.imgur.com/SBTL4.png

With help of copndz I added this, but then there is the notice "Undefined variable: productPartValue" in getParts():

<?php


    ////// PRODUCT TYPE ///////

    /**
     * @ORM\OneToMany(targetEntity="ProductPartValue", mappedBy="specification",  cascade={"all"})
     */
    protected $productPartValue;


    /**
     * Add productPartValue
     */
    public function addProductPartValue(\The\Bundle\Entity\ProductPartValue $productPartValue)
    {
        $this->productPartValue[] = $productPartValue;

        return $this;
    }

    /**
     * Remove productPartValue
     */
    public function removeProductPartValue(\The\Bundle\Entity\ProductPartValue $productPartValue)
    {
        $this->productPartValue->removeElement($productPartValue);
    }

    /**
     * Get productPartValue
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getProductPartValue()
    {
        return $this->productPartValue;
    }



    public function getParts() 
    { 
        $parts = new ArrayCollection(); 
        foreach($productPartValue as $part){ 
            $parts = $part->getPart(); 
        } 

        return $parts; 
    }
4

1 回答 1

0

我认为有关表单集合的文档会对您有很大帮助!

创建一个产品类型:

$formBuilder->add('productValues','collection');

创建一个 ProductPartValueType:

$formBuilder->add('part','entity')
            ->add('value')
于 2013-06-06T15:26:11.573 回答