Yii Framework Forum: Handling Many To Many Relation - Yii Framework Forum

Jump to content

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

Handling Many To Many Relation

#1 User is offline   JohnnyBeGood 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 11-October 12

Posted 19 March 2014 - 07:50 PM

Anyone know how to deal with many to many relationships in Yii2?

I've found this excellent article http://www.larryullm...s-in-yii-forms/ but this is for Yii 1.

Many thanks in advance
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 19 March 2014 - 08:06 PM

Docs: https://github.com/y...ith-pivot-table
God is real unless declared as integer
0

#3 User is offline   JohnnyBeGood 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 11-October 12

Posted 19 March 2014 - 08:14 PM

View PostORey, on 19 March 2014 - 08:06 PM, said:



Thanks for your reply.
I've already read it, but my main problem is the form. How do I present the field if I've no attribute (like in yii1) in the model?

I can manage one to many:
Example (Employee and Organization)
Employee model
public function getOrganizationList() { 
  return ArrayHelper::map(Organization::find()->asArray()->all(), 'Id', 'Name');
}

Employee _form
<?= $form->field($model, 'Organization_Id')->dropDownList($model->organizationList) ?>


But many to many:
Example (Article and Category)
Article model
public function getCategoryList() { 
  return ArrayHelper::map(Category::find()->asArray()->all(), 'Id', 'Name');
}

Article _form
<?= $form->field($model, '######')->dropDownList($model->categoryList) ?>


the '######' is my problem and how to do multiple selection
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 20 March 2014 - 03:33 AM

Right now I'm using virtual attributes, like
public $item_ids;
<?= $form->field($record, 'item_ids')->checkboxList(...); ?>


And then in aftersave I grab those and create pivot items.

But I'm pretty sure there can be easier way to do it (like using relation name as a field name)
God is real unless declared as integer
0

#5 User is offline   JohnnyBeGood 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 11-October 12

Posted 20 March 2014 - 06:43 AM

View PostORey, on 20 March 2014 - 03:33 AM, said:

Right now I'm using virtual attributes, like
public $item_ids;
<?= $form->field($record, 'item_ids')->checkboxList(...); ?>


And then in aftersave I grab those and create pivot items.

But I'm pretty sure there can be easier way to do it (like using relation name as a field name)


As far as I know, there is no relation name in yii2
0

#6 User is offline   schmunk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 566
  • Joined: 02-November 08
  • Location:Stuttgart, Germany

Posted 22 March 2014 - 06:44 AM

Is there already an example how to save related records for a model?
Phundament - Yii Application Boilerplate with composer support
Fork on github

Follow phundament on Twitter

DevSystem: Mac OS X 10.7 - PHP 5.3 - Apache2 - Yii 1.1 / trunk - Firefox or Safari
0

#7 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 22 March 2014 - 08:31 AM

View Postschmunk, on 22 March 2014 - 06:44 AM, said:

Is there already an example how to save related records for a model?


http://www.yiiframew...post__p__243022
God is real unless declared as integer
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