1

我有一个包含 4 个 realtionships 的表格,但其中 2 个有问题。这是我的表单的代码:

<?php

namespace Psw\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MeatType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name_pl')
            ->add('name_en')
            ->add('jm')
            ->add('vat')
            ->add('net_price')
            ->add('min')
            ->add('new')
            ->add('promotion', 'entity', array(
                    'class' => 'PswAdminBundle:Promotions',
                    'empty_value' => 'No promotion',
                    'required' => false
                ))
            ->add('promotion_price')
            ->add('producer', 'entity', array(
                    'class' => 'PswAdminBundle:MeatProducers'
                ))
            ->add('animals')
            ->add('categories')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Psw\AdminBundle\Entity\Meat'
        ));
    }

    public function getName()
    {
        return 'psw_adminbundle_meattype';
    }
}

和实体

肉:

<?php

namespace Psw\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

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

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="jm", type="string", length=10)
     */
    private $jm;

    /**
     * @var integer
     *
     * @ORM\Column(name="vat", type="integer")
     */
    private $vat;

    /**
     * @var float
     *
     * @ORM\Column(name="net_price", type="float")
     */
    private $net_price;

    /**
     * @var float
     *
     * @ORM\Column(name="min", type="float")
     */
    private $min;

    /**
     * @var boolean
     *
     * @ORM\Column(name="new", type="boolean")
     */
    private $new;

    /**
     * @var integer
     *
     * @ORM\Column(name="promotion", type="integer", nullable=true)
     * @ORM\ManyToOne(targetEntity="Promotions")
     */
    private $promotion;

    /**
     * @var float
     *  
     * @ORM\Column(name="promotion_price", type="float")
     *  
     */
    private $promotion_price;

    /**
     * @ORM\ManyToMany(targetEntity="Animals")
     * @ORM\JoinTable(name="meat_animals",
     *      joinColumns={@ORM\JoinColumn(name="meat_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="animal_id", referencedColumnName="id")}
     *      )
     **/
    private $animals;

    /**
     * @ORM\ManyToMany(targetEntity="MeatCategories")
     * @ORM\JoinTable(name="meat_meat_categories",
     *      joinColumns={@ORM\JoinColumn(name="meat_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
     *      )
     **/
    private $categories;

    /**
     * @ORM\Column(name="producer", type="integer")
     * 
     * @ORM\ManyToOne(targetEntity="MeatProducers", inversedBy="products")
     * @ORM\JoinColumn(name="producer", referencedColumnName="id")
     **/
    private $producer;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name_pl
     *
     * @param string $namePl
     * @return Meat
     */
    public function setNamePl($namePl)
    {
        $this->name_pl = $namePl;

        return $this;
    }

    /**
     * Get name_pl
     *
     * @return string 
     */
    public function getNamePl()
    {
        return $this->name_pl;
    }

    /**
     * Set name_en
     *
     * @param string $nameEn
     * @return Meat
     */
    public function setNameEn($nameEn)
    {
        $this->name_en = $nameEn;

        return $this;
    }

    /**
     * Get name_en
     *
     * @return string 
     */
    public function getNameEn()
    {
        return $this->name_en;
    }

    /**
     * Set jm
     *
     * @param string $jm
     * @return Meat
     */
    public function setJm($jm)
    {
        $this->jm = $jm;

        return $this;
    }

    /**
     * Get jm
     *
     * @return string 
     */
    public function getJm()
    {
        return $this->jm;
    }

    /**
     * Set vat
     *
     * @param integer $vat
     * @return Meat
     */
    public function setVat($vat)
    {
        $this->vat = $vat;

        return $this;
    }

    /**
     * Get vat
     *
     * @return integer 
     */
    public function getVat()
    {
        return $this->vat;
    }

    /**
     * Set net_price
     *
     * @param float $netPrice
     * @return Meat
     */
    public function setNetPrice($netPrice)
    {
        $this->net_price = $netPrice;

        return $this;
    }

    /**
     * Get net_price
     *
     * @return float 
     */
    public function getNetPrice()
    {
        return $this->net_price;
    }

    /**
     * Set min
     *
     * @param float $min
     * @return Meat
     */
    public function setMin($min)
    {
        $this->min = $min;

        return $this;
    }

    /**
     * Get min
     *
     * @return float 
     */
    public function getMin()
    {
        return $this->min;
    }

    /**
     * Set new
     *
     * @param boolean $new
     * @return Meat
     */
    public function setNew($new)
    {
        $this->new = $new;

        return $this;
    }

    /**
     * Get new
     *
     * @return boolean 
     */
    public function getNew()
    {
        return $this->new;
    }

    /**
     * Set promotion
     *
     * @param integer $promotion
     * @return Meat
     */
    public function setPromotion($promotion)
    {
        $this->promotion = $promotion;

        return $this;
    }

    /**
     * Get promotion
     *
     * @return integer 
     */
    public function getPromotion()
    {
        return $this->promotion;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->animals = new ArrayCollection();
        $this->categories = new ArrayCollection();
    }

    /**
     * Add animals
     *
     * @param \Psw\AdminBundle\Entity\Animals $animals
     * @return Meat
     */
    public function addAnimal(\Psw\AdminBundle\Entity\Animals $animals)
    {
        $this->animals[] = $animals;

        return $this;
    }

    /**
     * Remove animals
     *
     * @param \Psw\AdminBundle\Entity\Animals $animals
     */
    public function removeAnimal(\Psw\AdminBundle\Entity\Animals $animals)
    {
        $this->animals->removeElement($animals);
    }

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

    /**
     * Add categories
     *
     * @param \Psw\AdminBundle\Entity\Meat_categories $categories
     * @return Meat
     */
    public function addCategorie(\Psw\AdminBundle\Entity\Meat_categories $categories)
    {
        $this->categories[] = $categories;

        return $this;
    }

    /**
     * Remove categories
     *
     * @param \Psw\AdminBundle\Entity\Meat_categories $categories
     */
    public function removeCategorie(\Psw\AdminBundle\Entity\Meat_categories $categories)
    {
        $this->categories->removeElement($categories);
    }

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

    /**
     * Set producer
     *
     * @param \Psw\AdminBundle\Entity\MeatProducers $producer
     * @return Meat
     */
    public function setProducer(\Psw\AdminBundle\Entity\MeatProducers $producer = null)
    {
        $this->producer = $producer;

        return $this;
    }

    /**
     * Get producer
     *
     * @return \Psw\AdminBundle\Entity\MeatProducers 
     */
    public function getProducer()
    {
        return $this->producer;
    }

    /**
     * Set promotion_price
     *
     * @param float $promotionPrice
     * @return Meat
     */
    public function setPromotionPrice($promotionPrice)
    {
        $this->promotion_price = $promotionPrice;

        return $this;
    }

    /**
     * Get promotion_price
     *
     * @return float 
     */
    public function getPromotionPrice()
    {
        return $this->promotion_price;
    }
}

肉类生产商:

<?php

namespace Psw\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MeatProducers
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class MeatProducers
{
    /**
     * @var integer
     *
     * @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;

    /**
     * @ORM\OneToMany(targetEntity="Meat", mappedBy="producer")
     **/
    private $products;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return MeatProducers
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    public function __toString()
    {
        return $this->name;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->products = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add products
     *
     * @param \Psw\AdminBundle\Entity\Meat $products
     * @return MeatProducers
     */
    public function addProduct(\Psw\AdminBundle\Entity\Meat $products)
    {
        $this->products[] = $products;

        return $this;
    }

    /**
     * Remove products
     *
     * @param \Psw\AdminBundle\Entity\Meat $products
     */
    public function removeProduct(\Psw\AdminBundle\Entity\Meat $products)
    {
        $this->products->removeElement($products);
    }

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

关于动物和类别的多对多理论没有问题。但是我拥有多对一的促销和制作人会导致一些问题。

问题是当尝试插入/更新记录时,学说调用 __toString 方法而不是 getId 来获取要放入数据库的值。

我与其他实体有这样的工作示例,它们几乎相同,但在这种情况下它只是不想工作。

控制器代码来自 crud 生成器,如有必要,我没有更改任何内容,我可以发布它。

问题是如何让它使用 getId 方法?

4

1 回答 1

3

这里的问题不在于 __toString,而在于您的映射注释。您不应将 @Column 用于关联映射。相反,使用@JoinColumn:

/**
 * @ORM\ManyToOne(targetEntity="Promotions")
 * @ORM\JoinColumn(name="promotion_id", referencedColumnName="id")
 */
private $promotion;
于 2013-01-12T15:08:55.300 回答