Laravel保存模型和关系
我有以下型号:Laravel保存模型和关系,laravel,laravel-4,Laravel,Laravel 4,我有以下型号: class Movie extends Eloquent { protected $primaryKey = "movie_id"; public function detail() { return $this->hasOne('Detail'); } public function firstpage() { return $this->hasOne('Firstpage'); } public function country() {
class Movie extends Eloquent {
protected $primaryKey = "movie_id";
public function detail()
{
return $this->hasOne('Detail');
}
public function firstpage()
{
return $this->hasOne('Firstpage');
}
public function country()
{
return $this->belongsToMany('Countries','movies_countries','movie_id','country_id');
}
public function year()
{
return $this->hasOne('Years');
}
public function media() {
return $this->hasMany('Media');
}
}
这是一个有趣的模型:
class Years extends Eloquent {
protected $table = 'movies_years';
protected $primaryKey = "relation_id";
public function movie()
{
return $this->belongsTo('Movie');
}
DBTable for years有一个字段“movie\u year”和“movie\u id”
因此,我有以下问题,或理解问题:
我试图用新数据更新车型年数,但似乎无法完成。我尝试了以下方法:
$movies = Movie::find($tmp['movie_id']);
$movies->title = $tmp['title'];
$movies->title_product = $tmp['title_product'];
$movies->title_orginal = $tmp['title_original'];
$movies->year = array('movie_year' => $tmp['movieyears']);
$movies->push();
眼睛一直盯着$movies->year,其他一切都很好
我也尝试过一些愚蠢的事情,比如:
$movies->year() = array('movie_year' => $tmp['movieyears']);
我不知道如何更新Years模型,它与电影模型有关。官方方法是使用附加、同步或关联方法。有关这些方法的说明,请参阅 在您的情况下,您必须执行以下操作:
$movies->year()->attach($tmp['movieyears']);
要删除关系(而不是电影或年份),请使用detach()。Ok,自己找到它,尝试并出错:$movies->Year()->update(数组('Movie_Year'=>$tmp['movieyears');但是为什么不记录更新命令呢?不幸的是,有时候你不得不深入研究API。