从实体及其关系中选择特定字段

问题描述 投票:0回答:1

我有许多与之相关的实体,由于优化了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中选定字段的子文档数组中接收基本字段,在此示例中,仅idname

甚至可能吗?有时我的实体有3个关联,有时则没有。目前,我正在使用序列化程序,但想对此进行优化

symfony doctrine
1个回答
0
投票

尝试我的示例,我认为这就是您所需要的。

$qb = $this->createQueryBuilder( 'document' );
$qb->select([
    'document.slug',
    'document.name',
    'document.isAnnex',
    'document.canNumberBeSuggested',
    'document.canBeEdited',
    'childDocument.id',
    'childDocument.name'
])
->leftJoin('documnet.childDocuments', 'childDocument')
->where('document.canBeGenerated = 1');
© www.soinside.com 2019 - 2024. All rights reserved.