PHP / Laravel存储库模式,正确的逻辑创建"复杂"对象

时间:2016-11-14 12:22:42

标签: php laravel repository-pattern

绕过操纵"复杂"的逻辑。使用存储库模式的对象,来回... 顺便说一下,我正在使用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函数?此外,还需要一些字段,因此并非所有数据都是必需的,即文件不是必需的。 我如何链接所有必要的步骤? 或者我是以完全不同的方式处理这个问题?

正确理解这一逻辑让我感到紧张......欢迎所有解决方案。

希望我清楚+/-解释我所拥有的和问题是什么。哦,我想坚持使用存储库模式;)

1 个答案:

答案 0 :(得分:0)

为了记录,表格溢出:

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”表。