我有两个实体
员工和FreeDay
一名员工可以有一天或多天的免费日。免费日被定义为星期几,因此星期一=> 1,星期二=> 2等..
在员工表单视图中,我想将FreeDay实体显示为复选框列表,如下所示:
但我收到了这个错误:
警告:spl_object_hash()期望参数1为对象,给定整数
我真的很困惑,而且我不知道如何解决这个问题。
实体代码如下:
员工实体:
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',
));
}
}
答案 0 :(得分:3)
在表单构建器中使用entity选项需要选项是该实体的对象,请参阅symfony文档:http://symfony.com/doc/current/reference/forms/types/entity.html#using-choices
当你将一个数组传递给它时,你总会遇到这个错误。
要实现您在问题中概述的内容,您需要在数据库中使用相关的ID和名称(如您在getDays函数中列出的名称),因此星期一将在您的数据库中拥有1的id。
如果您只想要所有选项,请参阅文档的此部分:http://symfony.com/doc/current/reference/forms/types/entity.html#basic-usage
但是,如果您想过滤结果,则需要一个查询构建器,如文档中所述:http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities
如果这对您不起作用或者我误解了这个问题,请告诉我。