为什么Symfony和Doctrine在插入/更新记录时使用__toString而不是获取Id

时间:2013-01-12 05:02:22

标签: symfony doctrine-orm

我有一个包含4个版本的表单但有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;
    }
}

MeatProducers:

<?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方法?

1 个答案:

答案 0 :(得分:3)

这里的问题不在于__toString,它位于您的映射注释中。您不应该使用@Column进行关联映射。相反,使用@JoinColumn:

/**
 * @ORM\ManyToOne(targetEntity="Promotions")
 * @ORM\JoinColumn(name="promotion_id", referencedColumnName="id")
 */
private $promotion;