如何使好礼的embedOne关系正常工作?

时间:2018-08-24 23:24:15

标签: php mongodb laravel moloquent

我尝试了文档中的示例。我上了两节课:

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);
    }
}

在修补会议中,我创建了一位作者和一本书,并尝试保存链接:

>>> $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

但是,这本书没有存储任何作者。

>>> $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

是坏了,还是我做错了方向?当我使用create()方法时,我得到一个干净的结果:

>>> $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",
       },
     },
   }

尝试使用save()方法更改作者仍然无效:

>>> $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",
       },
     },
   }

问候!

伯恩哈德

1 个答案:

答案 0 :(得分:0)

我认为您的错误是在第一行$author = new App\Author(['name'=>'John Doe'])之后,您没有保存。是的,您创建了一个新的Author,但是您没有保存它,因此,在调用$book->author()->save($author)时,将给出一个在数据库上没有id的对象,这就是为什么它不被保存的原因,

尝试运行类似这样的内容:

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

现在应该正确保存作者!

此外,这可行:$book->author()->create(['name'=>'Jane Doe']);,因为create方法保存在后台,并在数据库中传递了现有对象。