我有两个与多个相关的模型。
车间
const sinon = require("sinon");
const gnomesControllerFactory = require("gnomesController.js");
const Gnome = require("gnomeModel.js");
describe("Controllers: Gnomes", () => {
describe("get() gnomes", () => {
it("should return 400 when an error occurs", () => {
const request = {};
const response = {
send: sinon.spy(),
status: sinon.stub()
};
response.status.withArgs(400).returns(response);
Gnome.find = sinon.stub();
Gnome.find.withArgs({}).rejects("Error");
const gnomesController = gnomesControllerFactory(Gnome);
return gnomesController.get(request, response)
.then(arg => {
console.log(arg);
sinon.assert.calledWith(response.send, "Error");
});
});
});
});
注册
public $belongsToMany = [
'registrations' => [
'Wooltown\Events\Models\Registration',
'table' => 'wooltown_events_registration_workshop',
'count' => true,
],
];
我正在抓取这样的记录:
public $belongsToMany = [
'workshops' => [
'Wooltown\Events\Models\Workshop',
'table' => 'wooltown_events_registration_workshop',
'count' => true,
],
];
我期待结果中所有相关研讨会(在本例中为2)的数组,但研讨会数组只向我显示:
$vars['registration'] = Registration::with('workshops')->find($registration->id);
我在这里缺少什么?如何获得相关研讨会模型的集合?
答案 0 :(得分:2)
您应该将计数器用作单独的关系:
public $belongsToMany = [
'workshops' => [
'Wooltown\Events\Models\Workshop',
'table' => 'wooltown_events_registration_workshop',
],
'workshops_count' => [
'Wooltown\Events\Models\Workshop',
'table' => 'wooltown_events_registration_workshop',
'count' => true,
],
];