Php Yii中的关系
我有 状态模型Php Yii中的关系,php,yii,Php,Yii,我有 状态模型 <?php /** * This is the model class for table "status". * * The followings are the available columns in table 'status': * @property integer $status_id * @property string $message * @property string $created * @property integer
<?php
/**
* This is the model class for table "status".
*
* The followings are the available columns in table 'status':
* @property integer $status_id
* @property string $message
* @property string $created
* @property integer $thumbs_up
* @property integer $thumbs_down
* @property string $reply
* @property integer $user_id
*
* The followings are the available model relations:
* @property Comment[] $comments
* @property User $user
* @property ThumbUpDown[] $thumbUpDowns
*/
class Status extends CActiveRecord {
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Status the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName() {
return 'status';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('message, created', 'required'),
array('thumbs_up, thumbs_down, user_id', 'numerical', 'integerOnly' => true),
array('message', 'length', 'max' => 255),
array('reply', 'length', 'max' => 45),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('status_id, message, created, thumbs_up, thumbs_down, reply, user_id', 'safe', 'on' => 'search'),
);
}
/**
* @return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'comments' => array(self::HAS_MANY, 'Comment', 'status_id'),
'user' => array(self::BELONGS_TO, 'Register', 'user_id'),
'thumbUpDowns' => array(self::HAS_MANY, 'Thumb_up_down', 'status_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'status_id' => 'Status',
'message' => 'Message',
'created' => 'Created',
'thumbs_up' => 'Thumbs Up',
'thumbs_down' => 'Thumbs Down',
'reply' => 'Reply',
'user_id' => 'User',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('status_id', $this->status_id);
$criteria->compare('message', $this->message, true);
$criteria->compare('created', $this->created, true);
$criteria->compare('thumbs_up', $this->thumbs_up);
$criteria->compare('thumbs_down', $this->thumbs_down);
$criteria->compare('reply', $this->reply, true);
$criteria->compare('user_id', $this->user_id);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
public function getUrl() {
return Yii::app()->createUrl('status', array(
'id' => $this->status_id,
'title' => $this->message,
));
}
public function addComment($comment) {
$comment->status_id = $this->status_id;
$comment->user_id = Yii::app()->user->id;
$comment->created = date('Y-m-d H:i:s');
return $comment->save();
}
}
看起来用户是一个数组。尝试$data->user['username']
看起来用户是一个数组。请尝试使用以下条件的“with”属性:
$criteria->with = array('user').
这比懒散的负载要好。看看这个。
如果您可以在使用$data->user->username的地方提供更多的代码,这会更好,因为它是gridview?使用以下条件的“with”属性:
$criteria->with = array('user').
这比懒散的负载要好。看看这个。
如果你能在使用$data->user->username时提供更多代码,那就更好了,因为它是gridview?这个错误(试图获取非对象的属性)意味着“user”表中没有id字段值等于$data->user\u id的记录
检查$data->user\U id的值,确保它存在于“user”表的id字段中。此错误(试图获取非对象的属性)意味着“user”表中没有id字段值等于$data->user\U id的记录
检查$data->user\U id的值,并确保它存在于“user”表的id字段中。尝试将您的状态
模型关系字符串更改为:
'user' => array(self::BELONGS_TO, 'Register', array('user_id' => 'user_id)),
希望能有所帮助。尝试将您的状态更改为:
'user' => array(self::BELONGS_TO, 'Register', array('user_id' => 'user_id)),
希望能有所帮助。您应该能够使用打印($data)
查看变量的内部结构。然后您将对如何使用变量有意见 您应该能够使用打印($data)
查看变量的内部结构。然后您将对如何使用变量有意见 希望这能对你有所帮助
$data["user"]["username"]
希望这能对你有所帮助
$data["user"]["username"]
确保user_id指向有效的用户记录几乎就是db外键的作用。(另外,根据模式的不同,user_id可能为NULL)。确保user_id指向有效的用户记录与db外键的作用非常相似。(另外,根据模式的不同,user_id可能为NULL)user
是一个关系,因此它要么是user
类的实例,要么是NULL(除非有人一直在手动处理addRelatedRecord
)。这和phpdoc的注释是一致的。为什么用户是数组而不是对象?这对我来说很有效,但是的,同样的问题。“为什么用户是数组而不是对象?”。我一直在努力寻找它不起作用的原因!事实上,这不太可能user
是一个关系,因此它要么是user
类的实例,要么是NULL(除非有人一直在手动处理addRelatedRecord
)。这和phpdoc的注释是一致的。为什么用户是数组而不是对象?这对我来说很有效,但是的,同样的问题。“为什么用户是数组而不是对象?”。我一直在努力寻找它不起作用的原因!