简体   繁体   English

如何在CakePHP中重构更好的模型函数?

[英]How to refactor better model functions in CakePHP?

I'm reading programming best practices and it is said that when creating a function we should make it do a only single specific task. 我正在阅读编程最佳实践,据说在创建函数时,应该使它仅执行单个特定任务。

I got model functions that retrieves data and it's related data. 我有检索数据及其相关数据的模型函数。 Example: 例:

$this->Student->StudentAssignments(); $ this-> Student-> StudentAssignments();

Currently this function retrieves the student's assignments plus the question for each assignment and data about the student. 当前,此功能检索学生的作业以及每个作业的问题以及有关该学生的数据。 I use them all. 我全部用完了。 My dilemma is if I try to make separate functions that retrieves the related data (student and question datas) it's taxing since I'm producing more calls to the DB. 我的难题是,如果我尝试创建单独的函数来检索相关数据(学生和问题数据),那是很麻烦的,因为我正在向数据库发出更多的调用。

What would you guys suggest? 你们会建议什么?

I think you're doing fine. 我觉得你很好。 But you should reconsider renaming your function to 但是您应该重新考虑将功能重命名为

$this->Student->getStudentAssignmentsWithQuestions

Or whatever you think fit. 或者您认为合适的任何东西。 I think one should try to do as few calls to the database as possible (I assume you're performing a join somewhere in there), instead of fetching each set of elements by specific methods. 我认为应该尽量减少对数据库的调用(我假设您正在其中的某个位置执行联接),而不是通过特定的方法来获取每组元素。 This can lead to the fact that you'll get more methods (and therefore have to write some more tests), but I think this is the right way to do it. 这可能导致您将获得更多方法(因此必须编写更多测试)的事实,但是我认为这是正确的方法。

To defend the design argument: Your method does just one single task; 捍卫设计论点:您的方法仅执行一项任务。 it fetches student's assignments with each assignment's questions. 它以每个作业的问题获取学生的作业。

Something to keep in mind when doing this sort of refactoring... 进行这种重构时要记住的一点...

I typically will have a Model->getSomethingAndSomethingElse functions in my models. 我的模型中通常会有一个Model-> getSomethingAndSomethingElse函数。 These functions are public and meant to be called as a substitute for doing complicated (or any) find calls from the Controller. 这些函数是公共的,旨在代替Controller进行复杂的(或任何)find调用。

What I will usually do is then build up a small collection of private functions in the model. 然后,我通常要做的是在模型中建立一小部分私有函数。 In your case I might have something along the lines of... 在您的情况下,我可能会有一些类似...

Student->getStudentAssigmentsWithQuestions 学生-> getStudentAssigmentsWithQuestions

that then calls some private functions ie 然后调用一些私有函数,即

Student->getStudent which might call Student->joinStudentAssignment which in turn might call Assignment->joinAssignmentQuestion etc. Student-> getStudent可能会调用Student-> joinStudentAssignment,后者又可能会调用Assignment-> joinAssignmentQuestion等。

The double underscore prefixes have been removed since markdown wants to bold things because of them. 双下划线前缀已被删除,因为markdown希望因为它们而将其加粗。 If you are using php5 the underscores aren't really important anyways as long as you use the "private" or "proteced" keywords. 如果您使用的是php5,则只要您使用“ private”或“ proteced”关键字,下划线就并不重要。

Basically I use the public method as a container for a group of very specific query building or association building private functions within the models. 基本上,我将public方法用作模型中一组非常特定的查询构建或关联构建私有功能的容器。 This allows me to have an api that has complex data returned, but I build the query or the result set (depending on the type of data, relationships involved or query complexity) from small pieces - that can ideally be purposed and used in more than one public function call. 这使我可以拥有一个返回复杂数据的api,但是我可以从很小的部分构建查询或结果集(取决于数据的类型,所涉及的关系或查询的复杂性)-理想地,它可以用于一个公共函数调用。

No, if you're strictly concerned about code refactoring you should break down that blob into simpler functions that perform a single task as you said. 不,如果您严格关心代码重构,则应将该斑点分解为执行您所说的单个任务的简单函数。 Yes, you will hit more your database but considering how easy is to work with caching in cakephp, performance should not be an issue. 是的,您将访问更多的数据库,但是考虑到在cakephp中使用缓存有多么容易,性能应该不是问题。 And if it is, then you shouldn't worry about code refactoring at this point. 如果是这样,那么您现在就不必担心代码重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM