Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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_Sql_Symfony_Doctrine - Fatal编程技术网

Php 避免返回完整相关实体的原则

Php 避免返回完整相关实体的原则,php,sql,symfony,doctrine,Php,Sql,Symfony,Doctrine,我是Symfony的新手,我正在尝试从我的内容表中获取所有记录。它可以工作,但也会返回相关实体中的所有字段 $content = $this->contentRepository->findAll(); 下面是我得到的: [{ "id": 2, "field1": "xx", "field2": "xx", "field3": 22

我是Symfony的新手,我正在尝试从我的内容表中获取所有记录。它可以工作,但也会返回相关实体中的所有字段

$content = $this->contentRepository->findAll();
下面是我得到的:

[{
    "id": 2,
    "field1": "xx",
    "field2": "xx",
    "field3": 22,
    "field4": {"id":1, "field1":"xx", ...},
    ....
},...]
在field4上,我只想获取id作为值,而不是整个对象。就像我在做SQL一样。
从其他地方阅读我发现了惰性加载,但它似乎不起作用。

只需转到您的
内容存储库
文件并定义如下函数

public function getAllContentIds()
{
    $qb = $this->createQueryBuilder();
    return $qb->select('id')
        ->from('YourBundleName:YourTableName')
        ->getQuery()
        ->getResult();
}
$contentIds = $this->contentRepository->getAllContentIds();
然后像这样使用这个函数

public function getAllContentIds()
{
    $qb = $this->createQueryBuilder();
    return $qb->select('id')
        ->from('YourBundleName:YourTableName')
        ->getQuery()
        ->getResult();
}
$contentIds = $this->contentRepository->getAllContentIds();

但是如果你想得到一个字段数组,最好是得到一个实体数组,然后用or将它们序列化。我的意思是,条令返回整个关联实体作为字段4的值,我只需要该实体的id。其他字段可以考虑更新你的答案以匹配问题。这可能很有挑战性,因为条令不支持连接部分实体。您可以使用数据传输对象仅获取某些字段