tags:

views:

53

answers:

2

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 ?

A: 

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')));
SpawnCxy
A: 

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).

Travis Leleu
Thanks Travis, I will try it.In fact, i am finding the way how to get the total no. of item base on the id.example : <code>function getStudentByClassid($classid)</code>Using normal php coding, I just simply create a function and return interger, then apply it into the .php file like foreach($Class as $class){echo $class->getName();echo getStudentByClassid($class->getID());}but in cakephp, i can not simply call the function in Views !What is the proper way actually ?
Hugo
In the Cake way, rather than calling functions inside the views, the views only have arrays of data (excepting helpers...) So basically your controller calls the getStudentByClassid( $class_id ), and sets it to the view. In the controller, $this->set( 'students', $this->Class->getStudentsByClassId( $class_id ); Then in the view you have a $students array with the data in it.
Travis Leleu