0

I have two models: Users and Lessons

  • User has one Teacher
  • User has and belongs to many Students

Both Teachers and Students are Users

In my Users model I have this association:

public $hasMany = array(
    'Lesson' => array(
        'className' => 'Lesson',
        'foreignKey' => 'user_id',
        'dependent' => false,
    )
);

In my Lessons model I currently have this association:

public $belongsTo = array(
    'Teacher' => array(
        'className' => 'User',
        'foreignKey' => 'teacher_id',
        'conditions' => array('Teacher.group_id' => '2'),
        'fields' => '',
        'order' => ''
    )
);

My questions are:

  1. Why does $teachers = $this->Lesson->Teacher->find('list'); return all the users and not just the teachers? Why don't the conditions set in the belongsTo part of the model filter the results to show only the Users with the group_id of 2 i.e. the Teachers? And if that's not the way it's supposed to work then what is the point of setting conditions here?

    As of now I have to include it in the find each time which seems kind of redundant: $teachers = $this->Lesson->Teacher->find('list', array('conditions'=>array('Teacher.group_id'=>'2')))

  2. How do I write the associations for Students so that Lessons has many students? And how do I set up the database for it?

Community
  • 1
  • 1
Chaim
  • 2,109
  • 4
  • 27
  • 48
  • Regarding the database portion of your question: You need to share what your current tables _(users, teachers, students, lessons, users_teachers, etc.)_ are and _(at a minimum)_ those table's primary and foreign keys. Your question is confusing because the statement "Users has one Teacher" implies 2 tables due to the use of the Cakephp terminology. But then your belongsTo relationship within the Lesson model describes Teachers as a subset of the User table. – AgRizzo Oct 20 '14 at 11:40

1 Answers1

0

1. The condition in your association should be changed to include the Model's name rather than the alias:

'conditions' => array('User.group_id' => 2),

2. The association should be hasAndBelongsToMany (HABTM), and can be defined in your User model like this:

public $hasAndBelongsToMany = array(
    'Lessons' => array(
        'className' => 'Lesson',
        'joinTable' => 'lessons_users', // or the name from DB
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'lesson_id',
        'unique' => 'keepExisting' // Set this based on your needs, check link above
    )
);

Given the association above, you need a new table lessons_users in your database to store data about this relationship between students and lessons:

          +++++++++++++++++++++++++++++++++++++++++++++++++
          +  id (pk)  +  lesson_id (fk)  +  user_id (fk)  +
          +++++++++++++++++++++++++++++++++++++++++++++++++
          +     1     +         1        +        1       +
          +-----------+------------------+----------------+
          +     2     +         1        +        2       +
          +-----------+------------------+----------------+
          +     3     +         2        +        1       +
          +-----------+------------------+----------------+
          +     4     +         2        +        2       +
          +++++++++++++++++++++++++++++++++++++++++++++++++

From the example above; user with id 1 is taking lessons 1 & 2, and user with id 2 is taking lessons 1 & 2 as well. Likewise, lesson with id 1 has users with ids 1 & 2 enrolled, and lesson with id 2 has users with ids 1 & 2 enrolled as well.

kshaaban
  • 1
  • 3
  • 1) Changing the condition to use the model name instead of the alias results in a sql error (from what I understand CakePHP uses the alias in the query?) 2) How does this help me with accessing the Students in the Lesson model – Chaim Oct 19 '14 at 13:58
  • **1)** I'm not sure exactly sure on this one, but I think you can't use the alias there because this is where you are defining the alias to begin with! **2)** You would have to add the same association (changing field names ofc) to your `lesson` model if you didn't have it there before, then use Containable behavior for both models to find your data. Check this [question on similar matter.](http://stackoverflow.com/questions/13826474/cakephp-containable-with-habtm "HABTM Containable") – kshaaban Oct 19 '14 at 19:00