gpt4 book ai didi

cakephp-2.0 - cakephp 将 user_id 设置为当前用户

转载 作者:行者123 更新时间:2023-12-04 05:26:55 25 4
gpt4 key购买 nike

我在 CakePHP 2.x 中工作

目前在我的应用程序中,用户可以选择添加任何用户的内容。我想强制他们将自己的 id 作为“user_id”。 User_id 是外键,我正在使用 ACL、Auth。我试图通过使用 $this->Data => $this->auth->user('id); 在 Controller 中设置数据;但设置值并没有接缝。

Cakephp 添加 View :

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

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

最佳答案

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

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

关于cakephp-2.0 - cakephp 将 user_id 设置为当前用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com