How to make moloquent's embedsOne relations work?

324 Views Asked by At

I tried the example from the documentation. I got two classes:

namespace App;

use Moloquent\Eloquent\Model as Eloquent;

class Author extends Eloquent
{
    protected $fillable = [ 'name' ];
}
class Book extends Eloquent
{
    public function author()
    {
        return $this->embedsOne(Author::class);
    }
}

In a tinker session I create an author and a book, and try to save the link:

>>> $author = new App\Author(['name'=>'John Doe']);
=> App\Author {#2284
     name: "John Doe",
   }
>>> $book = new App\Book();
=> App\Book {#2277}
>>> $book->author()->save($author);
=> App\Author {#2284
     name: "John Doe",
     updated_at: MongoDB\BSON\UTCDateTime {#2296
       +"milliseconds": "1534969446023",
     },
     created_at: MongoDB\BSON\UTCDateTime {#2296},
     _id: MongoDB\BSON\ObjectId {#2298
       +"oid": "5b7dc6662dab0d03621a1c82",
     },
   }
>>> $book->save();
=> true

However, there is no author stored with the book.

>>> $book
=> App\Book {#2277
     updated_at: MongoDB\BSON\UTCDateTime {#2268
       +"milliseconds": "1534969455111",
     },
     created_at: MongoDB\BSON\UTCDateTime {#2268},
     _id: MongoDB\BSON\ObjectId {#2294
       +"oid": "5b7dc66f2dab0d03621a1c83",
     },
   }
>>> $book->author
=> null

Is it broken, or am I doing it the wrong way? When I use the create() method, i get a clean result:

>>> $book->author()->create(['name'=>'Jane Doe']);
=> App\Author {#2302
     name: "Jane Doe",
     updated_at: MongoDB\BSON\UTCDateTime {#2308
       +"milliseconds": "1534970275755",
     },
     created_at: MongoDB\BSON\UTCDateTime {#2308},
     _id: MongoDB\BSON\ObjectId {#2317
       +"oid": "5b7dc9a32dab0d03621a1c84",
     },
   }
>>> $book
=> App\Book {#2277
     updated_at: MongoDB\BSON\UTCDateTime {#2268
       +"milliseconds": "1534969455111",
     },
     created_at: MongoDB\BSON\UTCDateTime {#2268},
     _id: MongoDB\BSON\ObjectId {#2294
       +"oid": "5b7dc66f2dab0d03621a1c83",
     },
     author: App\Author {#2310
       name: "Jane Doe",
       updated_at: MongoDB\BSON\UTCDateTime {#2308
         +"milliseconds": "1534970275755",
       },
       created_at: MongoDB\BSON\UTCDateTime {#2308},
       _id: MongoDB\BSON\ObjectId {#2309
         +"oid": "5b7dc9a32dab0d03621a1c84",
       },
     },
   }

Trying to change the author with the save() method still doesn't work:

>>> $book->author()->save(App\Author::first())
=> App\Author {#2352
     _id: MongoDB\BSON\ObjectId {#2342
       +"oid": "5b7dcdfd2dab0d03621a1c85",
     },
     name: "Your Name",
     updated_at: MongoDB\BSON\UTCDateTime {#2346
       +"milliseconds": "1534971389628",
     },
     created_at: MongoDB\BSON\UTCDateTime {#2347
       +"milliseconds": "1534971389628",
     },
   }
>>> $book
=> App\Book {#2277
     updated_at: MongoDB\BSON\UTCDateTime {#2292
       +"milliseconds": "1534970479148",
     },
     created_at: MongoDB\BSON\UTCDateTime {#2268
       +"milliseconds": "1534969455111",
     },
     _id: MongoDB\BSON\ObjectId {#2294
       +"oid": "5b7dc66f2dab0d03621a1c83",
     },
     author: App\Author {#2310
       name: "Jane Doe",
       updated_at: MongoDB\BSON\UTCDateTime {#2308
         +"milliseconds": "1534970275755",
       },
       created_at: MongoDB\BSON\UTCDateTime {#2308},
       _id: MongoDB\BSON\ObjectId {#2309
         +"oid": "5b7dc9a32dab0d03621a1c84",
       },
     },
   }

Greetings!

Bernhard

1

There are 1 best solutions below

3
On

I think your error is that after the first line: $author = new App\Author(['name'=>'John Doe']), you don't save. Yes you create a new Author, but you don't save it, so when the $book->author()->save($author) is called, an object without an id on the Database is given, thats why it doesn't get saved,

Try running something like this:

$author = new App\Author(['name'=>'John Doe']);
$author->save();
$book = new App\Book();
$book->autor_id = $author->id;
$book->save();

Now the author should be saved properly!

Also, this works: $book->author()->create(['name'=>'Jane Doe']); because the create method is saving on the background, passing an existing object on the Database.