Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.0.0BETA1
    • Component/s: Hydration
    • Labels:
      None

      Description

      Currently supported ways of schema migration are sufficient in many cases. But I think it's worth considering to have more explicit way to migrate schema for better readability / code maintenance.

      It might be useful to be able to transform original MongoDb data prior to hydration. Maybe as a new lifecycle callback (e.g. preLoad). This way one could have special property for schema version in the document and normalize data according to version differencies. e.g.:

      function preLoad(array &$data)
      {
          if(!isset($data['version'])) {
              $data['version'] = 1;
          }
          switch($data['version']) {
              case 1:
                   $data['fullName'] = $data['name'];
                   unset($data['name']);
                   // breaks are ommitted intentionally here and below
              case 2:
                   $e = explode(' ', $data['fullName'])
                   $data['firstname'] = $e[0];
                   $data['lastname'] = $e[1];
                   unset($data['fullName']);
              case 3:
                   $data['address'] = array(
                       'street' => $data['street'],
                       'city' => $data['city']
                   );
                   unset($data['street']);
                   unset($data['city']);
               case 4:
               default:
                    // current version
          }
      }
      

      Just as example. It also simplifies writing tests for schema migrations as I think. Is it possible with current ODM internals? What do you think?
      I could try to implement this feature if it is acceptable proposal.

        Activity

        Hide
        Jonathan H. Wage added a comment -

        I think this can be implemented already without having to change Doctrine. You just need to manage a version field and use the preLoad() like you are above. It is an acceptable idea, but I think it is already possible. What do you think?

        Show
        Jonathan H. Wage added a comment - I think this can be implemented already without having to change Doctrine. You just need to manage a version field and use the preLoad() like you are above. It is an acceptable idea, but I think it is already possible. What do you think?
        Hide
        Vladimir Razuvaev added a comment -

        It's great if it is already possible. But I think there is no "preLoad", "preHydrate" or similar event right now. At least I didn't find one in code or docs.

        Show
        Vladimir Razuvaev added a comment - It's great if it is already possible. But I think there is no "preLoad", "preHydrate" or similar event right now. At least I didn't find one in code or docs.
        Hide
        Jonathan H. Wage added a comment -

        Ok, I think maybe we can add a preLoad() event.

        Show
        Jonathan H. Wage added a comment - Ok, I think maybe we can add a preLoad() event.
        Show
        Jonathan H. Wage added a comment - Fixed by http://github.com/doctrine/mongodb-odm/commit/78fefc3b70aa8ec0facd3cc4ffa27d6c88670407

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Vladimir Razuvaev
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: