在雄辩中使用多态关系扩展模型 [英] Using polymorphic relationships in Eloquent to extend model

查看:128
本文介绍了在雄辩中使用多态关系扩展模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢Slim,但仍然在尝试学习它,并决定重做一个旧应用程序。我正在努力使用雄辩,但很快就失去了,我不认为是非常复杂的。
我的应用程序太复杂了,所以我在这里回溯到这个教程,因为这或多或少是我想要做的,使用扩展另一个类的模型,看看我是否可以得到这个工作: http://www.richardbagshaw.co。 uk / laravel-user-types-and-polymorphic-relationships / 这只是用户类型的扩展名。



我不能。这是Laravel的一个教程,所以我知道会有所不同。我已经重新创建了数据库(减去一些无关紧要的东西,如用户名和密码),并充分填充。我已经复制了用户,自由职业者和员工类的代码,只修改了用户类,删除了这个(我想)中似乎不需要的额外的方法。

 命名空间eloquent \eloquent; 

class User extends Eloquent implements {

protected $ table ='users';

public function userable()
{
return $ this-> morphTo();
}
}

如果我这样做:

  $ user = $ app-> user-> find(1) - > firstName; 
echo $ user

它按预期工作。
这不是:

  $ user = $ app-> user-> find(1); 
echo $ user-> userable-> dayrate;

它给了我这样的:
致命错误:D类中没有找到类Employee \Apache24\htdocs\eloquent\vendor\illuminate\database\Eloquent\Model.php在第900行



然而,它是否正确识别是否它正在寻找一个员工或自由职业者,我认为是从DB列userable_type。



问题是真的我应该如何访问子类的字段?我完全错了,还是有更好的方法?

解决方案

我最终回到了这个问题,解决这个问题,以防其他人遇到类似的问题。
问题是,它正在寻找数据库中的确切字符串作为用户类型,在这种情况下为Employee。真正需要的是查找包含命名空间的整个字符串。通过将整个命名空间添加到数据库条目中,以userable_type(即eloquent \eloquent\Employee),然后选择它,并可以找到模型...
我在Eloquent中查看了选项,如MorphClass,但是我似乎没有做我所需要的。



我还没有将这个实际上实现到我正在开展的项目中,但是我想我可能会离开整个字符串在那里,因为我很懒惰,它不是关键任务,时间敏感的应用程序,或尝试用中间件或??做某事。无论如何,希望能帮助别人。


I am quite new to Slim, still trying to learn it and decided to redo an old app I'd made. I am trying to use Eloquent but have quickly gotten lost doing what I wouldn't think is very complicated. The app I had was even too complicated to learn on, so I backtracked to this tutorial here, as this is more or less what I am trying to do, use models extending one other class, to see if I can even get this working: http://www.richardbagshaw.co.uk/laravel-user-types-and-polymorphic-relationships/ It's just a user type extension.

I cannot. This is a tutorial for Laravel obviously, so I know it will be a bit different. I have recreated the database (minus some of the extraneous stuff like username and password) and populated it sufficiently. I have copied the code for the User, Freelancer and Employee classes, modifying only the User class removing the extra methods which don't seem to be required for this (I think) as below.

namespace eloquent\eloquent;

class User extends Eloquent implements {

  protected $table = 'users';

  public function userable()
  {
        return $this->morphTo();
  }
}

If I do this:

$user = $app->user->find(1)->firstName;
echo $user

It works as expected. This does not:

$user = $app->user->find(1);
echo $user->userable->dayrate;

It gives me this: Fatal error: Class 'Employee' not found in D:\Apache24\htdocs\eloquent\vendor\illuminate\database\Eloquent\Model.php on line 900

It does however correctly identify whether it's looking for an employee or a freelancer, which I assume is coming from the DB column userable_type.

Question is really how should I be accessing the fields of the subclass? Am I doing it totally wrong, or is there a better way?

解决方案

I eventually came back to this question, and was able to solve it, just in case anyone else is having a similar problem. The problem was that it was looking for the exact string in the Database as the usertype, in this case Employee. What it really needed, was to look for the whole string including the namespace. By adding the entire namespace into the database entry as userable_type (ie eloquent\eloquent\Employee) it then picks it up and can find the model... I looked into options in Eloquent, like MorphClass but that does not appear to do what I need.

I haven't actually implemented this into a project I'm working on yet, but I imagine I'll probably either leave the whole string in there because I'm lazy and it's not a mission-critical, time sensitive app, or try to do something with middleware or ??. Anyways, hope that helps someone else.

这篇关于在雄辩中使用多态关系扩展模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆