【问题标题】:cakephp setting user_id to current usercakephp 将 user_id 设置为当前用户
【发布时间】:2012-10-27 09:57:47
【问题描述】:

我正在使用 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'));
}

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% 确定如何或在哪里执行此操作,任何帮助将不胜感激,谢谢大家。

【问题讨论】:

  • 您是否尝试过调试“$this->auth”的内容。如果这是您在代码中的方式,那么您最好更正“$this->Auth”
  • 不太清楚我做了什么让它继续下去,但这绝对是影响结果的一个因素。谢谢你的回答。

标签: cakephp-2.0


【解决方案1】:

这就是我的做法,我认为它可能值得发布,因为它的代码少了一点;

// 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']));
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    相关资源
    最近更新 更多