Symfony Select 选择绑定到实体布尔字段
Symfony Select choices bound to entity boolean fields
我正在尝试将 multiselect 的 options/choices 耦合到数据库中的一个字段。 Symfony 本身是否已经支持?
相关实体字段:
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isSpellchecked;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isCompleted;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isEmailed;
HTML 示例:
<select multiple>
<option value="isSpellchecked">Spellchecked</option>
<option value="isCompleted">Completed</option>
<option value="isEmailed">Emailed</option>
</select>
当然,我将使用 Bootstrap Multiple 作为 select 的前端实现。
问题是:如何将选项连接到字段,而无需在控制器中手动操作。
我是不是遗漏了什么?
在我看来,您只需在实体中映射一个属性。
/**
* @var array
*
* @ORM\Column(type="array", nullable=true)
*/
private $myChoices;
...
public function __construct()
{
$this->myChoices = [];
}
/**
* @return array
*/
public function getMyChoices()
{
return array_unique($this->myChoices);
}
/**
* @param array $myChoices
*/
public function setMyChoices(array $myChoices)
{
$this->myChoices = [];
foreach ($myChoices as $myChoice) {
$this->addMyChoice($myChoice);
}
return $this;
}
/**
* @param string $myChoice
*/
public function addMyChoice($myChoice)
{
if (!$this->hasMyChoice($myChoice)) {
$this->myChoices[] = $myChoice;
}
return $this;
}
/**
* @param string $myChoice
*
* @return bool
*/
public function hasMyChoice($myChoice)
{
return in_array($myChoice, $this->myChoices, true);
}
然后在你的 formType 中做一个经典的 :
->add('myChoices', ChoiceType::class, [
'choices' => [
'Spellchecked',
'Completed',
'Emailed',
],
'expanded' => true,
'multiple' => true,
])
然后,您的选择将作为数组保存在数据库中,如下所示:
mychoices field = a:2:{i:0;s:12:"Spellchecked";i:1;s:7:"Emailed";}
// which is like ['Spellchecked', 'Emailed']
您可以像这样检索数据:
$isSpellchecked = $MyEntity->hasMyChoice('Spellchecked'); // return true
$isCompleted = $MyEntity->hasMyChoice('Completed'); // return false
我正在尝试将 multiselect 的 options/choices 耦合到数据库中的一个字段。 Symfony 本身是否已经支持?
相关实体字段:
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isSpellchecked;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isCompleted;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isEmailed;
HTML 示例:
<select multiple>
<option value="isSpellchecked">Spellchecked</option>
<option value="isCompleted">Completed</option>
<option value="isEmailed">Emailed</option>
</select>
当然,我将使用 Bootstrap Multiple 作为 select 的前端实现。 问题是:如何将选项连接到字段,而无需在控制器中手动操作。
我是不是遗漏了什么?
在我看来,您只需在实体中映射一个属性。
/**
* @var array
*
* @ORM\Column(type="array", nullable=true)
*/
private $myChoices;
...
public function __construct()
{
$this->myChoices = [];
}
/**
* @return array
*/
public function getMyChoices()
{
return array_unique($this->myChoices);
}
/**
* @param array $myChoices
*/
public function setMyChoices(array $myChoices)
{
$this->myChoices = [];
foreach ($myChoices as $myChoice) {
$this->addMyChoice($myChoice);
}
return $this;
}
/**
* @param string $myChoice
*/
public function addMyChoice($myChoice)
{
if (!$this->hasMyChoice($myChoice)) {
$this->myChoices[] = $myChoice;
}
return $this;
}
/**
* @param string $myChoice
*
* @return bool
*/
public function hasMyChoice($myChoice)
{
return in_array($myChoice, $this->myChoices, true);
}
然后在你的 formType 中做一个经典的 :
->add('myChoices', ChoiceType::class, [
'choices' => [
'Spellchecked',
'Completed',
'Emailed',
],
'expanded' => true,
'multiple' => true,
])
然后,您的选择将作为数组保存在数据库中,如下所示:
mychoices field = a:2:{i:0;s:12:"Spellchecked";i:1;s:7:"Emailed";}
// which is like ['Spellchecked', 'Emailed']
您可以像这样检索数据:
$isSpellchecked = $MyEntity->hasMyChoice('Spellchecked'); // return true
$isCompleted = $MyEntity->hasMyChoice('Completed'); // return false