生成系谱树YII2

生成系谱树YII2,yii2,Yii2,我正在尝试建立血统 我的数据库模式: CREATE TABLE `Pedigrees` ( `pedigrees_id` int(10) NOT NULL AUTO_INCREMENT, `horse_name` varchar(32) NOT NULL, `sire_id` int(10) DEFAULT NULL, `dam_id` int(10) DEFAULT NULL, PRIMARY KEY (`pedigrees_id`), and other info data 控制器模型

我正在尝试建立血统 我的数据库模式:

CREATE TABLE `Pedigrees` ( 
`pedigrees_id` int(10) NOT NULL AUTO_INCREMENT,
`horse_name` varchar(32) NOT NULL,
`sire_id` int(10) DEFAULT NULL,
`dam_id` int(10) DEFAULT NULL, 
PRIMARY KEY (`pedigrees_id`),
and other info data
控制器模型和index.php和viev.php的Generete GII CRUD

获取SIRE和DAM的信息到viev.php

模型系谱:

public function getParentsir()
    {
        return $this->hasOne(Pedigrees::className(), ['pedigrees_id' => 'sire_id']);
    }
    public function getParentdam()
    {
       return $this->hasOne(Pedigrees::className(), ['pedigrees_id' => 'dam_id']);
    }
对于viev.php,请使用html+php()


和表格示例:

   <table>
        <TR><TD ROWSPAN=8 >
          <div>
            <h3> <?php
                    echo Html::a( Html::encode($model->name, $model->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->pedigrees_id])); 
                    echo Html::img('/web/images/photo_p/'.$model->photo_path, [ 'class'=>'img-responsive img-rounded']);;
                    echo "</h3>";
                    echo "<ul>
                            <li>$model->color</li>
                            <li>$model->land_stand</li>
                            <li>$model->date_birth</li>
                            <li>$model->titles</li>                                    
                    </ul> </div> </div> </TD>";
                 ?>
                <?php if ($model->parentsir) {   
                          echo "   <TD ROWSPAN=4 >

                                <div class=header><h3>sir1_1 ";
                                    echo Html::a( Html::encode($model->parentsir->name, $model->parentsir->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->parentsir->pedigrees_id]));
                                    echo Html::img('/web/images/photo_p/'.$model->parentsir->photo_path, [ 'class'=>'img-responsive img-rounded']);
                                    echo "<br>";
                                    echo Html::label( $model->parentsir->color);
                                    echo Html::label( $model->parentsir->date_birth);
                                    echo Html::label( $model->parentsir->titles);

                            echo "</h3></div>
                    </TD>
                    <TD ROWSPAN=2 >
                       <div class=header><h3>sir1_3";
                           if ($model->parentsir->parentsir) {
                              echo Html::a( Html::encode($model->parentsir->parentsir->name, $model->parentsir->parentsir->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->parentsir->parentsir->pedigrees_id]));
                              echo Html::img('/web/images/'.$model->parentsir->parentsir->photo_path, ['class'=>'img-responsive img-rounded']);
                              echo Html::label( $model->parentsir->parentsir->color);
                              echo Html::label( $model->parentsir->parentsir->date_birth);
                              echo Html::label( $model->parentsir->parentsir->titles);
                          } else {
                              echo "no info";
                        }    
                        ETC...
</table>


我能看到的第一件事是你需要修改你的
getParentsir
方法,而不是
['pedigrees\u id'=>'sire']
将它改为
['pedigrees\u id'=>'sire\u id']
。对
getParentdam
执行同样的操作。谢谢。对编辑。但用这种方法我只能得到3条sir和dam的记录。如果我尝试获取信息记录4,我看不到任何信息,我的意思是
parentdam->parentdam->parentdam->parentdam->parentdam->“info”
但是对于整个表,我需要7条sir记录和7条dam记录。例如
   <table>
        <TR><TD ROWSPAN=8 >
          <div>
            <h3> <?php
                    echo Html::a( Html::encode($model->name, $model->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->pedigrees_id])); 
                    echo Html::img('/web/images/photo_p/'.$model->photo_path, [ 'class'=>'img-responsive img-rounded']);;
                    echo "</h3>";
                    echo "<ul>
                            <li>$model->color</li>
                            <li>$model->land_stand</li>
                            <li>$model->date_birth</li>
                            <li>$model->titles</li>                                    
                    </ul> </div> </div> </TD>";
                 ?>
                <?php if ($model->parentsir) {   
                          echo "   <TD ROWSPAN=4 >

                                <div class=header><h3>sir1_1 ";
                                    echo Html::a( Html::encode($model->parentsir->name, $model->parentsir->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->parentsir->pedigrees_id]));
                                    echo Html::img('/web/images/photo_p/'.$model->parentsir->photo_path, [ 'class'=>'img-responsive img-rounded']);
                                    echo "<br>";
                                    echo Html::label( $model->parentsir->color);
                                    echo Html::label( $model->parentsir->date_birth);
                                    echo Html::label( $model->parentsir->titles);

                            echo "</h3></div>
                    </TD>
                    <TD ROWSPAN=2 >
                       <div class=header><h3>sir1_3";
                           if ($model->parentsir->parentsir) {
                              echo Html::a( Html::encode($model->parentsir->parentsir->name, $model->parentsir->parentsir->pedigrees_id), Url::to(['pedigrees/view', 'id' => $model->parentsir->parentsir->pedigrees_id]));
                              echo Html::img('/web/images/'.$model->parentsir->parentsir->photo_path, ['class'=>'img-responsive img-rounded']);
                              echo Html::label( $model->parentsir->parentsir->color);
                              echo Html::label( $model->parentsir->parentsir->date_birth);
                              echo Html::label( $model->parentsir->parentsir->titles);
                          } else {
                              echo "no info";
                        }    
                        ETC...
</table>