2

我正在使用 CakePHP 2.x

目前在我的应用程序中,用户可以选择以任何用户身份添加某些内容。我想强迫他们拥有自己的 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'));
}

蛋糕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% 确定如何或在哪里执行此操作,任何帮助都会非常感谢各位。

4

1 回答 1

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']));
...
于 2012-11-29T07:06:46.477 回答