我想知道是否有办法添加created_by
和modified_by
类似于created
和modified
在CakePHP中的工作方式?
我喜欢这样一个事实,即蛋糕会识别这些字段并自动处理它们,无论模型如何,我根本不需要实现它们。我想使用当前用户ID添加一个类似的功能(在我的应用程序中,总有一个用户ID,即使它有时可能为0)。
我假设app_model中的起始位置为before_save()
-
另外,有没有办法让我自己将蛋糕识别为用户表的外键(类似于它识别user_id
的方式),或者我必须添加has / belongs手动关系?我问,因为这将在我的大多数模型上进行,所以我更愿意减少冗余代码。
谢谢!
答案 0 :(得分:6)
对于问题的第一部分,我使用此行为代码来完成您正在寻找的内容:
class UserLinkBehavior extends ModelBehavior
{
/**
* The string to use to retrieve the user id from CakeSession
*
* @var string
*/
var $user_id_key = 'Auth.User.id';
function setup(&$model, $settings)
{
if(isset($settings['user_id_key']))
{
$this->user_id_key = $settings['user_id_key'];
}
}
function beforeSave(&$model)
{
App::uses('CakeSession', 'Model/Datasource');
$logged_user_id = CakeSession::read($this->user_id_key);
if(isset($logged_user_id))
{
$this->set_user_on_current_model($model, $logged_user_id);
}
return true;
}
/**
* Set the created_by and modified_by user id on the current model
*
* @param Model $model
* @param int $logged_user_id
* @return void
*/
private function set_user_on_current_model(&$model, $logged_user_id)
{
if(isset($logged_user_id))
{
/*
* Id is not set -> it is a creation
*/
if($model->hasField('created_by') && (!isset($model->data[$model->alias]['id']) || empty($model->data[$model->alias]['id'])))
{
if(!isset($model->data[$model->alias]['created_by']))
{
$model->data[$model->alias]['created_by'] = $logged_user_id;
/*
* If the save is called with a whitelist, add 'created_by' to the whitelist
* in order to have this field saved as well
*/
if(!empty($model->whitelist) && !in_array('created_by', $model->whitelist))
{
$model->whitelist[] = 'created_by';
}
}
}
/*
* Id is set -> it is an update
*/
if($model->hasField('modified_by') && isset($model->data[$model->alias]['id']) && !empty($model->data[$model->alias]['id']))
{
$model->data[$model->alias]['modified_by'] = $logged_user_id;
/*
* If the save is called with a whitelist, add 'modified_by' to the whitelist
* in order to have this field saved as well
*/
if(!empty($model->whitelist) && !in_array('modified_by', $model->whitelist))
{
$model->whitelist[] = 'modified_by';
}
}
}
}
}
然后在模型或AppModel中声明它
var $actsAs = array('UserLink');
对于问题的第二部分,您可以添加对行为的beforeFind()回调,并使用model->bindModel()函数链接具有created_by
和modified_by
字段的模型使用用户模型。 Personaly我喜欢在需要时手动在每个模型中声明这些链接。
答案 1 :(得分:0)
它不能像创建和修改的字段一样,但您可以在控制器方法中的任何位置添加此字段。
例如。在add方法中,您可以添加如下内容。
$this->request->data['ModelName']['created_by'] = $this->Auth->user['userid'];
答案 2 :(得分:0)
我发现this one并且对我有用,易于实施和理解
<?php
App::uses('Model', 'Model');
class AppModel extends Model {
//get current logged-in user
public function getCurrentUser() {
App::uses('CakeSession', 'Model/Datasource');
$Session = new CakeSession();
$user = $Session->read('Auth.User');
return $user['id'];
}
//populate created_by and modified_by
public function beforeSave($options = array()) {
parent::beforeSave($options);
//find all fields from table created_by/modified_by exists
$fields = array_keys($this->getColumnTypes());
//get modal name to feed in data in appropriate array key
$modal = array_keys($this->data);
$modal = $modal[0];
//add created_by value
if(in_array('created_by', $fields) && !isset($this->data[$modal]['id'])){
//correct this line
$this->data[$modal]['created_by'] = $this->getCurrentUser()==null?-1:$this->getCurrentUser();
return true;
}elseif(in_array('modified_by', $fields)){
$this->data[$modal]['modified_by'] = $this->getCurrentUser()==null?-1:$this->getCurrentUser();
return true;
}
return true;
}
}