首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii 2架构的问题

Yii 2架构的问题
EN

Stack Overflow用户
提问于 2017-06-13 12:47:13
回答 1查看 101关注 0票数 0

我试图用YII2创建一个博客,我的框架令人困惑,无法从数据库调用数据。

例如,当我从"user“表调用"username”时,

代码语言:javascript
复制
<?= 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\模型\类别::fullname

请你帮我解决我犯错误的问题好吗?

这是我的帖子模型包含:

代码语言:javascript
复制
<?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']);
    }
}

在这里,用户模型:

代码语言:javascript
复制
<?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;
        }
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-13 13:17:18

Post模型中更改为此。

代码语言:javascript
复制
public function getUser()
{
    return $this->hasOne(User::className(), ['id' => 'category_id']);   
}

public function getCategory()
{
    return $this->hasOne(Category::className(), ['id' => 'user_id']);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44522000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档