cakephp将user_id设置为当前用户

时间:2012-10-27 09:57:47

标签: cakephp-2.0

我在CakePHP 2.x

工作

目前,在我的应用中,用户可以选择以任何用户身份添加内容。我想强迫他们将自己的id作为“user_id”。 User_id是外键,我正在使用ACL,Auth。我试图使用$ this-> Data =>设置控制器中的数据。 $这 - > auth->用户('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')); ?>

cake php controller:

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

Cake Php模型:

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%确定如何或在何处执行此操作,任何帮助都会非常有用,谢谢你们。

1 个答案:

答案 0 :(得分: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']));
...