-
mlurie Member
  
-
Posts: 58
Threads: 16
Joined: Jul 2020
Reputation:
2
(08-19-2020, 04:22 AM)InsiteFX Wrote: If you take a look at the Model Insert method you can see how CodeIgniter is doing this.
Take a look ad the if statements.
InsiteFX, I followed your advice. Based on what I learned from looking at the CodeIgniter Model, I was able to come up with this:
PHP Code: /** * This method saves the session "user_id" value to "created_by" and "updated_by" array * elements before the row is inserted into the database. * */ protected function insertUserstamp(array $data) { $user_id = session()->get('user_id'); if (!empty($user_id) && !array_key_exists('created_by', $data) && !array_key_exists('updated_by', $data)) { $data['data']['created_by'] = $user_id; $data['data']['updated_by'] = $user_id; } return $data; }
/** * This method saves the session "user_id" value to "updated_by" array element before * the row is inserted into the database. * */ protected function updateUserstamp(array $data) { $user_id = session()->get('user_id'); if (!empty($user_id) && !array_key_exists('updated_by', $data)) { $data['data']['updated_by'] = $user_id; } return $data; }
I then included them in the $beforeInsert and $beforeUpdate arrays. Everything seems to be working correctly.
I do have one followup question. What is the best way to make these methods available to every model in App\Models? Should I create a new "CustomModel" that extends Model with these methods, and make all models located in App\Models children of the new custom model?
-
mlurie Member
  
-
Posts: 58
Threads: 16
Joined: Jul 2020
Reputation:
2
(08-27-2020, 08:41 AM)mlurie Wrote: (08-19-2020, 04:22 AM)InsiteFX Wrote: If you take a look at the Model Insert method you can see how CodeIgniter is doing this.
Take a look ad the if statements.
InsiteFX, I followed your advice. Based on what I learned from looking at the CodeIgniter Model, I was able to come up with this:
PHP Code: /** * This method saves the session "user_id" value to "created_by" and "updated_by" array * elements before the row is inserted into the database. * */ protected function insertUserstamp(array $data) { $user_id = session()->get('user_id'); if (!empty($user_id) && !array_key_exists('created_by', $data) && !array_key_exists('updated_by', $data)) { $data['data']['created_by'] = $user_id; $data['data']['updated_by'] = $user_id; } return $data; }
/** * This method saves the session "user_id" value to "updated_by" array element before * the row is inserted into the database. * */ protected function updateUserstamp(array $data) { $user_id = session()->get('user_id'); if (!empty($user_id) && !array_key_exists('updated_by', $data)) { $data['data']['updated_by'] = $user_id; } return $data; }
I then included them in the $beforeInsert and $beforeUpdate arrays. Everything seems to be working correctly.
I do have one followup question. What is the best way to make these methods available to every model in App\Models? Should I create a new "CustomModel" that extends Model with these methods, and make all models located in App\Models children of the new custom model?
Creating a new "CustomModel" that extends Model with these methods, and making all models located in App\Models children of the new custom model accomplished my goal.
-
Matleyx Junior Member
 
-
Posts: 36
Threads: 4
Joined: Jun 2019
Reputation:
0
12-19-2020, 02:35 AM
(This post was last modified: 12-19-2020, 02:36 AM by Matleyx.)
Hi Mlurie.
I'm interest for this functionality.
Can you explain me how you add this?
I understand that you created a new model, but if you tell me all the steps, i'll thank you a lot.
-
InsiteFX Super Moderator
     
-
Posts: 6,758
Threads: 346
Joined: Oct 2014
Reputation:
247
12-19-2020, 01:49 PM
(This post was last modified: 12-19-2020, 01:50 PM by InsiteFX.)
Just create a new model that extends CodeIgniter's Model.
PHP Code: ?php
namespace App\Models;
use CodeIgniter\Model;
class YourModelName extends Model { /** * Class properties go here. * ------------------------------------------------------------------- * * public, private, protected, static and const. */
/** * Name of database table * * @var string */ protected $table = '';
/** * The format that the results should be returned as. * Will be overridden if the as* methods are used. * * @var string */ protected $returnType = 'array';
/** * An array of field names that are allowed * to be set by the user in inserts/updates. * * @var array */ protected $allowedFields = [];
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() { parent::__construct(); }
// -----------------------------------------------------------------------
} // End of YourModelName Model Class.
/** * ----------------------------------------------------------------------- * Filename: YourModelName.php * Location: ./app/Models/YourModelName.php * ----------------------------------------------------------------------- */
Just add his code from above to this Model that you should rename to your Model's name.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
Matleyx Junior Member
 
-
Posts: 36
Threads: 4
Joined: Jun 2019
Reputation:
0
ok, ho creato questo CustomModel.php:
PHP Code: <?php
namespace App\Models;
use CodeIgniter\Model;
class CustomModel extends Model { /** * Class properties go here. * ------------------------------------------------------------------- * * public, private, protected, static and const. */
/** * Name of database table * * @var string */ protected $table = '';
/** * The format that the results should be returned as. * Will be overridden if the as* methods are used. * * @var string */ protected $returnType = 'array';
/** * An array of field names that are allowed * to be set by the user in inserts/updates. * * @var array */ protected $allowedFields = [];
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() { parent::__construct(); }
// ----------------------------------------------------------------------- /** * This method saves the session "user_id" value to "created_by" and "updated_by" array * elements before the row is inserted into the database. * */ protected function insertUserstamp(array $data) { $user_id = session()->get('user_id'); if (!empty($user_id) && !array_key_exists('created_by', $data) && !array_key_exists('updated_by', $data)) { $data['data']['created_by'] = $user_id; $data['data']['updated_by'] = $user_id; } return $data; }
/** * This method saves the session "user_id" value to "updated_by" array element before * the row is inserted into the database. * */ protected function updateUserstamp(array $data) { $user_id = session()->get('user_id'); if (!empty($user_id) && !array_key_exists('updated_by', $data)) { $data['data']['updated_by'] = $user_id; } return $data; } } // End of YourModelName Model Class.
/** * ----------------------------------------------------------------------- * Filename: YourModelName.php * Location: ./app/Models/YourModelName.php * ----------------------------------------------------------------------- */
But do I also have to modify the standard model to run these functions?
Otherwise how do I call them in insert and updates?
-
InsiteFX Super Moderator
     
-
Posts: 6,758
Threads: 346
Joined: Oct 2014
Reputation:
247
You include the method names that you created into the $beforeInsert and $beforeUpdate arrays.
PHP Code: /** * Callbacks for beforeInsert * * @var array */ protected $beforeInsert = [ 'insertUserstamp', ];
/** * Callbacks for afterUpdate * * @var array */ protected $afterUpdate = [ 'updateUserstamp', ];
What this is doing is using your new methods as callback methods.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|