dongliaoqiang8524 2014-05-29 15:24
浏览 52
已采纳

如何在Eloquent插入中指定书籍和作者关系?

I was figuring out how to insert a hasMany entry using Laravel 4

Author.php module

class Author extends  Eloquent //\LaravelBook\Ardent\Ardent 

{

public static $rules = array(
'title'             =>  'required',
'last_name'         =>  'required',
'first_name'        =>  'required',


);


/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'authors';

  public function abstraction()
{
    return $this->belongsTo('Book','book_id');
}

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getRegId()
{
    return $this->getKey();
}

}

Book.php Module

class Book extends  Eloquent //\LaravelBook\Ardent\Ardent 

{

public static $rules = array(
'title'             =>  'required',
'authors'           =>  'required',


);


/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'abstracts';

public function author()
{
    return $this->hasMany('Author');
}

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getRegId()
{
    return $this->getKey();
}

My controller

$r= new Book();

                $r->title       =   Input::get('title');
                $r->authors     =   Input::get('authors');
                $r->affiliation =   Input::get('affiliation');
                $r->keywords    =   Input::get('keywords');
                $r->summary     =   Input::get('summary');

                $r->save();
                $authors = new Author();
                $authors_array = array(
                    array('name' => 'Arun1' , 'affiliation' => 'aff_arun'),
                    array('name' => 'Arun2' , 'affiliation' => 'aff_arun'),
                    array('name' => 'Arun3' , 'affiliation' => 'aff_arun3'),
                    array('name' => 'Arun4' , 'affiliation' => 'aff_arun'),
                    );


                $authors->book()->associate($r);                    



                $authors->save($authors_array);

I'm getting one null record on authors table which pointed to the book and other 3 record that I inserted here without any pointing to the book.

  • 写回答

1条回答 默认 最新

  • donglu5041 2014-05-29 15:57
    关注

    First off I believe this is wrong schema, since probably one author could write more than a single book.

    So I suggest you define many-to-many (belongsToMany) relation for this instead.

    Anyway here is solution to your problem:

    // I wonder why you call the book $r ?
    $r = new Book;
    ...
    $r->save();
    
    $authors_array = array(...);
    
    foreach ($authors_array as $author)
    {
       $r->author()->save(new Author($author)); 
       // you need $fillable/$guarded on Author model or it will throw MassAssignementException
    }
    

    Another suggestion: rename the relation to authors so it is meaningful and you won't catch yourself trying to treat it like a single model (while it always returns the Collection)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog