PHP / Laravel存储库模式,创建“复杂”对象的正确逻辑
作者:互联网
使用存储库模式来处理“复杂”对象的逻辑,来回…
顺便说一下,我正在使用Laravel框架.
好的,我在数据库中有这些表:
>用户
>公司
> company_translations
> company_contacts
> company_files
关系:
– 用户与公司有一对一的多态性(我有更多类型的用户,如管理员和客户)
– 公司有one-many with company_translations(公司的某些属性可以翻译成多种语言)
– 公司有一对多与company_contacts
– 公司有one-many with company_files
我有一个CompanyController和一个CompanyRepository.我试图把所有的创造逻辑
公司在CompanyRepository中,而不是使用公司其他部分的回购(联系人,文件,翻译).
因此,例如,当管理员创建公司时,控制器从包含所有数据的表单接收信息(还有一个表单请求,负责验证输入,典型规则)
关于公司(核心,翻译,文件,联系人),然后它会将该数据传递给repo函数,如:
$company = $this->companyRepository->create($data);
我的问题在于该函数,我如何构造/验证传递给函数的数据?它必须在表公司中创建一个条目,一个用于用户,一个或多个用于翻译,一个或多个用于联系人,0个或更多用于文件.
例如,来自有关翻译的请求的输入组织如下:
companyDescription['en']
companyDescription['es']
companyDescription['pt']
etc
回购不应该知道它的结构,对吧?我是否在控制器内执行此操作,例如:
$userData = [
'email' => $request->input('email'),
'password' => $request->input('password'),
etc
];
$companyData = [
'company_name' => $request->input('companyName'),
'company_slug' => $request->input('companySlug'),
etc
];
然后将这些变量传递给repo函数?此外,还需要一些字段,因此并非所有数据都是必需的,即文件不是必需的.
我如何链接所有必要的步骤?
或者我以完全不同的方式处理这个问题?
正确理解这一逻辑让我感到紧张……欢迎所有解决方案.
希望我很清楚/ – 解释我有什么,问题是什么.哦,我想坚持使用存储库模式;)
解决方法:
为了记录,表格溢出:
array: [▼
"active" => "1"
"highlighted" => "0"
"jobOffersApproved" => "1"
"trainingCoursesApproved" => "1"
"email" => ""
"password" => ""
"password_confirmation" => ""
"companyName" => ""
"companyDescription" => array:3 [▼
"pt" => ""
"en" => ""
"es" => ""
]
"businessSector" => ""
"fiscalNumber" => ""
"website" => ""
"linkedin" => ""
"facebook" => ""
"twitter" => ""
"googleplus" => ""
"country" => ""
"district" => ""
"county" => ""
"location" => ""
"address" => ""
"postalCode" => ""
"phone" => ""
"cellphone" => ""
"fax" => ""
"mainContact" => array:3 [▼
"firstName" => ""
"lastName" => ""
"email" => ""
]
"jobOffersContact" => array:3 [▼
"firstName" => ""
"lastName" => ""
"email" => ""
]
"trainingCoursesContact" => array:3 [▼
"firstName" => ""
"lastName" => ""
"email" => ""
]
]
“用户”表将收到此信息:
array: [▼
"active" => "1"
"email" => ""
"password" => ""
]
“公司”表:
array: [▼
"highlighted" => "0"
"jobOffersApproved" => "1"
"trainingCoursesApproved" => "1"
"companyName" => ""
"businessSector" => ""
"fiscalNumber" => ""
"website" => ""
"linkedin" => ""
"facebook" => ""
"twitter" => ""
"googleplus" => ""
"country" => ""
"district" => ""
"county" => ""
"location" => ""
"address" => ""
"postalCode" => ""
"phone" => ""
"cellphone" => ""
"fax" => ""
]
“company_contacts”表:
每个数组条目都是表中的一行.只有“mainContact”是强制性的.
array: [▼
"mainContact" => array:3 [▼
"firstName" => ""
"lastName" => ""
"email" => ""
]
"jobOffersContact" => array:3 [▼
"firstName" => ""
"lastName" => ""
"email" => ""
]
"trainingCoursesContact" => array:3 [▼
"firstName" => ""
"lastName" => ""
"email" => ""
]
]
此外,应该有一个来自表单的文件,关于公司徽标,这不是强制性的,并且会转到“company_files”表.
标签:php,repository-pattern,laravel 来源: https://codeday.me/bug/20190711/1429287.html