Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2851

Allow set custom collection initializer at runtime

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.4.1
    • Fix Version/s: None
    • Component/s: ORM
    • Labels:
      None

      Description

      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.

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Konstantin
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: