mongodb-odm icon indicating copy to clipboard operation
mongodb-odm copied to clipboard

Embedded document as _id

Open TheHett opened this issue 7 years ago • 8 comments

Code below works fine:

/**
 * Class UserStats
 * @ODM\Document(collection="user_stats")
 */
class UserStats
{
    /**
     * @ODM\Id(strategy="NONE")
     */
    private $id;

    /** @ODM\Field(type="int") */
    private $downloaded;

    /**
     * @ODM\EmbedOne(targetDocument="core\mongo\keys\UserStatsKey")
     */
    private $test;
    
    //getters and setters
}

result:

> db.user_stats.find().pretty()
{
	"_id" : {
		
	},
	"downloaded" : 10,
	"test" : {
		"id" : 1,
		"date" : ISODate("2017-10-06T09:04:21.557Z")
	}
}

I tried to use Embedded document as primary key:

/**
 * Class UserStats
 * @ODM\Document(collection="user_stats")
 */
class UserStats
{
    /**
     * @ODM\Id(strategy="NONE")
     * @ODM\EmbedOne(targetDocument="core\mongo\keys\UserStatsKey")
     */
    private $id;

    /** @ODM\Field(type="int") */
    private $downloaded;

    //getters and setters
}

result:

octrine\ODM\MongoDB\Mapping\MappingException: No identifier/primary key specified for Document 'core\mongo\entities\UserStats'.

It is not possible? Id and EmbeddedOne annotations replaced each other.

TheHett avatar Oct 06 '17 09:10 TheHett

Both Id and EmbedOne are considered as field definitions by ODM and they replacing each other is a desired effect at the moment. I think it would be useful to have a possibility to have a mapped object as an identifier thus I'm marking this as an idea for a feature.

As a workaround you can use array to store data in database and return a value object from the getter:

class SomeClass
{
    /** @ODM\Id(strategy="NONE") */
    private $id;

    public function getId()
    {
        return new IdObject($this->id['val'], $this->id['another']);
    }
}

malarzm avatar Oct 06 '17 12:10 malarzm

Will this feature be implemented in version 2.0?

petr-buchin avatar May 17 '18 18:05 petr-buchin

It's not on my timeline for 2.0 since we're trying to wrap up the driver change without getting into too many other topics. I'll schedule it for a 2.x milestone, which means it will arrive sometime after 2.0. If you want to take a crack at implementing this, we appreciate the contribution.

alcaeus avatar May 18 '18 04:05 alcaeus

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 21 '19 06:01 stale[bot]

Hi,

We have this kind of documents, would they be supported by this feature?

{
    "_id" : {
        "country" : "AT",
        "handle" : "classic-box"
    },
    "other_fields": "other_values"
}

olvlvl avatar Nov 14 '19 23:11 olvlvl

Yes 😊

alcaeus avatar Nov 15 '19 03:11 alcaeus

Hi,

We have this kind of documents, would they be supported by this feature?

{
    "_id" : {
        "country" : "AT",
        "handle" : "classic-box"
    },
    "other_fields": "other_values"
}

this is just what I'm trying to approach, how did you do this? when I flush, this is always storing an empty object :S

ivanaareon avatar Oct 31 '21 21:10 ivanaareon

@ivanaareon I'm not using Doctrine when documents have compound identifiers. There are often projections, so that's alright for my usage.

olvlvl avatar Nov 04 '21 17:11 olvlvl