mongodb-odm
mongodb-odm copied to clipboard
Embedded document as _id
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.
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']);
}
}
Will this feature be implemented in version 2.0?
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.
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.
Hi,
We have this kind of documents, would they be supported by this feature?
{
"_id" : {
"country" : "AT",
"handle" : "classic-box"
},
"other_fields": "other_values"
}
Yes 😊
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 I'm not using Doctrine when documents have compound identifiers. There are often projections, so that's alright for my usage.