I have a BLOB field in my database which contains compressed data.
I need compress / uncompress to be transparent, and user class do not need to write:
$objModel->field = gzencode($objModel->field);
$objModel->field = gzdecode($objModel->field);
For saving I got it, overriding save method:
public function save($attributes[] = null) {
$this->field = gzencode($objModel->field);
return parent::save($attributes);
}
But when I recover data from the database I do not get to gzdecode "transparent", I have tried overriding boot, __call, __callstatic and others, but unsuccessfully.
Can someone tell me which method recovers data from DB and fills the model object so I can override it and make gzdecode?
I wouldn't recommend you override Eloquent methods. Just use accessor:
public function getFieldAttribute($value)
{
return gzdecode($value);
}
And mutator:
public function setFieldAttribute($value)
{
$this->attributes['field'] = gzencode($value);
}
Related
I'm sure there is a common pattern for this kind of thing, and I'm struggling with search terms to find answers, so please bear with me if is this a dupe.
I have a few Classes in my app that create pretty standard Models that are stored in a relational database, eg;
// AtsType::name examples = 'XML', 'RSS', 'SOAP'
class AtsType extends Model
{
public function ats_instances()
{
return $this->hasMany('App\AtsInstance');
}
public function import()
{
}
}
What I need that import() method to do, however, somehow invokes a class/interface/contract/whatever based upon the actual model instance. So something like this;
AtsTypeRss::import()
AtsTypeXml::import()
AtsTypeSoap::import()
I'd like them to be standalone classes, in order to eventually use some artisan commands that will generate them for a developer, along with a data migration to create the new model names into the database.
I'm just unsure how to go about this.
You could try something like (as seen here), I've searched how to use variable in namespace :
class AtsType extends Model
{
protected $import_method = 'MyMethod';
public function ats_instances()
{
return $this->hasMany('App\AtsInstance');
}
public function import()
{
$string = $this->import_method;
$class = '\\controller\\' . $string;
$newObject = new $class();
}
}
I am using Jenssegers Optimus package to obfuscate my URLS.
Currently, I am calling it in every single controller that deals with get requests. Problem is, I need to constantly encode and decode my IDs in almost all methods in my controllers.
E.g.:
use Jenssegers\Optimus\Optimus;
class ResponseController extends Controller
{
protected $optimus;
public function __construct(Optimus $opt)
{
$this->optimus = $opt;
}
public function index()
{
$labels = Label->get();
foreach ($labels as $key => $label){
$label->id = $this->optimus->encode($label->id);
$labels[$key] = $label;
}
return view('responses/index', compact('labels'));
}
public function show($id)
{
$id = $this->optimus->decode($id);
$label = Label::get($id);
}
}
I thought of creating Accessors & Mutators to always encrypt the IDs of the models I need to obfuscate in the URL. So I'd put them in a trait to reuse the code. I tried:
use Jenssegers\Optimus\Optimus;
trait EncodeId{
public function getIdAttribute($value, Optimus $optimus)
{
return $optimus->encode($value);
}
}
Then I added this trait to my model. However, Laravel would throw an error complaining about Optimus $optimus in the method definition. It said $optimus was expected to be a type of Jenssegers\Optimus\Optimus even though I am declaring it. That works for controllers just fine, but it doesn't work for models apparently. Or I shouldn't try to use a trait in this case.
Here's the actual error:
FatalThrowableError in EncodeId.php line 10:
Type error: Argument 2 passed to App\Label::getIdAttribute() must be an instance of Jenssegers\Optimus\Optimus, none given, called in /home/../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 2734
It would be really nice if I could use Optimus obfuscation on the model level rather than calling its encode and decode functions multiple times in my controllers.
There's another package called FakeID that is meant to do that. I tried to implement it in my project but it didn't work. I am pretty sure I could handle it myself since it seems a simple task.
Get mutators (AKA accessors) are called along with a single argument. That's why you are getting expected to be a type of Jenssegers\Optimus\Optimus error as Jenssegers\Optimus\Optimus is not injected by framework when calling acessors or mutators (like controllers does).
Just read this snippet from source code (line 2632):
public function getAttributeValue($key)
{
$value = $this->getAttributeFromArray($key);
// If the attribute has a get mutator, we will call that then return what
// it returns as the value, which is useful for transforming values on
// retrieval from the model to a form that is more useful for usage.
if ($this->hasGetMutator($key)) {
return $this->mutateAttribute($key, $value);
}
//...
}
And now the call to $this->mutateAttribute($key, $value); (line 2736)
protected function mutateAttribute($key, $value)
{
return $this->{'get'.Str::studly($key).'Attribute'}($value);
}
Did you understand now?
Acessor/get mutator are called along with just one argument: $value.
Solution
You could to try something like this:
public function getIdAttribute($value)
{
return app(Optimus::class)->encode($value);
}
Invoking Optimus instance from container (app()) would do the trick.
So here's the code
use App\Video;
class HomeController extends Controller
{
protected $video;
public function index()
{
// $video_to_watch is fetched from db and I want to save it and use it in
// another function in this controller
$this -> video = $video_to_watch;
return view('home', compact('video_to_watch'));
}
public function feedback(Request $request)
{
dd($this -> video);
}
}
feedback returns null for some reason.
when I put the
dd($this -> video);
in index() it works fine, not null.
I have tried what's suggested here: Laravel doesn't remember class variables
but it didn't help.
I'm sure it's something stupid I'm overlooking. But can't seem to figure out what, any help much appreciated.
You can't keep your $video value between 2 different requests. You have to fetch your video data in each request.
use App\Video;
class HomeController extends Controller
{
public function index() {
$myVideo = $this->getMyVideo();
return view('home', $myVideo);
}
public function feedback(Request $request) {
dd($this->getMyVideo);
}
private function getMyVideo() {
// fetch $video_to_watch from db
return $video_to_watch ;
}
}
First of all don't fetch data inside a Controller. It's only 'a glue' between model and view. Repeat. No fetching inside a controller.
Use domain services and dependency injection to get business data and if you want to share this data create shared service (single instance).
-
Putting a data object into a controller property class makes a temporary dependency between method calls. Avoid it. Use services instead.
I have a custom setter that I'm running in a __construct method on my model.
This is the property I'm wanting to set.
protected $directory;
My Constructor
public function __construct()
{
$this->directory = $this->setDirectory();
}
The setter:
public function setDirectory()
{
if(!is_null($this->student_id)){
return $this->student_id;
}else{
return 'applicant_' . $this->applicant_id;
}
}
My problem is that inside my setter the, $this->student_id (which is an attribute of the model being pulled from the database) is returning null.
When I dd($this) from inside my setter, I notice that my #attributes:[] is an empty array. So, a model's attributes aren't set until after __construct() is fired. How can I set my $directory attribute in my construct method?
You need to change your constructor to:
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
$this->directory = $this->setDirectory();
}
The first line (parent::__construct()) will run the Eloquent Model's own construct method before your code runs, which will set up all the attributes for you. Also the change to the constructor's method signature is to continue supporting the usage that Laravel expects: $model = new Post(['id' => 5, 'title' => 'My Post']);
The rule of thumb really is to always remember, when extending a class, to check that you're not overriding an existing method so that it no longer runs (this is especially important with the magic __construct, __get, etc. methods). You can check the source of the original file to see if it includes the method you're defining.
I wouldn't ever use a constructor in eloquent. Eloquent has ways to accomplished what you want. I would used a boot method with an event listener. It would look something like this.
protected static function boot()
{
parent::boot();
static::retrieved(function($model){
$model->directory = $model->student_id ?? 'applicant_' . $model->applicant_id;
});
}
Here are all the model events you can use: retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, trashed, forceDeleted, restoring, restored, and replicating.
I am using the following code to initialize a model from within my controller:
$this->load->model('model_name');
Is it possible to modify the above line somehow so that the model constructor recieves a parameter? I want to use the following code in the model constructor:
function __construct($param_var) {
parent::Model();
$this->$param_var = $param_var; //I'm not even sure this works in PHP..but different issue
}
This would be very helpful so that I can reuse my model classes. Thanks.
UPDATE:
(from one of the answers, my original question is solved..thanks!)
Just to explain why I wanted to do this: the idea is to be able to reuse a model class. So basically to give a simple example I would like to be able to pass an "order_by" variable to the model class so that I can reuse the logic in the model class (and dynamically change the order-by value in the sql) without having to create a separate class or a separate function.
Is this poor design? If so could you please explain why you wouldn't do something like this and how you would do it instead?
You can't pass parameters through the load function. You'll have to do something like:
$this->load->model('model_name');
$this->model_name->my_constructor('stuff');
In the model:
function my_constructor($param_var) {
...
}
Response to update:
You could just pass the order_by value when you're calling your model function. I'm assuming in your controller action, you have something like $this->model_name->get($my_id); Just add your order_by parameter to this function. IMO this makes your model logic more flexible/reusable because the way you were doing it, I assume setting order_by in the constructor will set the order_by value for every function.
In model
<?php
/* Load Model core model */
/* BASEPATH = D:\xampp\htdocs\ci_name_project\system\ */
include BASEPATH . 'core\\Model.php';
class User_model extends CI_Model {
/* Properties */
private $name;
/* Constructor parameter overload */
public function __construct($name) {
$this->set_name($name);
}
/* Set */
public function set_name($name) {
$this->name = $name;
}
/* Get */
public function get_name() {
return $this->name;
}
}
in controller
<?php
class User_controller extends CI_Controller {
public function index() {
/* Load User_model model */
/* APPPATH = D:\xampp\htdocs\ci_name_project\application\ */
include APPPATH . 'models\\User_model.php';
$name = 'love';
/* Create $object_user object of User_model class */
$object_user = new User_model($name);
echo $object_user->get_name(); // love
}
}
I see your reasoning for this, but may I suggest looking at Object-Relational Mapping for your database needs. There is a user-made ORM library for CodeIgniter called DataMapper that I've been using lately. You can use tables in your controllers as objects, and it may be a better fit for your problem.
Instead of using DataMapper i suggested to use IgnitedRecord because that the DataMapper is no longer maintained more over it has been replaced into Ruby
I am using CI ver 3.X, so what I am about to say is it will work for Codeigniter 3.X (and I haven't checked ver 4+ yet).
When I went thru the source code of the function model() in file system/libraries/Loader.php, noticed that it does not support loading the model with construct parameters. So if you want to make this happen you have to change the source code (bold, I know, and I just did).
Down below is how I did it.
1. Firstly, replace line 355
$CI->$name = new $model();
with some modifications:
$_args_count = func_num_args();
if(3 < $_args_count){
$refl = new ReflectionClass($model);
$CI->$name = $refl->newInstanceArgs(array_slice($_args_count, 3));
}else{
$CI->$name = new $model(); // origin source code
}
2. Load the model with a bit difference:
$this->load->model("model_name", "model_name", false, $param_var); // where amazing happens
Now you can have $this->model_name as you wished.