Closed 84564221 closed 10 years ago
The embedded models use _id as default primary key as a way to identify the individual models. I don't really see why (or how) you would use embedded documents without _id.
I'm looking for a solution, to reduce blowed up laravel model classes. WIth the DoctrineMongoDBBundle
this was a common way for me, to embed objects {}
within MongoDB documents.
What do you recommend to achieve this?
You can try:
<?php
class User extends Moloquent
{
protected $collection = 'users';
...
public function item()
{
return $this->embedsOne('Item');
}
}
...
class Item extends Moloquent
{
protected $primaryKey = '__id';
...
}
and dont set __id
Is there a solution for this? I'm currently working on a Meteor App that's not using ObjectID's. I'm migrating some things to laravel and now I'm encountering this issue. Setting no primaryKey variable does not working. Setting it to null also does not work. I think the problem is the setAttribute function. The whole function seems a strange to be honest.
To create an Moloquent model for a legacy collection that has no primary key, simply set the protected
primaryKey
attribute tonull
.This method does not work for EmbedsOne relations or EmbedsMany relations.
Example classes (
User
embedsOneItem
):Result after performing
$user->item()->save($item)
: