Is there a way to add created_by and modified_by, similar to how created and modified works in cakePHP?

I'm wondering, is there a way to add created_byand modified_by, similar to the way createdand modifiedwork in CakePHP?

I like the fact that the cake recognizes these fields and takes care of them automatically, regardless of the model, without me even realizing them. I would like to add a similar function using the current user ID (there is always a user ID in my application, even if it can sometimes be 0).

I assume the starting place is before_save()in app_model?

-

Also, is there a way to get the cake to automatically recognize this as a foreign key for the user table (similar to how it recognizes user_id), or will I need to add the has / to to relationship property manually? I ask because this will continue on most of my models, so I would prefer to reduce redundant code.

Thank!

+5
source share
3 answers

In the first part of your question, I use this behavior code to accomplish exactly what you are looking for:

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

Then just declare it in your model or your AppModel

var $actsAs = array('UserLink');

, , beforeFind() model- > bindModel(), created_by modified_by . , .

+6

, , .

. , .

 $this->request->data['ModelName']['created_by'] = $this->Auth->user['userid'];
0

,

<?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;
}
}
0

All Articles