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

时间:2020-01-09 15:08:43

标签: symfony doctrine

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

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

1 个答案:

答案 0 :(得分: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');

$queryResult = $qb->getQuery()->getResult();

$items = [];
foreach($queryResult as $item) {
    $id = $item['id'];

    if (!isset($items[$id])) {
        $items[$id]['id'] = $id;
    }

    $items[$id]['child'][] = [
        'slug' => $item['name']
    ];
}


var_dump($items);