通过急切加载查找总和

时间:2015-08-18 17:25:53

标签: php laravel-5.1

我有一个表模式如下:

+----+---------------+------------+--------+
| id | crowd_fund_id |   email    | amount |
+----+---------------+------------+--------+
|  1 |            11 | jj@xx.com  |    200 |
|  2 |            11 | sd@ff.com  |    250 |
|  3 |            12 | jj@xx.com  |    150 |
|  4 |            12 | abc@cc.com |    230 |
+----+---------------+------------+--------+

Entries表:

+----+---------+----------+------+
| id | user_id | crowd_id | name |
+----+---------+----------+------+
|  1 |       6 |       11 | Abc  |
|  2 |       6 |       12 | Xyc  |
|  3 |       8 |       18 | ijn  |
+----+---------+----------+------+

在Backer的模型中

public function entry()
    {
        return $this->belongsTo('App\Entries', 'crowd_fund_id', 'crowd_id');
    }

在控制器中我打电话给:

$var = Backers::with('entry')->where('email', $user->email)->get();

这很好用。现在我想通过急切加载获得sum。 这意味着我需要调用像

这样的东西
Backers::with('entry')->with('sum')->where('email', $user->email)->get();

sum将计算amount所有crowd_fund_id等于原始email = $user->email的所有Backers::with('entry')->with('sum')->where('email', $user->email)->get(); 的总和。 这意味着当我打电话

crowd_fund_id

我应该得到:
1为crowd_fund_id = crowd_id的支持者详细信息 1为原始的amount的相应条目 所有crowd_fund_id = crowd_fund_id的总和,其中Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 177, in run_wsgi execute(self.server.app) File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 165, in execute application_iter = app(environ, start_response) File "/usr/lib/python2.7/dist-packages/openerp/service/server.py", line 281, in app return self.app(e, s) File "/usr/lib/python2.7/dist-packages/openerp/service/wsgi_server.py", line 216, in application return application_unproxied(environ, start_response) File "/usr/lib/python2.7/dist-packages/openerp/service/wsgi_server.py", line 202, in application_unproxied result = handler(environ, start_response) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 1280, in __call__ return self.dispatch(environ, start_response) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 1254, in __call__ return self.app(environ, start_wrapped) File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 588, in __call__ return self.app(environ, start_response) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 1421, in dispatch result = ir_http._dispatch() File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_http.py", line 168, in _dispatch return self._handle_exception(e) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_http.py", line 138, in _handle_exception return request._handle_exception(exception) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 659, in _handle_exception return super(HttpRequest, self)._handle_exception(exception) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_http.py", line 164, in _dispatch result = request.dispatch() File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 677, in dispatch r = self._call_function(**self.params) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 303, in _call_function return checked_call(self.db, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 113, in wrapper return f(dbname, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 300, in checked_call return self.endpoint(*a, **kw) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 796, in __call__ return self.method(*args, **kw) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 396, in response_wrap response = f(*args, **kw) File "/usr/lib/python2.7/dist-packages/openerp/addons/report/controllers/main.py", line 62, in report_routes html = report_obj.get_html(cr, uid, docids, reportname, data=options_data, context=context) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/report/models/report.py", line 175, in get_html return self.render(cr, uid, [], report.report_name, docargs, context=context) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/report/models/report.py", line 151, in render return view_obj.render(cr, uid, template, values, context=context) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_ui_view.py", line 1029, in render return self.pool[engine].render(cr, uid, id_or_xml_id, qcontext, loader=loader, context=context) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 259, in render return self.render_node(self.get_template(id_or_xml_id, qwebcontext), qwebcontext) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_qweb.py", line 188, in get_template xml_doc = qwebcontext.loader(name) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_ui_view.py", line 1027, in loader return self.read_template(cr, uid, name, context=context) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "<string>", line 2, in read_template File "/usr/lib/python2.7/dist-packages/openerp/tools/cache.py", line 121, in lookup value = d[key] = self.method(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_ui_view.py", line 859, in read_template arch = self.read_combined(cr, uid, view_id, fields=['arch'], context=context)['arch'] File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/base/ir/ir_ui_view.py", line 524, in read_combined while v.mode != 'primary': File "/usr/lib/python2.7/dist-packages/openerp/fields.py", line 823, in __get__ return record._cache[self] File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5906, in __getitem__ return value.get() if isinstance(value, SpecialValue) else value File "/usr/lib/python2.7/dist-packages/openerp/fields.py", line 55, in get raise self.exception MissingError: ('MissingError', u'One of the documents you are trying to access has been deleted, please try again after refreshing.') 来自支持者的详细信息。

我怎么能得到这个?

1 个答案:

答案 0 :(得分:1)

您可以尝试沿着这些方向(在Backers模型中):

public function backersSum()
{
    return $this->hasOne('App\Backer')
        ->selectRaw('crowd_fund_id, sum(amount) as aggregate')
        ->groupBy('crowd_fund_id');
}

这样做可以让你像任何关系一样急切地加载它。然后你可以做这样的事情来访问它:

public function getBackersSumAttribute()
{
  if ( ! $this->relationLoaded('backersSum')) 
    $this->load('backersSum');

  $related = $this->getRelation('backersSum');

  return ($related) ? (int) $related->aggregate : 0;
}