表单视图中带有复选框列表的一对多符号


Symfony one to many with checkbox list in form view

我有两个实体

员工和免费日

一名员工可以有一天或多天的空闲时间。空闲日定义为一周中的某一天,因此周一=>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',
    ));
}
}

在表单生成器中使用实体选项需要这些选项是该实体的对象,请参阅symfony文档中的此处:http://symfony.com/doc/current/reference/forms/types/entity.html#using-选择

当您将数组传递给它时,总会遇到此错误。

为了实现你在问题中概述的内容,你需要在你的数据库中有空闲日,并带有相关的id和名称,就像你在getDays函数中列出的那样,所以星期一你的数据库中将有一个id为1。

如果您只想要所有选项,请参阅文档的此部分:http://symfony.com/doc/current/reference/forms/types/entity.html#basic-使用

但是,如果要过滤结果,则需要一个查询生成器,如文档中所述:http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-实体的定制查询

如果这对你不起作用,或者我误解了这个问题,请告诉我。