1
我只是Symfony的新手,所以請耐心等待。ManyToOne主義數據庫與Extra Field Symfony的關係3
我想創建Symfony的數據庫關係,讓說..
我有實體Currency
,AddRate
和AddRateCurrency
這些實體將完成3分簡單的事情。
- 用戶將使用
Currency
實體添加不同類型的貨幣。 - 用戶將選擇使用
AddRate
實體將這些貨幣添加到哪個分支。 - 用戶將使用
AddRateCurrency
更新每種貨幣的匯率。
,我遇到的問題是
預期類型的價值 「學說\ COMMON \收藏\收藏|陣」 的關聯域 「MontealBundle \實體\ AddRate#$貨幣」,得到了 「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;
}
}