开发者

how to display associate items in a single page?

It regarding cakephp, my database structure are as follows :

1.) Class

  • id
  • name
  • student_count

2.) Student

  • id
  • name
  • class_id
  • grade_id

3.) Grade

  • id
  • name

My question : I want my statistic page with the layout similar to -

1.) Class 1 (Total student = XX)

  • Grade A student = XX
  • Grade B student = XX
  • Grade C student = XX

开发者_开发问答2.) Class 2 (total student = XX)

  • Grade A student = XX
  • Grade B student = XX
  • Grade C student = XX

I am applying counterCache => true in my Student's Model with ,so that I can get the total number of student on each Classroom using a single find('all') function, but how about the number of Student in each Grade ? and how to combine 2 query into a single display ?


Not sure but I think you can try it like this:

$students = $this->Student->find('all',array('condtions'=>array('class_id'=>array($class1id,$class2id)),'group'=>array('class_id','grade_id')));


I think you may want to revisit your schema. Do you really only want a student to belong to only one class?

I'd recommend setting up class as the same. Change your Student model to have id, name. Then set up a Students HABTM Grade, setup a Grade hasAndBelongsTo Class. You will need a joining table, grades_students, which will link students and grades.

Now each student can have > 1 class, although you're limited to just 1 grade per student per class (what if a student repeats a class? This should be determined by your business design...)

Finally, with this setup, you will need to use the Containable behavior. It will allow you to filter from the Student model on criteria from the related models (generally with HABTM you can't do that otherwise).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜