Relation MANY_MANY with info column in relation table

I have the following situation:

table_company

id_company

name_company

table_training

id_training

name_training

table_rel_company_training

id_company

id_training

[color="#FF00FF"]number_employees[/color]

Where table_rel_company_training is the NxN relationship. But it have a column for the number of employees participating in training.

What is the correct way to handle this case? With a MANY_MANY relation or winth BELONG_TO and HAS_MANY?

any idea?

If you’re using 1.1.7, maybe try this:

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through

Unfortunately I have not found a solution and sending more details to try to help.

Database:

Training’s model:




class Training {

(...)

	public function relations() {

		return array(

			'companys' => array(self::MANY_MANY, 'Company', 'rel_company_training(id_company, id_training)'),

		);

	}

}



Company’s model:




class Company {

(...)

	public function relations() {

		return array(

			'trainings' => array(self::MANY_MANY, 'Training', 'rel_company_training(id_company, id_training)'),

		);

	}

}



View training_form.php:




<?php 

(...)

foreach($model->companys as $company)

{

?>

           <div>

               <?php echo $form->labelEx($company,'name_company'); ?>

               <?php echo $form->textField($company, 'name_company'); ?>

           </div>

           <div>

               <?php echo $form->labelEx($company,'number_employees'); ?>

               <?php echo $form->textField($company, 'number_employees'); ?>

           </div>

<?

}

(...)

?>



Obviously, it will not work.

I intend to use the ztabularinputmanager extension to save the data.

My question is about the field ‘number_employees’ in the table rel_company_training. How to work with it?

Can anyone help me please?

Are you trying to get the ‘number_employees’ value from the relational table ?

I don’t think you can do it with the ARs. You have to query it yourself, or add this field to the company class!

Furthermore, does the MANY_MANY work ?

Yes, a need to read and write the value of ‘number_employees’ field in the relational table.

This field can not be on the table of company, since it is a many to many relationship.

Each table record relationship represents a company that participates in a training class. And each company has a number of employees participating.

Everything else works perfectly.

So it represent the employees of a company who want to participate in the training!

Anyway, I wouldn’t store any additional info in a relation-table, but create a model for this!

cheers

UPDATE: Shouldn’t the MANY MANY relation within Training not be defined as:




class Training {

(...)

        public function relations() {

                return array(

                        'companys' => array(self::MANY_MANY, 'Company', 'rel_company_training(id_training, id_company)'),

                );

        }

}



No expert can pronounce, even to say that does not work or that it is designed wrong, please?

Solution

Training model:




class Training {

...

        public function relations() {

                return array(

                        'companyTrainings' => array(self::HAS_MANY, 'CompanyTraining', 'id_training'),

                        'companies' => array(self::HAS_MANY, 'Company', 'id_company', 'through' => 'companyTraining'),

                );

        }

}



Company model:




class Company {

...

        public function relations() {

                return array(

                        'companyTrainings' => array(self::HAS_MANY, 'CompanyTraining', 'id_company'),

                        'trainings' => array(self::HAS_MANY, 'Training', 'id_training', 'through' => 'companyTraining'),

                );

        }

}



CompanyTraining model:




class CompanyTraining {

...

}



Using:




$models=Company::model()->with('companyTrainings','trainings')->findAll();

or

$models=Training::model()->with('companyTrainings','companies')->findAll();


number_employees will be availible in $model->companyTrainings array in both situations.



Thank you, creocoder.

I will test and return if you find any problem.

Today I finished work on this screen and it worked fine.

Thank you for your help!

sir creocoder, where should I code this? on what form? thanks




$models=Company::model()->with('companyTrainings','trainings')->findAll();

or

$models=Training::model()->with('companyTrainings','companies')->findAll();


number_employees will be availible in $model->companyTrainings array in both situations.



I am not creocoder, but I’ll answer - you could use it in any controller action you like and then send this $models variable to the view.

If you are not familiar with controllers and views, read this - http://www.yiiframework.com/doc/guide/1.1/en/basics.controller , http://www.yiiframework.com/doc/guide/1.1/en/basics.view. These are base concepts and you have to understand them in order to work with YII.

LastDay thank you very much for your help :D

I’ve read the link you gave and start working on it, now its working thanks for the help :)