1
votes

I'm just new in Symfony so please bear with me.

I want to create a database relationship in Symfony, let say..

I have Entity Currency, AddRate and AddRateCurrency These entities would accomplish 3 simple things.

  1. The User will gonna add different types of currencies using Currency entity.
  2. The User will gonna choose which branch these currencies must be added using AddRate entity.
  3. The User will gonna update the rates of each currency using AddRateCurrency.

The problem that I encountered is

Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "MontealBundle\Entity\AddRate#$currency", got "MontealBundle\Entity\Currency" instead.

On adding rate AddRateForm I used EntityType to embed my form.

 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.'))
                )
            ))
        ;
    }

On Currency Entity

/**
 * @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();
    }
}

On AddRate Entity

/**
 * @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;
    }
}

On AddRateCurrency Entity

/**
 * @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

1 Answers

0
votes

In your AddRate entity, remove the setCurrency function. Symfony will then (automatically) use your addCurrency function instead since $currency is an ArrayCollection.

Your choice of variable names adds to the confusion somewhat. In AddRate, you may want to consider renaming $currency to $currencies.