Relations in TreeGridMultitableConnector


#1

Hi!

I’m trying to create TreeGrid with TreeGridMultitableConnector connector, but getting same data on second levels.

I’ve created two tables:

[code]CREATE TABLE test_cats (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
CHARACTER SET utf8
COLLATE utf8_general_ci;

INSERT INTO test_cats(id, name) VALUES (1, ‘CatA’);
INSERT INTO test_cats(id, name) VALUES (2, ‘CatB’);
INSERT INTO test_cats(id, name) VALUES (3, ‘CatC’);

CREATE TABLE test_subcats (
id int(11) NOT NULL AUTO_INCREMENT,
pid int(11) NOT NULL,
name varchar(50) DEFAULT NULL,
PRIMARY KEY (id),
CONSTRAINT FK_test_subcats_pid FOREIGN KEY (pid)
REFERENCES geo3.test_cats (id) ON DELETE NO ACTION ON UPDATE RESTRICT
)
ENGINE = INNODB
CHARACTER SET utf8
COLLATE utf8_general_ci;

INSERT INTO test_subcats(id, pid, name) VALUES (3, 1, ‘Subcat1’);
INSERT INTO test_subcats(id, pid, name) VALUES (4, 1, ‘Subcat2’);
INSERT INTO test_subcats(id, pid, name) VALUES (5, 1, ‘Subcat3’);
INSERT INTO test_subcats(id, pid, name) VALUES (6, 2, ‘Subcat21’);
INSERT INTO test_subcats(id, pid, name) VALUES (7, 2, ‘Subcat22’);
INSERT INTO test_subcats(id, pid, name) VALUES (8, 3, ‘Subcat31’);
[/code]

And models in Yii2.0:

[code]<?php

namespace app\models;

use Yii;
class TestCats extends \yii\db\ActiveRecord
{
public static function tableName()
{
return ‘test_cats’;
}
public function rules()
{
return [
[[‘name’], ‘string’, ‘max’ => 50],
];
}
public function getTestSubcats()
{
return $this->hasMany(TestSubcats::className(), [‘pid’ => ‘id’]);
}
}
?>
[/code]

[code]<?php

namespace app\models;

use Yii;
class TestSubcats extends \yii\db\ActiveRecord
{
public static function tableName()
{
return ‘test_subcats’;
}
public function rules()
{
return [
[[‘pid’], ‘required’],
[[‘pid’], ‘integer’],
[[‘name’], ‘string’, ‘max’ => 50],
[[‘pid’], ‘exist’, ‘skipOnError’ => true, ‘targetClass’ => TestCats::className(), ‘targetAttribute’ => [‘pid’ => ‘id’]],
];
}
public function getP()
{
return $this->hasOne(TestCats::className(), [‘id’ => ‘pid’]);
}
}
?>
[/code]

Controller:

[code] public function run()
{
$connector = new TreeGridMultitableConnector(null, “PHPYii”);
$connector->setMaxLevel(2);
$level = $connector->get_level();

    switch ($level){
        case 0:
            $connector->render_table(new TestCats(), "id", "name", "", "");
            break;
        case 1:
            $connector->render_table(new TestSubcats(), "id", "name", "", "pid");
            break;
    }
}

[/code]

View:

let grid = cm.attachGrid({ image_path: './dhtmlx/skins/skyblue/imgs/' }); grid.setHeader('Наименование,Инв. номер,Кол-во,Неиспр.,Коментарий,Описание'); grid.setInitWidths('*,140,100,80,*,*'); grid.setColSorting('str,str,int,na,str,str'); grid.setColTypes("tree,ro,ro,ro,ro,ro"); grid.enableAutoWidth(true); grid.setDateFormat('%d.%m.%Y'); grid.setFiltrationLevel(-1); grid.init(); grid.load('index.php?r=test/catalog'); grid.kidsXmlFile='index.php?r=test/catalog';

I expected to see tree like CatA->Subcat1,Subcat2,Subcat3; CatB->Subcat21,Subcat22;CatC->Subcat31, because last attribute ‘relation_id’ in render_table is ‘pid’. What I need to do for building correct tree?