Yii Framework Forum: Pivot Table Relation Field Form - Yii Framework Forum

Jump to content

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

Pivot Table Relation Field Form

#1 User is offline   JohnnyBeGood 

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

Posted 27 March 2014 - 06:38 PM

Pivot table relation

Anyone know how to present the checkboxList or any other widget in the update form, with the tags selected during the create article or post, already selected?

I believe this is something most of the people need.

Many thanks in advance.
0

#2 User is online   ORey 

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

Posted 28 March 2014 - 04:31 AM

It's pretty simple.
First you create model attribute to hold these values, like this:

public $pivot_ids = [];


Then you fill it with pivot values
$record->pivot_ids = \yii\helpers\ArrayHelper::getColumn(
    $record->getPivot()->asArray()->all(),
    'pivot_id'
);


where 'getPivot' is a relation to pivot table.

Finally you create a checkbox list:
<?= $form->field($record, 'pivot_ids')->checkboxList($options]); ?>


where 'options' is a list of options. You can get it like this:

$list = YourModel::find()
    ->select(['id', 'name'])
    ->asArray()
    ->all();

$options = \yii\helpers\ArrayHelper::map($list, 'id', 'name');


Personally I like to store this in model,

public static function listData()
{
    $data = static::find()
        ->active()
        ->select(['id', 'name'])
        ->orderBy('weight DESC, name')
        ->asArray()
        ->all();

    return \yii\helpers\ArrayHelper::map($data, 'id', 'name');
}


and then use $options = MyModel::listData()
God is real unless declared as integer
0

#3 User is offline   Nicola 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 179
  • Joined: 13-March 11
  • Location:Italy

Posted 28 March 2014 - 04:51 AM

I have a curiosity in your code:

public static function listData()
{
    $data = static::find()
        ->active()
        ->select(['id', 'name'])
        ->orderBy('weight DESC, name')
        ->asArray()
        ->all();

    return \yii\helpers\ArrayHelper::map($data, 'id', 'name');
}


what is "->active()"?
My company: DigiSin
0

#4 User is online   ORey 

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

Posted 28 March 2014 - 05:12 AM

Nothing special, just my custom named scope, something like 'is_disabled = 0'.
I dont want to output the records that are still in 'draft' state, so created a scope for those.
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 01 April 2014 - 08:11 PM

Finally, I've got it. Thanks to you. Many thanks ORey. You are the best.

It's a pity the documentation is so poor (for a while I hope)
I couldn't reach it if there wasn't a helpful member like you.

I think I will post a wiki with this simple relations solutions. Surely it will help a lot of people.

Many thanks once again.
0

#6 User is online   ORey 

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

Posted 02 April 2014 - 03:21 AM

As far as I know, there are two (!) books being written, so the docs are supposed to be awesome.

View PostJohnnyBeGood, on 01 April 2014 - 08:11 PM, said:

It's a pity the documentation is so poor (for a while I hope)
I couldn't reach it if there wasn't a helpful member like you.

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