Doctrine MongoDB ODM
  1. Doctrine MongoDB ODM
  2. MODM-173

Change the name of the collection dynamic

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Collections
    • Labels:
      None

      Description

      I have to create collection for users per each day they register so if I have 5 users and 2 register on 2013-03-02 and 3 have register on 2013-03-03 I will have 2 collections

      The user is declare in a YML file with no collection specified

      In the project I do an import via CLI something like this

      foreach($arUsers as $key => $arUser)
      {
      
          $collection_name = 'day_' . $arUser['date'];
      
      
          $user = new User();
      
          /* Change the collection name to be dynamic  */
          $OdmMetaData = $documentManager->getClassMetaData(get_class($user));
      
          $OdmMetaData->setCollection($collection_name);
      
          $user->setUserId($arUser['user_id']);
          $user->setEmail($arUser['email']);
      
          // ....
      
          $this->getDocumentManager()->persist($user);
          $this->getDocumentManager()->flush();
          
          // should save the user to `'day_' . $arUser['date']`
      }

      Problem is that when I look in the mongoDB I see only one collection with name "day_2013-03-03" which is the last date , if I set the

      $documentManager->flush();

      after the foreach end statement which I should it save all in the first collection "03-02".

        Activity

        Hide
        Marco Pivetta added a comment -

        I personally don't think that's an use case to be handled in the ODM.

        The problem is that class metadata properties are not constantly accessed, but instead cached locally in various components for performance reasons.

        A fix for this would be to explicitly deny modification of metadata after it was returned by the metadata factory (after "onLoadMetadata" events).

        Show
        Marco Pivetta added a comment - I personally don't think that's an use case to be handled in the ODM. The problem is that class metadata properties are not constantly accessed, but instead cached locally in various components for performance reasons. A fix for this would be to explicitly deny modification of metadata after it was returned by the metadata factory (after "onLoadMetadata" events).
        Hide
        Daniel Gafitescu added a comment -

        Reinitialize the DoctrineManager at every group of document did the trick but it's just not beautiful code.
        In a perfect world it would be nice to have a method setCollectionName on a object. But then again Doctrine is a open source project and all that work on the project are doing this on their own time and I thank you for that.

        Show
        Daniel Gafitescu added a comment - Reinitialize the DoctrineManager at every group of document did the trick but it's just not beautiful code. In a perfect world it would be nice to have a method setCollectionName on a object. But then again Doctrine is a open source project and all that work on the project are doing this on their own time and I thank you for that.

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Daniel Gafitescu
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: