php - PropertyAccessor 需要对象或数组的图形来操作,但它发现类型 "NULL"

标签 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;

...
还有 get 和 set 方法。

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

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

为此,我使用 Symfony 3.1、Doctrine 2.5.2、Sonata Bundle。
先感谢您!

最佳答案

看看那里:Sonata PostPersist

在这里你可以找到一个奏鸣曲构造函数,它可能对你有帮助。它可能比使用 _constructor 更好。你可以在下面找到一个例子。

public function postPersist($client)
{
    $em = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager');

    if ($client instanceof Client )
    {
        $test = new Test();
        $test->setClient($client);
        $test->setSurname($client->getSurname());
        $test->setFirstname($client->getFirstname());
        $em->persist($test);
        $em->flush();
    }
}

希望它有用。如果您有任何问题,请提问。

关于php - PropertyAccessor 需要对象或数组的图形来操作,但它发现类型 "NULL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441709/

相关文章:

php - Symfony 使用的是什么版本的 PHP?

php - Doctrine 和大量数据

php - 将 Pear DB 站点迁移到 mdb2,返回语法错误

javascript - 尝试以表单形式发送电子邮件时出现 405 错误

php - 连接 + 三元

php - 将 PostgreSQL DEFAULT 与 Doctrine 一起使用?

mysql - Symfony 4 中未使用原始查询的从站

php - 使用 PHP 后端在通知中心注册

symfony - Docker-compose自动安装wkhtmltopdf

Symfony:一个大师有多个类似的 bundle ?