cakephp:如何在模型中读取Auth

cakephp:如何在模型中读取Auth,cakephp,authentication,model,Cakephp,Authentication,Model,我有一个问题要做: public $hasOne = array( 'Friend' => array( 'className' => 'Friend', 'conditions' => array('Friend.friend_id' => $this->Auth->user('id)) ) ); 我有一个错误: 致命错误 错误:语法错误,意外的T_变量 文件:/Users/guillaumeba

我有一个问题要做:

 public $hasOne = array(
    'Friend' => array(
        'className'    => 'Friend',
        'conditions'   => array('Friend.friend_id' => $this->Auth->user('id))
    )
);
我有一个错误:

致命错误 错误:语法错误,意外的T_变量 文件:/Users/guillaumebaduel/Sites/app/Model/User.php 电话号码:98

有什么问题? 如何在模型中获取ID

感谢构造器中的

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);

    $this->hasOne = array(
        'Friend' => array(
            'className'    => 'Friend',
            'conditions'   => array('Friend.friend_id' => AuthComponent::user('id))
        )
    );
}
请注意,这里需要使用模型构造函数(基本OOP),并且不能仅动态使用未声明的对象。只是静态的,而且仅仅是因为用户方法允许您这样做


即使在运行时附加此类非无状态绑定比为未登录用户和其他不需要的用户附加此类绑定更干净。

为什么您认为Auth组件(作为对象)突然会在模型中神奇地可用?除此之外,这是无效的php语法——正如您自己所说。