Laravel 检查模型关系
我试图检查我的Laravel 检查模型关系,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我试图检查我的雄辩的模型,以找出它们与其他模型的关系。问题在于,关系仅被定义为单一方法,不存在关系的中心索引: public function posts() { return $this->hasMany('Post'); } 为了检查所有关系,我需要提取方法列表,取出继承自Eloquent的方法,执行每个方法并检查返回类型: $all = get_class_methods($model); $inherited = get_class_methods('Eloquent')
雄辩的模型,以找出它们与其他模型的关系。问题在于,关系仅被定义为单一方法,不存在关系的中心索引:
public function posts()
{
return $this->hasMany('Post');
}
为了检查所有关系,我需要提取方法列表,取出继承自Eloquent
的方法,执行每个方法并检查返回类型:
$all = get_class_methods($model);
$inherited = get_class_methods('Eloquent');
$unique = array_diff($all, $inherited);
foreach($unique AS $method)
{
$relation = $model->$method();
if(is_a($relation, 'Illuminate\Database\Eloquent\Relations\Relation'))
{
//... this is a relation, do something with it
}
}
不用说,这是非常危险的。有没有其他更安全的方法来执行此类检查?您可以将PHPDoc注释添加到关系方法中,然后使用PHP从源代码中提取这些注释