Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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
Php 按组序列化无法用于嵌入_Php_Symfony_Serialization - Fatal编程技术网

Php 按组序列化无法用于嵌入

Php 按组序列化无法用于嵌入,php,symfony,serialization,Php,Symfony,Serialization,我尝试使用组在调用时仅序列化MyEntity的一部分: $myEntities = $repository->findAll(); return $this->json([ 'myEntities' => $myEntities ], 200, [], ['groups' => 'main']); 在我的一个控制器的路由中(以避免循环引用错误)。但我很难让它与嵌入式产品一起工作 要简化了解情况,请参阅: /** * @ORM\

我尝试使用组在调用时仅序列化
MyEntity
的一部分:

$myEntities = $repository->findAll();

return $this->json([
            'myEntities' => $myEntities
        ], 200, [], ['groups' => 'main']);
在我的一个控制器的路由中(以避免循环引用错误)。但我很难让它与嵌入式产品一起工作

要简化了解情况,请参阅:

/**
 * @ORM\Entity
 */
class MyEntity
{

    /**
     * @ORM\Embedded(class="App\Entity\Util\MyTestEmbeddable")
     * @Groups("main")
     */
    protected MyTestEmbeddable $test;
}

但是结果只包含
MyEntity::$test
属性的空数组

我本来希望MyTestEmbeddable::$text
MyTestEmbeddable::$text2`也被序列化


我需要做什么才能使它如我所期望的那样工作?

您不仅需要为MyEntity::$test声明序列化组,而且还需要为希望成为序列化组一部分的MyTestEmbeddable的各个属性声明序列化组

例如:


通过这种方式,您可以选择要序列化的嵌入实体的各个属性。

这确实有效!该死……这是我本可以想到的——尽管我必须说,我无法将这种触发行为从使用实体传递下来,这让我很烦恼。我的意思是,在任何可能使用这种可嵌入方法的类中,都有必要坚持这种命名约定。尽管如此,你还是解决了眼前的问题,因此,如果你把它作为一个答案发布,我将欣然接受。谢谢
/**
 * @ORM\Embeddable
 */
class MyTestEmbeddable
{
    /**
     * @ORM\Column(type="string", nullable=false)
     */
    private string $text;

    /**
     * @ORM\Column(type="string", nullable=false)
     */
    private string $text2;

}
/**
 * @ORM\Column(type="string", nullable=false)
 * @Groups("main")
 */
private ?string $text;