controllers:
<?php
namespace app\controllers;
use Yii;
use yii\helpers\Json;
use yii\web\Controller;
use app\models\Folder;
class HomeController extends Controller
{
public $model;
public function init()
{
parent::init();
$this->model = new Folder();
}
public function actionIndex()
{
$view = Yii::$app->view;
$view->params['new'] = $this->model;
$folders = $this->model ->search();
return $this->render('index',[
'folders' => $folders,
]);
}
}
model:
<?php
namespace app\models;
use Yii;
class Folder extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public $fname;
public $type;
public $pid;
public static function tableName()
{
return 'folder';
}
public function search($param=null)
{
$condition = $param == null ? ['pid' => 0, 'type' => 0] : ['pid' => $param];
$query = Folder::find();
$folders = $query->where($condition)->all();
return $folders;
}
}
view:
<?php
/* @var $this yii\web\View */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Modal;
use app\assets\AppAsset;
AppAsset::register($this);
$this->title = 'CodeNote';
?>
<p class="left-container col-lg-2">
<p class="folder">
<?php foreach ($folders as $folder): ?>
<p id=<?= Html::encode("{$folder->id} ") ?> type=<?= Html::encode("{$folder->type} ") ?> >
<?= Html::encode("{$folder->fname} ") ?>
</p>
<?php endforeach; ?>
</p>
</p>
<p class="right-container col-lg-10">
</p>
<?php
Modal::begin([
'header' => '<h2>新建</h2>',
'id' => 'new-modal'
]);?>
<?php $form = ActiveForm::begin([
'id' => 'folder',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
'action' => ['/home/new'],
'layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n<p class=\"col-lg-6\">{input}</p>\n<p class=\"col-lg-offset-4 col-lg-6\">{error}</p>",
'labelOptions' => ['class' => 'col-lg-4 control-label '],
],
]); ?>
<?= $form->field($this->params['new'], 'fname', ['labelOptions' => ['label' => '名字']])->textInput(['autofocus' => true]) ?>
<?= $form->field($this->params['new'], 'type')->textInput()->hiddenInput(['value'=>'0'])->label(false); ?>
<?= $form->field($this->params['new'], 'pid')->textInput()->hiddenInput(['value'=>'0'])->label(false); ?>
<p class="form-group">
<p class="col-lg-offset-4 col-lg-4">
<?= Html::submitButton('确定', ['class' => 'btn btn-primary', 'name' => 'new-button']) ?>
</p>
</p>
<?php ActiveForm::end(); ?>
<?php Modal::end(); ?>
<?php
?>
数据库含有fname,type,pid,id字段,当运行上面程序时无法显示内容。但将model的变量$fname和ActiveForm的fname换成别的名字就能显示了。为什么这两个不能够同名呢??
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号