好吧首先,我正在與Yii 2基本不高級版本。yii 2.0管理創建不工作從ActiveForm
好吧,我已經做到了這一點,以便我的日誌功能可以將數據從數據庫中提取出來,並將其作爲硬編碼的用戶。然後我在索引視圖中顯示數據庫中所有管理員的gridview及其ID。我試圖創建一個管理員可以創建另一個管理員的創建頁面。
我確實設法得到這個工作,但對於我的生活,我無法找到我做了什麼使這個功能停止工作。
型號\ user.php的
<?php
namespace app\models;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\data\ActiveDataProvider;
/**
* User model
*
* @property integer $id
* @property string $username
* @property string $authkey
* @property string $password
* @property string $accessToken
*/
class User extends ActiveRecord implements IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'Users';
}
public function rules(){
return [
[['username','password'], 'required']
];
}
public static function findAdmins(){
$query = self::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id]);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return static::findOne('AuthKey');
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return static::findOne(['AuthKey' => $authKey]);
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;
}
}
查看\ ADMIN \的index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
$this->title = 'Admins';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Admin', ['create'], ['class' => 'btn btn-success']);?>
</p>
<?php
if(isset($user_id)){
print_r($user_id);
}
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
'username',
'password',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
所以此工程,並連接到我的數據庫罰款。它顯示了正確的欄目等。
現在,我會告訴你這是不工作的兩種觀點,A create.php這使得_form.php這個
意見\ ADMIN \ create.php:
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
$this->title = 'Create Admin';
$this->params['breadcrumbs'][] = ['label' => 'admin', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
if(isset($username)){
print_r($username);
echo '</br>';
print_r($password);
}
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form',[
'model' => $model
]);
?>
</div>
意見\ admin_form.php:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $model app\models\Users */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="admin-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->textInput(); ?>
<?= $form->field($model, 'password')->textInput(); ?>
<div class="form-group">
<?= Html::submitButton('create', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
現在終於我的控制器:
個控制器\ AdminController.php:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\models\User;
class AdminController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
$searchModel = new User;
$dataProvider = $searchModel->findAdmins();
return $this->render('index',[
'dataProvider' => $dataProvider
]);
}
public function actionCreate(){
$model = new User;
$request = Yii::$app->request;
$post = $request->post();
$username = "usernameStandard";
$password = "passwordStandard";
if($model->load(Yii::$app->request->post())){
$username = $post['User']['username'];
$password = $post['User']['password'];
}
return $this->render('create',[
'model' => $model,
'username' => $username,
'password' => $password
]);
}
}
那麼好吧所以,當我嘗試這樣做,我填寫表格從create.php(與渲染_form.php這個)。當我點擊創建按鈕。我print_r()後的結果,以確保沒問題,它確實再次顯示頁面的帖子結果。但是它不會在我的數據庫中創建一個新用戶。
真的卡在這裏任何人都可以幫忙嗎?
這個工作的感謝 –