[MODM-64] @DynamicFields Created: 28/Aug/10  Updated: 30/Aug/10  Resolved: 30/Aug/10

Status: Resolved
Project: Doctrine MongoDB ODM
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major
Reporter: Benjamin Eberlei Assignee: Jonathan H. Wage
Resolution: Invalid Votes: 0
Labels: None


 Description   

I suggest adding a new feature called @DynamicFields. The idea is from roman and documeneted here: http://github.com/romanb/couchdb-persistence-api/blob/master/src/main/java/org/code_factory/couch/annotations/DynamicFields.java

Idea: Save all "left-over" fields of the result in an array.

This would greatly enhance the flexibility of document mappers



 Comments   
Comment by Jonathan H. Wage [ 28/Aug/10 ]

Nice idea! So something like this could essentially work:

/** @Document */
class User
{
    /** @DynamicFields */
    public $data;
}

$user = $dm->find(....);
echo $user->data['username'];

I am not sure if it is a good idea to just allow them to use a single @DynamicFields, maybe we should throw an exception if they use it that way? Also, if they modify a value inside $data should we persist the changes?

Comment by Benjamin Eberlei [ 28/Aug/10 ]

i guess the semantics should be, just one dynamic fields per entity allowed and that is inlined into the "normal" fields using array_merge() ?

Comment by Benjamin Eberlei [ 29/Aug/10 ]

ok maybe array_merge() is a bit dangerous, when the dynamic field has a property named like a real property.

But you could do $data = array_merge($dynamicData, $staticData); and should be fine then semantically, of course this should be documented

I think you should have at least an ID or not? but something like this would even be very fast for prototyping:

/** @Document */
class User
{
    /** @Id @Field */
    private $id;

    /** @DynamicFields */
    public $data;
}
Comment by Jonathan H. Wage [ 29/Aug/10 ]

After thinking about it and discussing it I think the @Collection type can do what is needed:

/** @Document */
class User
{
    /** @Id */
    private $id;

    /** @Collection */
    public $data = array();
}
Generated at Fri Nov 21 08:32:59 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.