在行为类中获取变量值

时间:2014-09-18 10:17:16

标签: php yii

如何在模型类中获取变量$mapCommentColumn的值?

这是CommentableBehavior

中的modules/comment/behaviors/CommentableBehavior.php课程
class CommentableBehavior extends CActiveRecordBehavior
{
    /**
     * @var string name of the table defining the relation with comment and model
     */
    public $mapTable = null;
    /**
     * @var string name of the table column holding commentId in mapTable
     */
    public $mapCommentColumn = 'commentId';
    /**
     * @var string name of the table column holding related Objects Id in mapTable
     */
    public $mapRelatedColumn = null;

    .......

}

modules/comment/models/comment.php

protected function beforeSave()
    {   
        $commentedModel = CActiveRecord::model($this->module->commentableModels[$this->type]);
        $db = array(
            'join' => "JOIN " . $this->mapTable . " cm ON t.id = cm." . $this->mapCommentColumn,
            'condition' => "cm." . $this->mapYearColumn . "=:year
                            AND t.pending=:pending",
            'params' => array(':year'=>$_GET['year'], ':pending'=>0)
        );
        print_r(CommentableBehavior::getCommentCount($db));

 }

我希望从我的$this->mapTable课程中获得$this->mapCommentColumnCommentableBehavior。有任何想法吗?感谢

我在print_r()

收到此错误
<h1>CException</h1> <p>Property "CommentModule.mapTable" is not defined. (/Library/WebServer/Documents/dev/common/lib/yii/framework/base/CComponent.php:1‌​30)</p><pre>#0 /Library/WebServer/Documents/dev/common/lib/yii/framework/base/CModule.php(105): CComponent->__get('mapTable')

1 个答案:

答案 0 :(得分:0)

modules/comment/models/comment.php put

public function behaviors() 
{
    return array(
        'commentable'=>array(
            'class'=>'comment.behaviors.CommentableBehavior', 
        ),
    );
}

您可以通过$this->mapTable$this->commentable->mapTable

来调用它