Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony ManyToMany新值必须是\Traversable的数组或实例,并给定“NULL”_Symfony_Doctrine_Many To Many_Symfony4 - Fatal编程技术网

Symfony ManyToMany新值必须是\Traversable的数组或实例,并给定“NULL”

Symfony ManyToMany新值必须是\Traversable的数组或实例,并给定“NULL”,symfony,doctrine,many-to-many,symfony4,Symfony,Doctrine,Many To Many,Symfony4,我在Symfony 4.2.6应用程序中有一个多个关系,我希望它可以为空 因此,我的第一位实体特约服务商如下: use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\SpecialOfferRepo

我在Symfony 4.2.6应用程序中有一个多个关系,我希望它可以为空

因此,我的第一位实体特约服务商如下:

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\SpecialOfferRepository")
 */
class SpecialOffer
{
    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Neighbourhood", inversedBy="specialOffers")
     */
     private $neighbourhood;

     public function __construct()
    {
        $this->neighbourhood = new ArrayCollection();
    }

    /**
     * @return Collection|Neighbourhood[]
     */
    public function getNeighbourhood(): Collection
    {
        return $this->neighbourhood;
    }

    public function addNeighbourhood(Neighbourhood $neighbourhood): self
    {
        if (!$this->neighbourhood->contains($neighbourhood)) {
            $this->neighbourhood[] = $neighbourhood;
        }

        return $this;
    }

    public function removeNeighbourhood(Neighbourhood $neighbourhood): self
    {
        if ($this->neighbourhood->contains($neighbourhood)) {
            $this->neighbourhood->removeElement($neighbourhood);
       }

       return $this;
   }
}
它与邻里阶层有关:

/**
 * @ORM\Entity(repositoryClass="App\Repository\NeighbourhoodRepository")
 */
class Neighbourhood implements ResourceInterface
{
    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\SpecialOffer", mappedBy="neighbourhood")
     */
    private $specialOffers;

    public function __construct()
    {
        $this->specialOffers = new ArrayCollection();
    }

        /**
     * @return Collection|SpecialOffer[]
     */
    public function getSpecialOffers(): Collection
    {
        return $this->specialOffers;
    }

    public function addSpecialOffer(SpecialOffer $specialOffer): self
    {
        if (!$this->specialOffers->contains($specialOffer)) {
            $this->specialOffers[] = $specialOffer;
            $specialOffer->addNeighbourhood($this);
        }

        return $this;
    }

    public function removeSpecialOffer(SpecialOffer $specialOffer): self
    {
         if ($this->specialOffers->contains($specialOffer)) {
            $this->specialOffers->removeElement($specialOffer);
            $specialOffer->removeNeighbourhood($this);
        }

        return $this;
    }
}
最后形式是

class SpecialOfferType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'neighbourhood',
                EntityType::class,
                [
                    'class' => Neighbourhood::class,
                    'label' => 'form.neighbourhood.label',
                    'translation_domain' => 'Default',
                    'required' => false,
                    'placeholder' => 'form.neighbourhood.all'
                ]
             );
        }
   }
但是,如果我在表格中没有选择特殊优惠的特定街区,我会得到以下错误: 无法确定类App\Entity\SpecialOffer中属性邻域的访问类型:类App\Entity\SpecialOffer中的属性邻域可以使用方法addNeighbourhood、removeNeighbourhood定义,但新值必须是数组或\Traversable、NULL的实例

我的特别优惠是否可以包含和数组的邻里关系,或者只是空的


我觉得我忽略了一些非常明显的事情,任何帮助都将不胜感激

因为实体上的字段都是多对多的,因此需要一个数组或类似的字段,而表单字段是EntityType,它将返回一个预期类型的实体或null,我觉得存在某种形式的不对称

我会考虑从开始使用,或者至少将表单上的选项设置为true,以便返回值是一个数组。


另一个选项是向表单字段添加DataTransformer,这会将null转换为空数组,将一个实体转换为一个实体的数组,反之亦然。

因为实体上的字段都是多对多的,因此需要数组或类似的值,表单字段是EntityType,它将返回一个预期类型的实体或null,我觉得存在某种形式的不对称

我会考虑从开始使用,或者至少将表单上的选项设置为true,以便返回值是一个数组。 另一个选项是在表单字段中添加一个DataTransformer,它将null转换为空数组,将一个实体转换为一个实体的数组,反之亦然。

Test=>

$builder
            ->add(
                'neighbourhood',
                EntityType::class,
                [
                    'class' => Neighbourhood::class,
                    'label' => 'form.neighbourhood.label',
                    'translation_domain' => 'Default',
                    'required' => false,
                    'multiple' => true,
                    'placeholder' => 'form.neighbourhood.all'
                ]
             );
测试=>

$builder
            ->add(
                'neighbourhood',
                EntityType::class,
                [
                    'class' => Neighbourhood::class,
                    'label' => 'form.neighbourhood.label',
                    'translation_domain' => 'Default',
                    'required' => false,
                    'multiple' => true,
                    'placeholder' => 'form.neighbourhood.all'
                ]
             );