Getting relative data in MANY_MANY with CActiveDataProvider

Let’s create 3 simple talbles




CREATE TABLE `item1` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `item1` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `item1` VALUES ('1', 'запись11');

INSERT INTO `item1` VALUES ('2', 'запись12');


CREATE TABLE `item2` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `item2` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `item2` VALUES ('1', 'запись21');

INSERT INTO `item2` VALUES ('2', 'запись22');


CREATE TABLE `item_rel` (

  `id_item1` int(10) unsigned NOT NULL,

  `id_item2` int(10) unsigned NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `item_rel` VALUES ('1', '1');



And create 2 simple models




<?php


class Item1 extends CActiveRecord {


    public static function model($className=__CLASS__) {

        return parent::model($className);

    }


    public function tableName() {

        return 'item1';

    }


    public function relations() {


        return array(

            'item2' => array(self::MANY_MANY, 'Item2', 'item_rel(id_item1, id_item2)', 'together' => true),

        );

    }


}


?>






<?php


class Item2 extends CActiveRecord {


    public static function model($className=__CLASS__) {

        return parent::model($className);

    }


    public function tableName() {

        return 'item2';

    }


    public function relations() {


        return array(

            'item1' => array(self::MANY_MANY, 'Item1', 'item_rel(id_item2, id_item1)', 'together' => true),

        );

    }


}


?>



In controller I want to get model1 with relative model2




<?php


class ItemController extends Controller {


    public function actionIndex() {

        $dataProvider = new CActiveDataProvider('Item1', array(

                    'criteria' => array(

                        'with' => 'item2',

                    ),

                ));

        $this->render('index', array(

            'dataProvider' => $dataProvider,

        ));

    }


}

?>



In view I try to show data




<?php foreach ($dataProvider->data as $v) : ?>

    <?= $v->item1; ?> - <?= $v->item2->item2; ?>

<?php endforeach; ?>



When I try to print $v->item2->item2 it’s got error

"Trying to get property of non-object"

Relative model exists, var_dump($v->item2) is not empty.

sql query is correct




SELECT `t`.`id` AS `t0_c0`, `t`.`item1` AS

`t0_c1`, `item2`.`id` AS `t1_c0`, `item2`.`item2` AS `t1_c1` FROM `item1`

`t`  LEFT OUTER JOIN `item_rel` `item2_item2` ON

(`t`.`id`=`item2_item2`.`id_item1`) LEFT OUTER JOIN `item2` `item2` ON

(`item2`.`id`=`item2_item2`.`id_item2`)  LIMIT 10



How can I get property values from model2 ?

If the relation is a many-many, you will get many objects.

Try with:


<?php foreach ($dataProvider->data as $v) : ?>

    <?= $v->item1; ?> - <?php foreach ($v->item2 as $item2) ?><?= $item2->item2; ?> <?php endforeach;?>

<?php endforeach; ?>

Thank You for explanation.


<?=$v->item2[0]->item2;?>