Many-Many Model Relationships

Hey everyone,

I have a project relating courses and professors. Each course can have multiple professors and each professor can have multiple courses, so I have a many-many table, professors_courses with a PRIMARY key defined on professor_id, course_id.

This table also contains some metadata about the professor/course combination. So, as a result, I have a model for that many-many relationship, ProfessorCourse.

I am trying to do this:


ProfessorCourse::model()->with( 'professor', 'course' )->findByPk( array( 'course_id' => 5, 'professor_id' => 10 ) );

Where my relations are defined as:


		return array(

			'course'	=> array( self::HAS_ONE, 'Course', 'course_id' ),

			'professor'	=> array( self::HAS_ONE, 'Professor', 'professor_id' )

		);

However, because of the composite key on ProfessorCourse in my many-many model, it is attempting to join courses ON (course.course_id = t.professor_id) (notice it is matching the course ID based on the combo’s professor ID)

Is there any way to specify which primary key in the many-many table to use on the JOIN for my relation? Does anyone have other suggestion on how to accomplish this task with models?

I’m an idiot. Just needed to use BELONGS_TO instead of HAS_ONE.

Another happy customer of the #yii channel :slight_smile: