【发布时间】:2017-06-13 12:47:13
【问题描述】:
我试图用 YII2 做一个博客,我的框架在从数据库中调用数据时很混乱。
例如,当我从“用户”表中调用“用户名”时,
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'user.fullname', --->> Yii2 is thinking that this is a category and not a user table
'title',
'description',
'content:html',
'count_view',
'status',
'created_at',
],
]) ?>
我收到此错误:-->> 未知属性:app\models\Category::fullname
请你帮我解决这个问题,我在哪里犯了错误?
这是我的帖子模型包含:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "post".
*
* @property integer $id
* @property integer $user_id
* @property string $title
* @property string $description
* @property string $content
* @property integer $count_view
* @property string $status
* @property string $created_at
*
* @property User $user
* @property TagAssign[] $tagAssigns
*/
class Post extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'post';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'count_view','category_id'], 'integer'],
[['content', 'status'], 'string'],
[['created_at'], 'safe'],
[['count_view'], 'default','value'=>0],
[['user_id'], 'default','value'=>Yii::$app->user->id],
[['title', 'description'], 'string', 'max' => 255],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'title' => 'Title',
'description' => 'Description',
'content' => 'Content',
'category' => 'Category',
'count_view' => 'Count View',
'status' => 'Status',
'created_at' => 'Created At',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(Category::className(), ['id' => 'user_id']);
}
public function getCategory()
{
return $this->hasOne(User::className(), ['id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTagAssigns()
{
return $this->hasMany(TagAssign::className(), ['post_id' => 'id']);
}
}
这里的用户模型:
<?php
namespace app\models;
use Yii;
use yii\web\IdentityInterface;
/**
* This is the model class for table "user".
*
* @property integer $id
* @property string $username
* @property string $password
* @property string $fullname
* @property string $status
* @property string $role
* @property string $created_At
*
* @property Post[] $posts
*/
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user';
}
public $current_password;
public $new_password;
public $confirm_password;
public $authKey;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['status', 'role'], 'string'],
[['created_At'], 'safe'],
[['username', 'password', 'fullname'], 'string', 'max' => 45],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'username',
'password' => 'password',
'fullname' => 'fullname',
'status' => 'Status',
'role' => 'Role',
'created_At' => 'Created At',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPosts()
{
return $this->hasMany(Post::className(), ['user_id' => 'id']);
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token,$type=null)
{
return static::findOne(['access_token'=>$token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey == $authKey;
}
public static function findByUsername($username)
{
return static::findOne(['username'=>$username]);
}
public function validatePassword($password)
{
if(Yii::$app->security->validatePassword($password,$this->password))
{
return true;
} else {
return false;
}
}
}
【问题讨论】:
-
您是否尝试将数据库和小部件中的列重命名为
user_fullname? -
Category和user型号?检查关系名称和属性名称。 -
你的
$model包含什么内容? -
是的,它不工作
-
这是我的模型---> public function attributeLabels() { return [ 'id' => 'ID', 'user_id' => 'User ID', 'title' => 'Title ', 'description' => 'Description', 'content' => 'Content', 'category' => 'Category', 'count_view' => 'Count View', 'status' => 'Status', 'created_at ' => '创建于', ]; }
标签: php yii2 frameworks