2012-01-06 45 views
0

目前,我创建了2个类:学生和课程。我们在这里要关注的是精通。因此,每个course_type都有自己的一套proficiencies(保存在course_proficiencies中 - 每个课程都有一个课程类型)。一旦学生报名参加一门课程,我希望能够追踪他们已经完成的熟练程度(一旦学生完成了熟练程度,一行就会被添加到student_proficiencies中)。PHP OOP:创建多少个类

a schema http://slickrocksolutions.com/test/schema.png

现在..为了得到熟练程度(和已完成的熟练程度),我应该创建一个名为StudentProficiencies与getCourseProficiencies()和getCompletedProficienciesByStudent($ studentID)新的类,或者,我应该做的事情:

$course->getCourseProficiencies(); 
$student->getCompletedProficiencies($courseId); 

而且,你会如何建议我增加熟练度?如果我没有创建一个新类,它可能是:

$student->completeProficiency($proficiencyId, $courseId); 

有什么想法?

+0

命名约定:与表名(有些人会认为上也是如此),类名应该是真的单数,例如'学生'和'课程'。 – 2012-01-06 21:26:43

+0

我认为这是完全没有采取一些捷径。通常,您不需要为连接表创建类,如'student_proficiencies' – 2012-01-06 21:28:33

+0

他们是。课程是:学生和课程 – execv 2012-01-06 21:28:34

回答

1

三类:

学生, 场, 能力

Course_type是课程的属性。

哈希表如下: course_type_to_proficiency, course_to_student, course_to_course_type

我觉得应该让你做一个学生的方法来查找熟练使用类似这样的查询:

学生为> get_proficiencies(//连接数据库等, 'SELECT cttp.proficiencies FROM course_type_to_proficiency cttp INNER JOIN course_to_course_type ctct ON cttp.course_type_id = ctct.course_type_id INNER JOIN course_to_student cts ON ctct.course_id = cts.course_id WHERE student_id = ###'

没有必要使课程类型它自己的类,除非它不超过刚刚加入的课程,学生和熟练程度。如果您需要跟踪每个课程的完整字段,或者course_type为此创建另一个哈希表。

0

我想你需要这样的类:

  1. 学生
  2. CourseType
  3. CourseProfiency
  4. StudentProfiency

至于方法...尝试写一些简单的使用 - 你的模型 - 它可以帮助你看到什么接口(方法)你的类真的需要。


至于StudentProfiency。它可以是Repository模式的实现,它可以帮助你通过一些具体参数来检索一些类实例(具体对象)。

+0

你会如何一起喜欢一切?会是这样的: $ student = new Student($ mysqli,$ studentId); $ course = new Course($ mysqli,$ courseId); $ sp = new StudentProfiency($ student,$ course); //只传递ID或对象? $ sp-> add($ proficiencyId); – execv 2012-01-06 21:38:48

+0

阅读http://en.wikipedia.org/wiki/Active_record_pattern 例如,你可以看到这个模式在Yii框架中如何实现。 – 2012-01-06 21:43:32

+1

恕我直言,这只适用于在ORM中建模表格的情况。否则,第一个类对象就像samtresler描述的那样 – 2012-01-08 05:23:59

0

这是一个喜好的问题,我更喜欢使用每个普通实体的类,与学生和课程一起是一个好的开始。

如果你发现自己有一个与对象无关的方法,那么创建一个静态对象作为“工具箱”并不是一个坏主意,例如,以获取学生为例。为了获得熟练(和完成的熟练),我会为每个班级创建方法以获得相应的学生成绩和学习成绩。

然而,student_proficiencies和couse_proficiencies应该有一个外键给proficiencies表,以避免重复,如果它们具有相同的值。

希望这会有所帮助。

0

你已经准确地辨认出您的使用案例,即2个的数据实体。 StudentCourse。您关系中的所有其他表格都是这两个实体的标准化表格。请注意,语法为C#,请相应翻译。

class Student 
{ 
    // To uniquely identify each student. This is not the same as the id 
    // that you use in your Student table. 
    public string RegistrationId {get; set;} 
    public string FirstName {get; set;} 
    public string LastName {get; set;} 
    public Course Course {get; set;} 
    public Proficiency {get; set;} 

    // Get the list of courses completed by this student 
    public IEnumerable<Course> GetCompletedProficiencies() 
    { 
     // ... 
    } 

    // Add a completed course 
    public bool AddCompletedProficiency(Course completedCourse) 
    { 
     // ... 
    } 
} 

class Course 
{ 
    public string Id {get; set;} 
    public string Name {get; set;} 
    public string Location {get; set;} 
    public string Type {get; set;} 
} 

尽管在数据库中需要一些更改。在Student表,每个学生都应该由一个唯一的标识符,是破解的人类(因此RegistrationId)被称为,这是不一样的,你使用维护您Student表的主键id

作为主键使用的id通常是整数,只有数据库系统才知道(通常是一个AUTOINCREMENTInteger字段)。该RegistrationId应是一种可以包括University NameSchool NameYear of AdmissionAlphabetical Order of Student's name

相同的标识Id也将适用于Course类。

的方法GetCompletedProficiencies()AddCompletedProficiency()封装数据库操作。

对于OOP的建议将首先在您的用例或需求中标识这些类,然后再进入数据库设计部分。

更多信息