DDC-2851: Allow set custom collection initializer at runtime #3559

Closed
opened 2026-01-22 14:22:35 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Dec 12, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user koc:

Use case: Company, Category, CompanyCategory entities. We are loading set of companies and should initialize Company.companyCategories collection with joined categories, which ordered by Company.title.

I cann't do it via Criteria API, it doesn't supports joins. + I should return same PersistentCollection instance (Company.companyCategories used in Symfony2 forms).

Current workaround:

    public function loadCompanyCategoriesCollectionForCompany(Company $company)
    {
        $companyCategories = $this->_em->createQueryBuilder()
            ->select('cc')
            ->from('OloloCompaniesBundle:CompanyCategory', 'cc')
            ->join('cc.category', 'c')
            ->addSelect('c')
            ->orderBy('c.title')
            ->where('cc.company = :company')
            ->setParameter('company', $company)
            ->getQuery()
            ->getResult()
        ;

        $coll = $company->getCompanyCategories();
        foreach ($companyCategories as $companyCategory) { /** @var $coll \Doctrine\ORM\PersistentCollection **/
            $coll->hydrateAdd($companyCategory);
        }
        $coll->setInitialized(true);
    }

What would be nice: native API for setting custom initializers.

Originally created by @doctrinebot on GitHub (Dec 12, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user koc: Use case: Company, Category, CompanyCategory entities. We are loading set of companies and should initialize Company.companyCategories collection with joined categories, which ordered by Company.title. I cann't do it via Criteria API, it doesn't supports joins. + I should return same PersistentCollection instance (Company.companyCategories used in Symfony2 forms). Current workaround: ``` public function loadCompanyCategoriesCollectionForCompany(Company $company) { $companyCategories = $this->_em->createQueryBuilder() ->select('cc') ->from('OloloCompaniesBundle:CompanyCategory', 'cc') ->join('cc.category', 'c') ->addSelect('c') ->orderBy('c.title') ->where('cc.company = :company') ->setParameter('company', $company) ->getQuery() ->getResult() ; $coll = $company->getCompanyCategories(); foreach ($companyCategories as $companyCategory) { /** @var $coll \Doctrine\ORM\PersistentCollection **/ $coll->hydrateAdd($companyCategory); } $coll->setInitialized(true); } ``` What would be nice: native API for setting custom initializers.
admin added the Improvement label 2026-01-22 14:22:35 +01:00
admin closed this issue 2026-01-22 14:22:36 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3559