2012-10-27 60 views
2

我在CakePHP的2.x的工作CakePHP的USER_ID设置为当前用户

目前在我的应用程序,用户可以选择要添加的东西任何用户。我想强制他们拥有自己的id作为“user_id”。 User_id是一个外键,我使用ACL,Auth。我试图通过使用$ this-> Data => $ this-> auth-> user('id);来设置控制器中的数据。但它并没有设定值。

CakePHP的添加视图:

<?php echo $this->Form->create('Asset'); ?> 
<fieldset> 
    <legend><?php echo __('Add Asset'); ?></legend> 
<?php 
    echo $this->Form->input('asset_name'); 
    echo $this->Form->input('description'); 
    echo $this->Form->input('vaule'); 
    echo $this->Form->input('date_bought'); 
    echo $this->Form->input('date_freehold'); 
    echo $this->Form->input('user_id'); 
?> 
</fieldset> 
<?php echo $this->Form->end(__('Submit')); ?> 

蛋糕PHP控制器:

public function add() { 
    if ($this->request->is('post')) { 
     $this->Asset->create(); 
     if ($this->Asset->save($this->request->data)) { 
         $this->data['Assets']['user_id'] = $this->Auth->user('id'); 
      $this->Session->setFlash(__('The asset has been saved')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The asset could not be saved. Please, try again.')); 
     } 
    } 
    $users = $this->Asset->User->find('list'); 
    $this->set(compact('users')); 
} 

蛋糕腓型号:

class Asset extends AppModel { 

/** 
* Validation rules 
* 
* @var array 
*/ 
public $validate = array(
    'asset_name' => array(
     'notempty' => array(
      'rule' => array('notempty'), 
      //'message' => 'Your custom message here', 
      //'allowEmpty' => false, 
      //'required' => false, 
      //'last' => false, // Stop validation after this rule 
      //'on' => 'create', // Limit validation to 'create' or 'update' operations 
     ), 
    ), 
    'date_bought' => array(
     'date' => array(
      'rule' => array('date'), 
      //'message' => 'Your custom message here', 
      //'allowEmpty' => false, 
      //'required' => false, 
      //'last' => false, // Stop validation after this rule 
      //'on' => 'create', // Limit validation to 'create' or 'update' operations 
     ), 
    ), 
    'user_id' => array(
     'numeric' => array(
      'rule' => array('numeric'), 
      //'message' => 'Your custom message here', 
      //'allowEmpty' => false, 
      //'required' => false, 
      //'last' => false, // Stop validation after this rule 
      //'on' => 'create', // Limit validation to 'create' or 'update' operations 
     ), 
    ), 
); 

//The Associations below have been created with all possible keys, those that are   not needed can be removed 

/** 
* belongsTo associations 
* 
* @var array 
*/ 
public $belongsTo = array(
    'User' => array(
     'className' => 'User', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 
} 

不是100%确定如何或在哪里做到这一点,任何帮助将非常感谢家伙非常感谢。

+0

您是否尝试过调试“$ this-> auth”中出现的内容。如果这是你在代码中的方式,那么你最好纠正“$ this-> Auth” –

+0

不太确定我做了什么来实现它,但这是影响结果的决定性因素。谢谢你的回答。 – agit

回答

3

以下是我的工作方式,认为可能值得张贴,因为它的代码少一点;

// in AppController 
function beforeFilter() { 
    // setup auth here 
    ... 
    $this->set('authUser', $this->Auth->user()); 
    ... 
} 

// in Add view's form 
... 
echo $this->Form->hidden('user_id', array('value'=>$authUser['id'])); 
... 
相关问题