У меня много сущностей с этими отношениями и из-за оптимизации вызовов REST, чтобы выбрать только те поля, которые мне нужны на самом деле, также с отношениями сущностей. Итак, у меня есть сущность, подобная этой:
/**
* @ORM\Entity(repositoryClass="App\Repository\DocumentRepository")
* @ORM\Table(name="document_types")
*/
class DocumentType
{
use RestDefaultsTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
* @Serializer\Groups({"main-document-type"})
*/
private $id;
/**
* @ORM\Column(type="string", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $slug;
/**
* @ORM\Column(type="string", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $symbol;
/**
* @ORM\Column(type="string")
* @Serializer\Groups({"main-document-type"})
*/
private $name;
/**
* @ORM\Column(type="string", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $description;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $canBeGenerated;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $inEmployer;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $inWorker;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $inAll;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $isAnnex;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*
*
*/
private $isAttachment;
/**
* @ORM\ManyToOne(targetEntity="DocumentType", inversedBy="childDocuments")
* @Serializer\Groups({"main-document-type"})
*/
private $parentDocument;
/**
* @ORM\OneToMany(targetEntity="DocumentType", mappedBy="parentDocument")
* @Serializer\Groups({"main-document-type"})
*
*/
private $childDocuments;
/**
* @ORM\Column(type="string", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $annexNumberStart;
/**
* @ORM\ManyToOne(targetEntity="ProfessionGroup", inversedBy="documentTypes")
* @Serializer\Groups({"main-document-type"})
*/
private $professionGroup;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $isNumbering;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $employmentGroup = [];
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $canNumberBeSuggested = false;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Serializer\Groups({"main-document-type"})
*/
private $canBeEdited = false;
}
И в хранилище сущностей я хочу сделать что-то вроде этого:
public function getDocuments( $filterData )
{
$select = [
// 'id' => 'id',
'slug' => 'slug',
'name' => 'name',
'isAnnex' => 'is_annex',
'canNumberBeSuggested' => 'can_number_be_suggested',
'canBeEdited' => 'can_be_edited',
];
$childDocumentsSelect = [
'id' => 'id',
'name' => 'name',
];
$qb = $this->createQueryBuilder( 'document' )
->where( 'document.canBeGenerated = 1' )
;
$qb->select( 'document.id AS id' );
foreach ( $select as $selectField => $selectValue )
{
$qb->addSelect( sprintf( 'document.%s AS %s', $selectField, $selectValue ) );
}
$qb->join( 'document.childDocuments', 'childDocuments' );
foreach ( $childDocumentsSelect as $selectField => $selectValue )
{
$qb->addSelect( sprintf( 'childDocuments.%s AS %s', $selectField, $selectValue ) );
}
return $qb->getQuery()->getResult();
}
, в результате я хочу получить базовые поля из $select
массив и массив дочерних документов с выбранными полями в $childDocumentsSelect
только в этом примере id
и name
Возможно ли вообще? Иногда у моих сущностей есть 3 ассоциации, иногда нет. В данный момент я использую сериализатор, но хочу оптимизировать этот