qiuxis 2016-09-30 01:26:43 8803次浏览 2条评论 7 0 0

model 文件 Lib.php 里面


namespace frontend\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;
/**
 * This is the model class for table "{{%lib}}".
 *
 * @property integer $libid
 * @property string $libname
 * @property integer $classid
 * @property integer $isuser
 * @property integer $isopen
 * @property string $libimg
 * @property string $note
 * @property integer $allowedit
 * @property integer $allowpractice
 * @property integer $comments
 * @property integer $onclick
 * @property integer $collection_num
 * @property integer $practice_num
 * @property integer $xbeans
 * @property integer $status
 * @property integer $created_by
 * @property integer $updated_by
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property LibClass $class
 */
class Lib extends \yii\db\ActiveRecord
{
    public $tree = [] ;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%lib}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['libname', 'classid', 'created_by', 'updated_by'], 'required'],
            [['classid', 'isuser', 'isopen', 'allowedit', 'allowpractice', 'comments', 'onclick', 'collection_num', 'practice_num', 'xbeans', 'status', 'created_by', 'updated_by', 'created_at', 'updated_at'], 'integer'],
            [['note'], 'string'],
            [['libname', 'libimg'], 'string', 'max' => 255],
            [['libname'], 'unique'],
            [['classid'], 'exist', 'skipOnError' => true, 'targetClass' => LibClass::className(), 'targetAttribute' => ['classid' => 'classid']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        $mappath = 'frontend/models/lib';
        return [
            'libid' => Yii::t($mappath, 'libid'),
            'libname' => Yii::t($mappath, 'libname'),
            'classid' => Yii::t($mappath, 'classid'),
            'isuser' => Yii::t($mappath, 'isuser'),
            'isopen' => Yii::t($mappath, 'isopen'),
            'libimg' => Yii::t($mappath, 'libimg'),
            'note' => Yii::t($mappath, 'note'),
            'allowedit' => Yii::t($mappath, 'allowedit'),
            'allowpractice' => Yii::t($mappath, 'allowpractice'),
            'comments' => Yii::t($mappath, 'comments'),
            'onclick' => Yii::t($mappath, 'onclick'),
            'collection_num' => Yii::t($mappath, 'collection_num'),
            'practice_num' => Yii::t($mappath, 'practice_num'),
            'xbeans' => Yii::t($mappath, 'xbeans'),
            'status' => Yii::t($mappath, 'status'),
            'created_by' => Yii::t($mappath, 'created_by'),
            'updated_by' => Yii::t($mappath, 'updated_by'),
            'created_at' => Yii::t($mappath, 'created_at'),
            'updated_at' => Yii::t($mappath, 'updated_at'),
        ];
    }

    /**
     *when create and update
     */
    public function behaviors()
    {
        return [TimestampBehavior::className()];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getClass()
    {
        return $this->hasOne(LibClass::className(), ['classid' => 'classid']);
    }

    /**
     * @inheritdoc
     * @return LibQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new LibQuery(get_called_class());
    }

    /**
     * @inheritdoc
     *
     */

    public function getData()
    {
        //$cates = LibClass::find()->asArray()->all();
        $cates = LibClass::find()->all();
        $cates = ArrayHelper::toArray($cates);
        return $cates;
    }

    public function getCatTree($cats , $bclassid = 0, $nu = 0 )
    {
        $bx = '---|' ;
        $nu++ ;
        foreach ($cats as $cat){
            $catid		= $cat['classid'];
            $catname	= $cat['classname'];
            $catbid		= $cat['bclassid'];
            $islast     = $cat['islast'];
            if ($catbid == $bclassid) {
                $this->tree[$catid]= str_repeat($bx, $nu) .'~'. $catname . ($islast ? '_last' : '') . PHP_EOL  ;
                $this->getCatTree($cats, $catid, $nu ) ;
            }
        }
    }

    public function tree() {
        $cats = $this->getData();
        $this->getCatTree($cats, 0, 0);
        return $this->tree ;
    }
}

//view 文件 _from.php 里面

<?= $form->field($model, 'classid')->dropDownList($model->tree(), ['prompt'=>'请选择']) ?>

//最终结果
QQ截圖20160930012553.png

  • 评论于 2016-10-28 18:21 举报

    我的为什么会提示

    QQ截图20161028181545.png

    请帮我看一下,我贴上代码

    <?php
    
    namespace backend\models;
    
    use Yii;
    use yii\behaviors\TimestampBehavior;
    use yii\helpers\ArrayHelper;
    use yii\db\ActiveRecord;
    
    /**
     * This is the model class for table "category".
     *
     * @property string $cateid
     * @property string $catename
     * @property string $parentid
     * @property string $createtime
     */
    class Category extends \yii\db\ActiveRecord
    {
        public $tree = [];
        /**
         * @inheritdoc
         */
        public static function tableName()
        {
            return 'category';
        }
    
        /*
         *添加默认填充时间
         */
        public function behaviors() 
        {
            return [
                [
                    'class' => TimestampBehavior::className(),
                    'attributes' => [
                        ActiveRecord::EVENT_BEFORE_INSERT => ['createtime'],
                        ActiveRecord::EVENT_BEFORE_UPDATE => ['createtime'],
                    ],
                ],
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [['parentid'], 'integer'],
                [['catename'], 'string', 'max' => 32,'tooLong' => '分类名称太长了'],
                ['createtime', 'safe']
            ];
        }
    
        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            
            return [
                'cateid' => Yii::t('app', 'Cateid'),
                'catename' => Yii::t('app', 'Catename'),
                'parentid' => Yii::t('app', 'Parentid'),
                'createtime' => Yii::t('app', 'Createtime'),
            ];
        }
        public function getClass()
        {
            return $this->hasOne(Category::className(),['parentid' => 'parentid']);
        }
    
        public static function find()
        {
            return new CategoryQuery(get_called_class());
        }
    
        public function getData()
        {
            $cates = Category::find()->all();
            $cates = ArrayHelper::toArray($cates);
            return $cates;
        }
    
        public function getCatTree($cats,$bclassid= 0, $nu = 0)
        {
            $bx = '---|';
            $nu++;
            foreach ($cats as $cat) {
                $catid = $cat['parentid'];
                $catname = $cat['catename'];
                $catbcid = $cat['bclassid'];
                if ($catbcid == $bclassid) {
                    $this->tree[$catid] = str_repeat($bx,$nu).'~'.$catename.PHP_EOL;
                    $this->getCatTree($cats,$catid,$nu);
                }
            }
        }
    
        public function tree()
        {
            $cats = $this->getData();
            $this->getCatTree($cats,0,0);
            return $this->tree ;
        }
    }
    
    

    这是model的代码,视图中是这样的

    <?= $form = ActiveForm::begin() ?>
    		<?= $form->field($model,'cateid')->dropDownList($model->tree(),['prompt'=> '请选择']) ?>
    	<?= $form->field($model,'catename')->textInput(['maxlength' => true]) ?>
    	<div class="form-group">
    		<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    	</div>
    	<?= ActiveForm::end() ?>
    
    1 条回复
    评论于 2016-10-28 21:05 回复

    先看看这个吧!
    需要有个父ID,即 bclassid = 0
    http://www.yiichina.com/tutorial/991

    另外建议先打印出结果

  • 评论于 2016-09-30 15:29 举报

    很不错的例子

您需要登录后才可以评论。登录 | 立即注册