在行为类中获取变量值


getting a variable value in a Behavior Class

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

这是我的modules/comment/behaviors/CommentableBehavior.php 中的CommentableBehavior

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));
 }

我想从我的CommentableBehavior类中获得$this->mapTable$this->mapCommentColumn。有什么想法吗?感谢

我在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')

modules/comment/models/comment.php中放入

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

您称之为$this->mapTable$this->commentable->mapTable