我是Symfony的新人,所以请耐心等待。
我想在Symfony中创建数据库关系,比方说..
我有实体Currency
,AddRate
和AddRateCurrency
这些实体将完成3个简单的事情。
Currency
实体添加不同类型的货币。AddRate
实体添加这些货币的分支。AddRateCurrency
更新每种货币的费率。我遇到的问题是
“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;
}
}
答案 0 :(得分:0)
在AddRate
实体中,删除setCurrency
功能。然后,Symfony将(自动)使用您的addCurrency
函数,因为$currency
是一个ArrayCollection。
您选择的变量名称会增加一些混乱。在AddRate
中,您可以考虑将$currency
重命名为$currencies
。