2012-01-06 62 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的建議將首先在您的用例或需求中標識這些類,然後再進入數據庫設計部分。

更多信息