Php 在拉雷维尔和蒙哥达不起作用的关系?
你好,我正在使用LAravel和MOngodb应用程序: 我有以下情况: 我有这样的评论和他们的点击集: Hoots模型集合Php 在拉雷维尔和蒙哥达不起作用的关系?,php,mongodb,laravel-4,Php,Mongodb,Laravel 4,你好,我正在使用LAravel和MOngodb应用程序: 我有以下情况: 我有这样的评论和他们的点击集: Hoots模型集合 "_id": ObjectId("52ef89e9c6b93ca123f2fd37"), "user_id": "52df9ab5c6b93c8e2a8b4567", "hoot": "Hello Thius is Tewst", "updated_at": ISODate("2014-02-03T12:22:01.530Z"), "created_at": I
"_id": ObjectId("52ef89e9c6b93ca123f2fd37"),
"user_id": "52df9ab5c6b93c8e2a8b4567",
"hoot": "Hello Thius is Tewst",
"updated_at": ISODate("2014-02-03T12:22:01.530Z"),
"created_at": ISODate("2014-02-03T12:22:01.530Z")
其热门车型系列如下:
"_id": ObjectId("52ef8d5ec6b93c6d24f2fd37"),
"created_at": ISODate("2014-02-03T12:36:46.130Z"),
"dip": NumberInt(1),
"hits": NumberInt(0),
"post_id": "52ef89e9c6b93ca123f2fd37",
"updated_at": ISODate("2014-02-03T13:35:20.766Z")
所以这里有一个评论,即hoot有一个描述他们喜欢和不喜欢的集合
我有一个模特儿
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class Hoots extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'hoots';
public $timestamps = true;
protected $fillable = array('user_id', 'hoot');
public function gethit()
{
return $this->hasOne('Hitdip');
}
}
请点评?
错误
在一对一的关系中,事情很简单。上述关系定义正确。但我们需要以这种格式获取内容
Hoots::with('gethit')->find(id)->get();
Hoots::with('gethit')->where('user_id',$user_id)->get();
有时类名不作为对象处理,因此必须直接在函数中传递 您需要更改gethit和gethoot函数,并且需要传递类对象 例如:
public function gethit(){
return $this->hasOne(new gethit(),'Hoots','post_id','_id');
}
与gethoot相同
public function gethoot(){
return $this->BelongsTo(new Hoots());
}
在一对多的关系中,像Hoots有很多帖子。所以你可以打电话
$posts = Hoots::with('gethit')->where('user_id','52df9ab5c6b93c8e2a8b4567')->get();
错误是。。?还是我们自己猜?@u_mulder抱歉告诉你了一半。再次回顾问题我更新了错误
gethit
是属性,gethit()
是函数。我也有同样的问题,但是一对多。。。似乎无法理解。。。请help@Raiden问题出在哪里?我使用一对多关系,当我做一些类似于$subcategories=$categories->subcategories()->get()
的事情时,类别有很多子类别。它给了我未定义的方法subcategories
错误,我在categories模型中有subcategories方法,它有一个hasMany(“SubcategoriesModel”)
关系。子类别模型有一个属于(“CategoriesModel”)
also@Raiden很抱歉延迟回复,但您可以这样做。在此处指定id。hasMany(“子类别模型”,“类别id”,“id”);然后像这样打电话。类别::with('subcategories')->where('condition')->get();这里是我们如何使用mongodb的$lookup聚合。是否有任何包请告诉我。
public function gethit(){
return $this->hasOne(new gethit(),'Hoots','post_id','_id');
}
public function gethoot(){
return $this->BelongsTo(new Hoots());
}
$posts = Hoots::with('gethit')->where('user_id','52df9ab5c6b93c8e2a8b4567')->get();