Yii Framework Forum: Relation MANY_MANY with info column in relation table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relation MANY_MANY with info column in relation table Rate Topic: ***** 1 Votes

#1 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 07-January 11

Posted 12 April 2011 - 07:28 AM

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
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?
Thanks Google Translator to helping me write the posts :)
0

#2 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 07-January 11

Posted 13 April 2011 - 04:30 PM

any idea?
Thanks Google Translator to helping me write the posts :)
0

#3 User is offline   ivo3d 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 15-December 10
  • Location:Sofia, Bulgaria

Posted 13 April 2011 - 05:18 PM

If you're using 1.1.7, maybe try this:

http://www.yiiframew...ry-with-through
0

#4 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 07-January 11

Posted 26 April 2011 - 04:35 PM

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

Database:

Posted Image

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?
Thanks Google Translator to helping me write the posts :)
0

#5 User is offline   jeanluca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 22-January 11
  • Location:Bunnik, Netherlands

Posted 27 April 2011 - 08:55 AM

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 ?
0

#6 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 07-January 11

Posted 27 April 2011 - 09:51 AM

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.
Thanks Google Translator to helping me write the posts :)
0

#7 User is offline   jeanluca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 22-January 11
  • Location:Bunnik, Netherlands

Posted 27 April 2011 - 01:56 PM

View PostAthos, on 27 April 2011 - 09:51 AM, said:

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)'),
                );
        }
}

0

#8 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 07-January 11

Posted 01 May 2011 - 02:42 PM

No expert can pronounce, even to say that does not work or that it is designed wrong, please?
Thanks Google Translator to helping me write the posts :)
0

#9 User is offline   creocoder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 09-March 09
  • Location:*.php

Posted 03 May 2011 - 06:04 PM

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.

No good, no bad, only consequence.
3

#10 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 07-January 11

Posted 03 May 2011 - 07:10 PM

Thank you, creocoder.

I will test and return if you find any problem.
Thanks Google Translator to helping me write the posts :)
0

#11 User is offline   Athos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 07-January 11

Posted 16 May 2011 - 03:07 PM

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

Thank you for your help!
Thanks Google Translator to helping me write the posts :)
0

#12 User is offline   NewbieHere 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 29-November 12

Posted 02 December 2012 - 04:47 AM

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.

0

#13 User is offline   LastDay 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 06-March 11
  • Location:Russia

Posted 02 December 2012 - 09:02 AM

View PostNewbieHere, on 02 December 2012 - 04:47 AM, said:

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

#14 User is offline   NewbieHere 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 29-November 12

Posted 04 December 2012 - 03:34 PM

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 :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users