【问题标题】:cakephp: How read Auth in a modelcakephp:如何在模型中读取 Auth
【发布时间】:2013-07-04 13:15:26
【问题描述】:

我有一个问题要这样做:

 public $hasOne = array(
    'Friend' => array(
        'className'    => 'Friend',
        'conditions'   => array('Friend.friend_id' => $this->Auth->user('id))
    )
);

我有一个错误:

致命错误 错误:语法错误,意外的 T_VARIABLE 文件:/Users/guillaumebaduel/Sites/app/Model/User.php 线路:98

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

谢谢

【问题讨论】:

  • 为什么你认为 Auth 组件(作为对象)会突然神奇地出现在模型中?除了这是无效的 php 语法这一事实 - 正如您自己所说的那样。

标签: cakephp authentication model


【解决方案1】:

在构造函数中:

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),并且您不能只动态使用未声明的对象。只是静态的,而且只是因为用户方法允许你这样做。

即使在运行时附加此类非无状态绑定会更简洁,而不是为未登录用户和其他不需要的用户这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    相关资源
    最近更新 更多