ManyToOne Doctrine数据库与Extra Field Symfony 3的关系

时间:2017-04-02 16:58:59

标签: php entity-framework symfony doctrine-orm doctrine

我是Symfony的新人,所以请耐心等待。

我想在Symfony中创建数据库关系,比方说..

我有实体CurrencyAddRateAddRateCurrency 这些实体将完成3个简单的事情。

  1. 用户将使用Currency实体添加不同类型的货币。
  2. 用户将选择使用AddRate实体添加这些货币的分支。
  3. 用户将使用AddRateCurrency更新每种货币的费率。
  4. 我遇到的问题是

      

    “Doctrine \ Common \ Collections \ Collection | array”类型的预期值   对于关联字段“MontealBundle \ Entity \ AddRate#$ currency”,得到了   相反,“MontealBundle \ Entity \ Currency”。

    在添加费率AddRateForm时,我使用EntityType来嵌入我的表单。

     public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('branch', EntityType::class, array(
                    'label' => 'Branch',
                    'class'=>'MontealBundle\Entity\Branch',
                    'query_builder' => function (BranchRepository $er) {
                        return $er->findAllActiveBranches();
                    },
                    'choice_label' => 'name',
                    'placeholder' => 'Choose a Branch',
                    'empty_data' => null,
                    'required' => true,
                    'constraints' => array(
                        new NotBlank(array("message" => 'Branch is required.'))
                    )
                ))
                ->add('currency', EntityType::class, array(
                    'label' => 'Currency',
                    'class'=>'MontealBundle\Entity\Currency',
                    'query_builder' => function (CurrencyRepository $er) {
                        return $er->findAllActiveCurrencies();
                    },
                    'choice_label' => 'currency',
                    'placeholder' => 'Choose a Currency',
                    'empty_data' => null,
                    'multiple' => true,
                    // 'expanded' => true,
                    'required' => true,
                    'constraints' => array(
                        new NotBlank(array("message" => 'Currency is required.'))
                    )
                ))
            ;
        }
    

    Currency实体上

    /**
     * @ORM\Entity
     * @ORM\Table(name="currency")
     * @ORM\Entity(repositoryClass="MontealBundle\Repository\CurrencyRepository")
     * @ORM\HasLifecycleCallbacks()
     * @UniqueEntity(fields={"code"}, message="Code is already used.")
     * @UniqueEntity(fields={"currency"}, message="Currency is already used.")
     */
    class Currency
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity="Administrator")
         * @ORM\JoinColumn(name="administrator_id", referencedColumnName="id")
         */
        private $administrator;
    
        /**
         * @ORM\Column(type="string", unique=true)
         */
        private $currency;
    
        /**
         * @ORM\Column(type="string", unique=true)
         */
        private $code;
    
        /**
         * @ORM\Column(type="integer")
         */
        private $status;
    
        /**
         * @ORM\Column(type="date")
         */
        private $createdAt;
    
        /**
         * @ORM\Column(type="date", nullable=true)
         */
        private $updatedAt;
    
        /**
         * @return mixed
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return mixed
         */
        public function getAdministrator()
        {
            return $this->administrator;
        }
    
        /**
         * @param mixed $administrator
         */
        public function setAdministrator($administrator)
        {
            $this->administrator = $administrator;
        }
    
        /**
         * @return mixed
         */
        public function getCurrency()
        {
            return $this->currency;
        }
    
        /**
         * @param mixed $currency
         */
        public function setCurrency($currency)
        {
            $this->currency = $currency;
        }
    
        /**
         * @return mixed
         */
        public function getCode()
        {
            return $this->code;
        }
    
        /**
         * @param mixed $code
         */
        public function setCode($code)
        {
            $this->code = $code;
        }
    
        /**
         * @return mixed
         */
        public function getStatus()
        {
            return $this->status;
        }
    
        /**
         * @param mixed $status
         */
        public function setStatus($status)
        {
            $this->status = $status;
        }
    
        public function getCreatedAt()
        {
            return $this->createdAt;
        }
    
        /**
         * @ORM\PrePersist
         */
        public function setCreatedAt()
        {
            $this->createdAt = new \DateTime();
        }
    
        public function getUpdatedAt()
        {
            return $this->updatedAt;
        }
    
        /**
         * @ORM\PreUpdate()
         */
        public function setUpdatedAt()
        {
            $this->updatedAt = new \DateTime();
        }
    }
    

    AddRate实体上

    /**
     * @ORM\Entity
     * @ORM\Table(name="add_rate")
     * @ORM\Entity(repositoryClass="MontealBundle\Repository\AddRateRepository")
     * @ORM\HasLifecycleCallbacks()
     */
    class AddRate
    {
    
        /**
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity="Branch")
         * @ORM\JoinColumn(name="branch_id", referencedColumnName="id")
         */
        private $branch;
    
        /**
         * @ORM\OneToMany(targetEntity="AddRateCurrency", mappedBy="add_rate")
         */
        private $currency;
    
        public function __construct()
        {
            $this->currency = new ArrayCollection();
        }
    
        /**
         * @ORM\ManyToOne(targetEntity="Administrator")
         * @ORM\JoinColumn(name="administrator_id", referencedColumnName="id")
         */
        private $administrator;
    
        /**
         * @ORM\Column(type="date")
         */
        private $createdAt;
    
        /**
         * @return mixed
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return mixed
         */
        public function getBranch()
        {
            return $this->branch;
        }
    
        /**
         * @param mixed $branch
         */
        public function setBranch($branch)
        {
            $this->branch = $branch;
        }
    
        /**
         * @return mixed
         */
        public function getCurrency()
        {
            return $this->currency;
        }
    
        /**
         * @param mixed $currency
         */
        public function setCurrency($currency)
        {
            $this->currency = $currency;
        }
    
        public function addCurrency(Currency $currency) {
            $this->currency[] = $currency;
        }
    
        /**
         * @return mixed
         */
        public function getAdministrator()
        {
            return $this->administrator;
        }
    
        /**
         * @param mixed $administrator
         */
        public function setAdministrator($administrator)
        {
            $this->administrator = $administrator;
        }
    
        /**
         * @ORM\PrePersist
         */
        public function setCreatedAt()
        {
            $this->createdAt = new \DateTime();
        }
    
        public function getCreatedAt()
        {
            return $this->createdAt;
        }
    }
    

    AddRateCurrency实体上

    /**
     * @ORM\Entity
     * @ORM\Table(name="add_rate_currency")
     */
    class AddRateCurrency
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity="AddRate", inversedBy="AddRateCurrency")
         * @ORM\JoinColumn(nullable=false)
         */
        private $currency;
    
        /**
         * @ORM\Column(type="string")
         */
        private $rate;
    
        /**
         * @return mixed
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return mixed
         */
        public function getCurrency()
        {
            return $this->currency;
        }
    
        /**
         * @param mixed $currency
         */
        public function setCurrency($currency)
        {
            $this->currency = $currency;
        }
    
        /**
         * @return mixed
         */
        public function getRate()
        {
            return $this->rate;
        }
    
        /**
         * @param mixed $rate
         */
        public function setRate($rate)
        {
            $this->rate = $rate;
        }
    
    
    }
    

1 个答案:

答案 0 :(得分:0)

AddRate实体中,删除setCurrency功能。然后,Symfony将(自动)使用您的addCurrency函数,因为$currency是一个ArrayCollection。

您选择的变量名称会增加一些混乱。在AddRate中,您可以考虑将$currency重命名为$currencies