In this document, it explains how to use mutators and accessors in Doctrine, but does not explain what they are.
Could anyone explain what mutators and accessors do and what they are?
Thanks in advance.
You can use mutators and accessors to implement additional behavior for your models' fields. Basically they transform the value from one form into another. For example if you look at Doctrine's docs they specify a md5Password mutator. Mutator means that Doctrine will call the specified mutator method whenever you set the value for the field. So whenever you do:
$user->password = 'foobar';
Doctrine will call the md5Password() of the model, hence transforming 'foobar' into md5('foobar'). In a nutshell this ensures that the password is always hashed on software-level.
Accessor is the opposite of mutator; it'll be called when the field is being read instead of being set (eg. when a row is read from the database).
Related
Can I extract a specific column for a model instance in Laravel instead of using query methods statically? more clearly, instead of writing the code below:
MyModel::select('columnName')->where('id', $model->id)->first();
can we write?
$model->select('columnName');
Well, when you grab it from a model class, it is a Eloquent Object, and it has all attributes from the database.
But if you are talking about serializing that into an array or JSON everytime, then you need to modify your $visible array ;
if you wanna transform it for a specific case, then you need to do :
MyModel::select('columnName')->where('id', $model->id)->first()->pluck('columnName');
I have a JSON field in a database which is populated using the array cast on an Eloquent model.
Before saving the field, Laravel sorts the elements by their key. Presumably this happens during serialisation.
Why does it do this? And is there a way to prevent it?
If you do not need to implement search by field, you can use the text type instead of the json type. Just before saving, you will need to execute json_encode, either yourself or implement a mutator.
I've never encountered this behavior before, but perhaps you can try using an accessor and a mutator to accomplish what you want. Let's say your database column is meta. You can use Laravel's special setAttribute and getAttribute methods in your model.
A mutator
For example, to json_encode data as it's about to be saved to your database it would be:
public function setMetaAttribute($value)
{
$this->attributes['meta'] = json_encode($value);
}
An accessor
To json_decode data as it's being retrieved it would be:
public function getMetaAttribute($value)
{
return json_decode($value, true);
}
The rule of thumb is take get or set and append your column name with first letters uppercased and removing any underscores, and appending Attribute to the method name. E.g. active_users column becomes getActiveUsersAttribute and so on.
Try that and see if it does any funky sorting.
Sometimes MySQL changes the order of keys when a JSON field is used.
The basic solution is to use a TEXT field to store json.
See Mysql 5.7 native json support - control keys order in json_insert function
In Laravel you can use getOriginal() on a model in order to get the original model (before changes since it was queried).
Now I need to access the relationship of that original model... is there a way to do that?
$item = OrderItem::where('id', $id)->with('qualification')->first();
$original_item = $item->getOriginal();
$original_item["qualification"] is not defined. I can access qualification_id though.
getOriginal() method returns array of the model's original attribute values, it is not a model itself, therefore you can not get a relationship.
So you can access the relationship using standard way: $item->qualification, that should not be affected by your changes of the parent model.
I would like to understand what happen when using the toArray () method in a case like this:
Ad::query()->with('nominations')->where(['id'=>$id])->get()->toArray();
Eloquent prevents the construction of the collection and the hydration of individual model objects and directly returns an array or eloquent first hydrates the collection and the model objects and then converts everything into an array (thus doing an extra operation)?
If the answer was the second, how can I get the first behavior with eloquent?
I specify with eloquent because it would be nice to be able to do this by continuing to refer in an abstract way to the entities without then mentioning specific database features in the code (for example the name of the tables).
Answering both your questions. The second one will happen. And you can't make Eloquent create an array, you can just convert an object or collection to an array.
Also, your query has a lot of redundant code, you could just do this:
Ad::with('nominations')->find($id)->toArray()
I create a model and save it, using the save() methods, and I do not fill all properties. Mysql will set the assigned default values of the columns.
But the values are not filled after, just the primary keys.
Isn't there a something like "refresh()" method which updates my model instance with the actual data?
Yes. There's a fresh method on the model that does exactly that.
You shouldn't need this. You should add the default values to the $attributes array so that you always have the default values automatically.