Relation MANY_MANY with info column in relation table
#1
Posted 12 April 2011 - 07:28 AM
table_company
id_company
name_company
table_training
id_training
name_training
table_rel_company_training
id_company
id_training
number_employees
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?
#2
Posted 13 April 2011 - 04:30 PM
#4
Posted 26 April 2011 - 04:35 PM
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?
#5
Posted 27 April 2011 - 08:55 AM
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 ?
#6
Posted 27 April 2011 - 09:51 AM
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.
#7
Posted 27 April 2011 - 01:56 PM
Athos, on 27 April 2011 - 09:51 AM, said:
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)'),
);
}
}
#8
Posted 01 May 2011 - 02:42 PM
#9
Posted 03 May 2011 - 06:04 PM
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.
#10
Posted 03 May 2011 - 07:10 PM
I will test and return if you find any problem.
#11
Posted 16 May 2011 - 03:07 PM
Thank you for your help!
#12
Posted 02 December 2012 - 04:47 AM
$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.
#13
Posted 02 December 2012 - 09:02 AM
NewbieHere, on 02 December 2012 - 04:47 AM, said:
$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.yiiframew...sics.controller , http://www.yiiframew...en/basics.view. These are base concepts and you have to understand them in order to work with YII.
#14
Posted 04 December 2012 - 03:34 PM
I've read the link you gave and start working on it, now its working thanks for the help

Help













