1

我相信 Yii 的 AR 模型中的关系返回为Array。但是“Web Application Development with Yii and PHP”一书尝试使用主题作为对象实例,它会出错。我是否遗漏了什么或理解错误或者是书的错误?

例如,在“评论”AR 模型类中,我们有:

public function relations()
{

    return array(
        'author' => array(self::BELONGS_TO, 'User', 'create_user_id'),
    );
}

book 将“用户名”称为:

$comment->author->username

我使用:

$comment->author['username']

哪一个是正确的?

更新-> 我将把所有相关代码放在这里: AR 模型:

/**
 * This is the model class for table "tbl_comment".
 *
 * The followings are the available columns in table 'tbl_comment':
 * @property integer $id
 * @property string $content
 * @property integer $issue_id
 * @property string $create_time
 * @property integer $create_user_id
 * @property string $update_time
 * @property integer $update_user_id
 *
 * The followings are the available model relations:
 * @property User $updateUser
 * @property Issue $issue
 * @property User $createUser
 */
class Comment extends TrackStarActiveRecord
{
/**book
 */
public function recent($limit=5)
{
    $this->getDbCriteria()->mergeWith(array(
    'order'=>'t.create_time DESC',
    'limit'=>$limit,)
    );
    return $this;   
}
/**
 * Returns the static model of the specified AR class.
 * @param string $className active record class name.
 * @return Comment 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 'tbl_comment';
}

/**
 * @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('content, issue_id', 'required'),
        array('issue_id, create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
        array('create_time, update_time', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, content, issue_id, create_time, create_user_id, update_time, update_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(
        'updateUser' => array(self::BELONGS_TO, 'User', 'update_user_id'),
        'issue' => array(self::BELONGS_TO, 'Issue', 'issue_id'),
        'author' => array(self::BELONGS_TO, 'User', 'create_user_id'),
    );
}

/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'content' => 'Content',
        'issue_id' => 'Issue',
        'create_time' => 'Create Time',
        'create_user_id' => 'Create User',
        'update_time' => 'Update Time',
        'update_user_id' => 'Update 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('id',$this->id);
    $criteria->compare('content',$this->content,true);
    $criteria->compare('issue_id',$this->issue_id);
    $criteria->compare('create_time',$this->create_time,true);
    $criteria->compare('create_user_id',$this->create_user_id);
    $criteria->compare('update_time',$this->update_time,true);
    $criteria->compare('update_user_id',$this->update_user_id);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
  }
  }

小部件组件:

<?php
/**
* RecentCommentsWidget is a Yii widget used to display a list of
recent comments
*/
class RecentCommentsWidget extends CWidget
{
private $_comments;
public $displayLimit = 5;
public $projectId = null;
public function init()
{
    if(null !== $this->projectId)
        $this->_comments = Comment::model()-          >with(array('issue'=>array('condition'=>'project_id='.$this->projectId)))->recent($this->displayLimit)->findAll();
    else
        $this->_comments = Comment::model()->recent($this->displayLimit)->findAll();
}
public function getData()
{
return $this->_comments;
}
public function run()
{
// this method is called by CController::endWidget()
$this->render('recentCommentsWidget');
}
}

小部件视图:

<ul>
    <?php foreach($this->getData() as $comment): ?>
<div class="author">
    <?php echo $comment->author->username; ?> added a comment.
</div>
<div class="issue">
    <?php echo CHtml::link(CHtml::encode($comment->issue->name),
    array('issue/view', 'id'=>$comment->issue->id)); ?>
</div>
    <?php endforeach; ?>
</ul>

此代码会导致非对象错误,但是当我更改时

$comment->author->username

到 $comment->author['username'] 它工作正常。我想知道它是如何通过对象访问方法来处理 $issue 的。

4

2 回答 2

0

这两种访问方法都可以。CActiveRecord实例使用 PHPsArrayAccess类来提供对其属性的数组样式访问。

您可能指的是关系类型:

HAS_ONE&BELONGS_TO都返回一个可以直接使用的对象,就像你给出的例子一样。NULL如果没有找到行,这些也会返回。

HAS_MANY&MANY_MANY关系返回一个数组,您可以像这样使用它:$model->authors[0]->name$model->authors[1]->name. Array如果没有找到行,这些将返回并清空。

STAT关系再次有点不同,并返回单个标量值(整数、字符串、布尔值等)。返回0未找到,但这可以通过配置defaultValue

阅读此处了解更多信息:

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

于 2013-02-15T13:58:04.540 回答
0

推荐的形式是:

$comment->author->username

您可以使用下面的代码...

$comment->author['username']

...因为 PHP 将对象作为数组处理。

于 2013-02-15T14:08:31.307 回答