我想知道是否可以创建在模型中的hasMany关系,这使得使用在用户所记录的ID的。

实施例: 一个末端具有许多分组表决(来自不同的用户)。这是容易的:

public $hasMany = array(
   'TipVoting' => array(
        'className' => 'TipVoting',
        'foreignKey' => 'tip_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ));

现在我想所有的技巧,但想知道是否已登录的用户事呢已经投这个提示。所以,我想创建一个Realtionship它看起来像这样:

public $hasMany = array(
   'MyTipVoting' => array(
        'className' => 'TipVoting',
        'foreignKey' => 'tip_id',
        'dependent' => false,
        'conditions' => array('TipVoting.user_id' => $loggedinUser_id),
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

我如何获得$ loggedinUser_id这个模型?或者,这是实现这一问题的好方法?我宁愿去一个单独的找上了TipVoting模型?

任何想法?

谢谢!

有帮助吗?

解决方案

使用中可容纳的行为。 http://book.cakephp.org/view/474/Containable

您不想定义不同的关系,你只是想通过相关标准,能够过滤。中容纳将允许你这样做。

通过只是TipVoting关系,在你的模型做到这一点:

var $actsAs = array( 'Containable' );

在你的控制器动作做到这一点:

$this->TipVoting->find( 'all', array( 
 'contain' => array( 
   'Vote' => array( 
    'conditions' => array( 
      'TipVoting.user_id' => $this->Auth->User('id') ), 
    'order' => 'TipVoting.id DESC' ) ) );

其他提示

如果我明白这个问题,你真正是后一种方式来定义条件的一次的,而不是一种方法来定义一个加入特定的条件。该Containable行为将处理后者,而不是前者。为此...

我不知道的任何方式在你的加入定义中使用的变量值。由于这些都是类变量,我怀疑它不能做(声明:我没有尝试过)。此外,还可以尝试访问的增加的复杂性是什么通常在模型层会话值(授权用户) - 一个MVC的严格意义上的东西没有没有

我所做的,以满足类似的需求是一个事物的组合如下:

我用我的AppController向前认证的用户信息:

# AppController
# You may or may not want to use the conditional
if( !empty( $this->data ) ) {
  $this->data['User'] = $this->Auth->user();
}

在模型中,创建一个beforeFind()回调将修改查询结构:

# Your model, e.g. TipVoting
# The data[] array will probably be keyed differently
public function beforeFind( $query ) {
  if( array_key_exists( 'active', $this->_schema ) ) {
    $conditions = !empty( $query['conditions'] ) ? $query['conditions'] : array();

    if( !is_array( $conditions ) ) {
      $conditions = array( $conditions );
    }

    if( !isset ( $conditions['user_id'] ) && !isset( $conditions[$this->alias . '.user_id'] ) ) {
      $conditions[$this->alias . '.user_id'] = $this->data['User']['Administrator'];
    }

    $query['conditions'] = $conditions;
  }

  return $query;
}

此代码仅仅是满足需要的类似的给你,因此它可能不会直接工作的摘要,但它应该给你什么是可能的想法,我认为它甚至可能是相当接近什么你后。要知道,你可能要在这里做一些修改,并在那里。

关键是,你通过转发会话信息模型明确地(而不是要求模型来直接访问它)保留了“正确”的MVC结构,同时设置查询条件从使用会话值每次纪录该模型被检索,除非用户为中心的条件已经参与。

这不是对你以后一个完美的结合,但它不应该太长时间将它量身定制您的需求。

尝试使用bindModel从appControllers beforeFilter。

...
public function beforeFilter( ){
    parent::beforeFilter( );
    if( $this->Auth->user( )){
        $this->User->bindModel(
            'hasMany' => array(
                'MyTipVoting' => array(
                    'className' => 'TipVoting',
                    'foreignKey' => 'tip_id',
                    'conditions' => array(
                        'MyTipVoting.user_id' => $this->Auth->user( 'id' ),
                        //note the use of the aliased name here, not TipVoting but MyTipVoting
                        ...
                    ),
                    ...
                ),
                ...
            ),
        );
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top