Php 在表单视图中使用复选框列表进行符号一对多
我有两个实体 雇员与自由日 一名员工可以有一天或多天的空闲时间。自由日定义为一周中的某一天,因此周一=>1,周二=>2等 在Employee Form视图中,我希望将FreeDay实体显示为复选框列表,如下所示: 但我有一个错误: 警告:spl_object_hash()要求参数1为object,给定整数 我真的很困惑,我不知道如何摆脱这个问题 实体代码如下所示: 员工实体:Php 在表单视图中使用复选框列表进行符号一对多,php,entity-framework,symfony,Php,Entity Framework,Symfony,我有两个实体 雇员与自由日 一名员工可以有一天或多天的空闲时间。自由日定义为一周中的某一天,因此周一=>1,周二=>2等 在Employee Form视图中,我希望将FreeDay实体显示为复选框列表,如下所示: 但我有一个错误: 警告:spl_object_hash()要求参数1为object,给定整数 我真的很困惑,我不知道如何摆脱这个问题 实体代码如下所示: 员工实体: namespace AppBundle\Entity; use Doctrine\ORM\Mapping as OR
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Employee
*
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class Employee
{
/**
* @ORM\OneToMany(targetEntity="FreeDay", mappedBy="employee", cascade={"persist","remove"},orphanRemoval=true)
*/
private $freedays;
/**
* Constructor
*/
public function __construct()
{
$this->freedays = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add freedays
*
* @param \AppBundle\Entity\Free $freeday
*
* @return Employee
*/
public function addFreeDay(\AppBundle\Entity\FreeDay $freeday)
{
$this->freedays[] = $freeday;
return $this;
}
/**
* Remove freeday
*
* @param \AppBundle\Entity\FreeDay $freeday
*/
public function removeFreeDay(\AppBundle\Entity\FreeDay $freeday)
{
$this->freedays->removeElement($freeday);
}
/**
* Get freeday
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getFreeDays()
{
return $this->freedays;
}
}
自由日实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
class FreeDay
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
* @ORM\ManyToOne(targetEntity="Employee", inversedBy="freedays")
* @ORM\JoinColumn(name="employee_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $employee;
/**
* @var int
*
* @ORM\Column(name="day", type="integer")
*/
private $day;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set day
*
* @param integer $day
*
* @return FreeDay
*/
public function setDay($day)
{
$this->day = $day;
return $this;
}
/**
* Get day
*
* @return int
*/
public function getDay()
{
return $this->day;
}
/**
* Set employee
*
* @param \AppBundle\Entity\Employee $employee
*
* @return FreeDay
*/
public function setEmployee(\AppBundle\Entity\Employee $employee = null)
{
$this->employee = $employee;
return $this;
}
/**
* Get employee
*
* @return \AppBundle\Entity\Employee
*/
public function getEmployee()
{
return $this->employee;
}
EmployeeType.php
class EmployeeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('freedays', EntityType::class, array(
'class'=>'AppBundle:FreeDay',
'choices'=>$this->getDays(),
'expanded'=>true,
'multiple'=>true
))
->add('save', SubmitType::class, array('label' => 'Salva'))
;
}
private function getDays() {
return array(
'Monday'=>1,
'Tuesday'=>2,
'Wednesday'=>3,
'Thursday'=>4,
'Friday'=>5,
'Saturday'=>6,
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Employee',
));
}
}
在form builder中使用实体选项要求选项是该实体的对象,请参见symfony文档中的以下内容: 当您向它传递数组时,总是会遇到此错误 为了实现你在问题中概述的内容,你需要在数据库中设置空闲日,并使用相关id和名称,就像你在getDays函数中列出的那样,因此周一在数据库中设置id为1 如果您只需要所有选项,请参阅文档的此部分: 但是,如果要筛选结果,则需要一个查询生成器,如文档中所述:
如果这对您不起作用,或者我误解了这个问题,请告诉我。你好,尼克,非常感谢您的回复。是的,在数据库中使用Days表可能是一个解决方案,但有没有一种方法可以在不创建db表的情况下实现这一点?我不知道,如果您想使用entity选项的话。因为实体的对象存储在数据库中,这就是entity form选项查找和返回的内容。此外,如果您决定使用我的答案,您的空闲日实体将需要一个_toString函数。