PropertyAccessor需要对象或数组的图形进行操作,但它发现类型为“NULL”

时间:2017-08-01 15:20:00

标签: php symfony doctrine sonata

我在AssignmentTypeAdmin中有这个方法:

protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('code', 'text')
            ->add('label', 'text', array('label'=>'Libellé'))
            ->add('assignHierarchyNode.label', 'text', array('label' => 'Noeud hiérarchique'))
            ->add('assignPortfolioType.portfolioTypeLabel', null, array('label' => 'Type de portefeuille'))
            ->add('assignGeoHierarchyNodeType.label', null, array('label' => 'Type de noeud hiérarchique'));
    }

在AssignmentType类中:

/**
 * @ORM\ManyToOne(targetEntity="HierarchyManagerBundle\Entity\HierarchyNode")
 * @ORM\JoinColumn(name="assign_hierarchy_node_id", referencedColumnName="id", nullable=FALSE)
 */
private $assignHierarchyNode;

在HierarchyNode类中: ...

 /**
     * @var string
     * @Gedmo\TreePathSource
     * @ORM\Column(name="label", type="string", length=255)
     */
    private $label;

... 也得到并设置方法。

我的问题是我收到此错误,我不知道为什么:

PropertyAccessor需要操作对象或数组的图形,但在尝试遍历属性“label”的路径“assignHierarchyNode.label”时发现类型为“NULL”。

我使用Symfony 3.1,Doctrine 2.5.2,Sonata Bundle。 提前谢谢!

2 个答案:

答案 0 :(得分:1)

对我来说,当它在构造器中存储 Objects 时,将其初始化为 array ArrayCollection 时,它就可以工作。

public function __construct() {
   $this->conditions = [];
}

它引发异常,因为如果未初始化,它将无法对其进行迭代。

答案 1 :(得分:0)

看看那里:Sonata PostPersist

在这里你可以找到一个Sonata构造函数,它可以帮助你。它可能比使用_constructor更好。你可以找到一个例子。

rm

希望它有用。如果您有任何疑问,请询问。