http://laravel.com/docs/4.2/eloquent#dynamic-properties
class Phone extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
$phone = Phone::find(1);
Now, if I then do something like this:
echo $phone->user->email;
echo $phone->user->name;
echo $phone->user->nickname;
Will Eloquent make a database call for every time I use the ->user dynamic property? Or is this smart enough to cache the user on the first call?
In your example, the user attribute on the $phone object will be lazy loaded, but it will only be loaded once.
Keep in mind, as well, that once the object is loaded, it does not reflect any changes to the underlying table unless you manually reload the relationship using the load method.
The following code illustrates the example:
$phone = Phone::find(1);
// first use of user attribute triggers lazy load
echo $phone->user->email;
// get that user outta here.
User::destroy($phone->user->id);
// echoes the name just fine, even though the record doesn't exist anymore
echo $phone->user->name;
// manually reload the relationship
$phone->load('user');
// now will show null, since the user was deleted and the relationship was reloaded
var_export($phone->user);
Related
I would like to be able to override a variable on the model, so that a normal field is instead replaced by a relationship's field, i.e.
Where product.image might normally be a field, I want to run a function which will go through all of the resulting products from a query and replace the image field with something like the following --
(Product.php) Model
...
public function variantImages(){
return $this->image = $this->variants()->first()->pluck('image_url');
}
...
So the default product image field is replaced by the "first product variant's image". I don't want to do this in a collection once I have already got the data, the problem here is being able to do this at a Model level.
Is there a way to do this within a scope?
You can create an accessor instead of a normal function:
// Singular because it only gets one
public function getVariantImageAttribute(){
return $this->image = $this->variants()->first()->pluck('image_url');
}
This will make it available under $product->variant_image
Then you can ensure it is always appended to your model (if you want) by adding it to the appends e.g.:
$appends = [ 'variant_image' ];
Since this is not the best idea since it will force load the relationship every time you get a product (even if you didn't request it) you can conditionally control when to append it via e.g.:
return response()->json($product->append('variant_image'));
Note that the append method also works for collecitions of eloquent models.
We have a COMMON database and then tenant databases for each organization that uses our application. We have base values in the COMMON database for some tables e.g.
COMMON.widgets. Then in the tenant databases, IF a table called modified_widgets exists and has values, they are merged with the COMMON.widgets table.
Right now we are doing this in controllers along the lines of:
public function index(Request $request)
{
$widgets = Widget::where('active', '1')->orderBy('name')->get();
if(Schema::connection('tenant')->hasTable('modified_widgets')) {
$modified = ModifiedWidget::where('active', '1')->get();
$merged = $widgets->merge($modified);
$merged = array_values(array_sort($merged, function ($value) {
return $value['name'];
}));
return $merged;
}
return $countries;
}
As you can see, we have model for each table and this works OK. We get the expected results for GET requests like this from controllers, but we'd like to merge at the Laravel MODEL level if possible. That way id's are linked to the correct tables and such when populating forms with these values. The merge means the same id can exist in BOTH tables. We ALWAYS want to act on the merged data if any exists. So it seems like model level is the place for this, but we'll try any suggestions that help meet the need. Hope that all makes sense.
Can anyone help with this or does anyone have any ideas to try? We've played with overriding model constructors and such, but haven't quite been able to figure this out yet. Any thoughts are appreciated and TIA!
If you put this functionality in Widget model you will get 2x times of queries. You need to think about Widget as an instance, what I am trying to say is that current approach does 2 queries minimum and +1 if tenant has modified_widgets table. Now imagine you do this inside a model, each Widget instance will pull in, in a best case scenario its equivalent from different database, so for bunch of Widgets you will do 1 (->all())+n (n = number of ModifiedWidgets) queries - because each Widget instance will pull its own mirror if it exists, no eager load is possible.
You can improve your code with following:
$widgets = Widget::where('active', '1')->orderBy('name')->get();
if(Schema::connection('tenant')->hasTable('modified_widgets')) {
$modified = ModifiedWidget::where('active', '1')->whereIn('id', $widgets->pluck('id'))->get(); // remove whereIn if thats not the case
return $widgets->merge($modified)->unique()->sortBy('name');
}
return $widgets;
OK, here is what we came up with.
We now use a single model and the table names MUST be the same in both databases (setTable does not seem to work even though in exists in the Database/Eloquent/Model base source code - that may be why it's not documented). Anyway = just use a regular model and make sure the tables are identical (or at least the fields you are using are):
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Widget extends Model
{
}
Then we have a generic 'merge controller' where the model and optional sort are passed in the request (we hard coded the 'where' and key here, but they could be made dynamic too). NOTE THIS WILL NOT WORK WITH STATIC METHODS THAT CREATE NEW INSTANCES such as $model::all() so you need to use $model->get() in that case:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class MergeController extends Controller
{
public function index(Request $request)
{
//TODO: add some validations to ensure model is provided
$model = app("App\\Models\\{$request['model']}");
$sort = $request['sort'] ? $request['sort'] : 'id';
$src_collection = $model->where('active', '1')->orderBy('name')->get();
// we setup the tenants connection elsewhere, but use it here
if(Schema::connection('tenant')->hasTable($model->getTable())) {
$model->setConnection('tenant');
$tenant_collection = $model->get()->where('active', '1');
$src_collection = $src_collection->keyBy('id')->merge($tenant_collection->keyBy('id'))->sortBy('name');
}
return $src_collection;
}
}
If you dd($src_collection); before returning it it, you will see the connection is correct for each row (depending on data in the tables). If you update a row:
$test = $src_collection->find(2); // this is a row from the tenant db in our data
$test->name = 'Test';
$test->save();
$test2 = $src_collection->find(1); // this is a row from the tenant db in our data
$test2->name = 'Test2'; // this is a row from the COMMON db in our data
$test2->save();
dd($src_collection);
You will see the correct data is updated no matter which table the row(s) came from.
This results in each tenant being able to optionally override and/or add to base table data without effecting the base table data itself or other tenants while minimizing data duplication thus easing maintenance (obviously the table data and population is managed elsewhere just like any other table). If the tenant has no overrides then the base table data is returned. The merge and custom collection stuff have minimal documentation, so this took some time to figure out. Hope this helps someone else some day!
We are trying to detect the changes in Laravel related models at attribute level, as we have to keep audit trail of all the changes which are made via the application.
We can track the changes via isDirty method on the Eloquent model for single model that is not related to any other model, but there is no way that we can track the changes on the related eloquent models. isDirty doesn't work on related models attributes. Can some one please help us on this?
Update to original question:
Actually we are trying to track changes on the pivot table that has extra attributes as well defined on it. IsDirty method doesn't work on those extra attributes which are defined in the pivot table.
Thanks
As much I understand your question, It's can achieve through Model Event and some sort of extra code with current and relation model.
Laravel Model Events
If you dont want to use any additional stuff, you can just use the Laravel Model Events (that in fact Ardent is wrapping in the hooks). Look into the docs http://laravel.com/docs/5.1/eloquent#events
Eloquent models fire several events, allowing you to hook into various
points in the model's lifecycle using the following methods: creating,
created, updating, updated, saving, saved, deleting, deleted,
restoring, restored.
Whenever a new item is saved for the first time, the creating and
created events will fire. If an item is not new and the save method is
called, the updating / updated events will fire. In both cases, the
saving / saved events will fire.
If false is returned from the creating, updating, saving, or deleting
events, the action will be cancelled:
Finally, reffering to your question you can utilize the above approaches in numerous ways but most obviously you can combine it (or not) with the Eloquent Models' getDirty() api docs here method and getRelation() api docs here method
It will work for example with the saving event.
Model::saving(function($model){
foreach($model->getDirty() as $attribute => $value){
$original= $model->getOriginal($attribute);
echo "Changed";
}
$relations = $model->getRelations();
foreach($relations as $relation){
$relation_model = getRelation($relation);
foreach($relation_model->getDirty() as $attribute => $value){
$original= $relation_model->getOriginal($attribute);
echo "Relation Changed";
}
}
return true; //if false the model wont save!
});
Another Thought might help you. when you saving
save() will check if something in the model has changed. If it hasn't it won't run a db query.
Here's the relevant part of code in Illuminate\Database\Eloquent\Model#performUpdate:
protected function performUpdate(Builder $query, array $options = [])
{
$dirty = $this->getDirty();
if (count($dirty) > 0)
{
// runs update query
}
return true;
}
The getDirty() method simply compares the current attributes with a copy saved in original when the model is created. This is done in the syncOriginal() method:
public function __construct(array $attributes = array())
{
$this->bootIfNotBooted();
$this->syncOriginal();
$this->fill($attributes);
}
public function syncOriginal()
{
$this->original = $this->attributes;
return $this;
}
check model is dirty isDirty():
if($user->isDirty()){
// changes have been made
}
Or check certain attribute:
if($user->isDirty('price')){
// price has changed
}
I did not check this code but hopeful to use as your answer by thoughts, if you have any confusion to deal such requirement or something need to optimize or change please let me know.
I've got these two models (changed the names for this example because they aren't in english):
Task:
public function times() {
return $this->hasMany('TaskTime', 'id');
}
TaskTime:
public function task() {
return $this->belongsTo('Task', 'task_id');
}
Also, inside the model Task, I've got this method:
public function start() {
$now = Carbon\Carbon::now();
$time = new TaskTime;
$time->task()->associate($this);
$time->beginning = $now->toDateTimeString();
$time->save();
// testing
echo $this->times()->get()->toJson();
echo '<br/><br/>';
echo $this->toJson();
die();
}
When I call the start() method, it correctly saves a new row in the TaskTime's corresponding table, with the foreign key correctly set to the Task.
The line echo $this->tempos()->get()->toJson(); correctly prints the rows, including the new one.
The line echo $this->toJson(); doesn't print the new row! Only prints the old ones.
I've tried save() and push() in both $this and $time and it still doesn't print the updated data!
Any idea of what can be causing this? I've been trying to debug this thing since yesterday and I ran out of ideas...
The problem is, that Eloquent does not update relation on the already loaded models after attaching, saving, associating etc.
It creates the relation, ie. inserts/updates necessary tables (attach, save, saveMany) or sets the relation on the model without saving anything in db (associate).
So in your case $this has no idea that newly created $tempo has been associated to it.
Now,
`$this->tempos()->get()->toJson();`
runs a new query to fetch related tempos, this is why you get correct result, but
`$this->tempos;
must have been loaded before associating new one, so they won't be reloaded from the db, thus you get 'old' result.
What you need is this:
public function start() {
// do what you need with $time
$tempo->task()->associate($this);
$tempo->save();
$this->load('tempos'); // reloads relation from db
// or:
$this->tempos->add($tempo); // manually add newly created model to the collection
}
Mind thought, that latter solution will cause unexpected result if $this->tempos have not been already loaded.
i'm newbie in MVC (using codeIgniter as my example) and i have read MVC fat model and skinny controller for like 3 times, what i got :
model does the hardwork while controller calls the model and passes the data to be rendered by view
but i have one confusion , example i have an admin page that would delete product data in the db, i would have this codes (using codeIgniter):
public function deleteProduct($id = '')
{
if( is_digit($id))
{
$this->load->model('productModel');
$this->productModel->deleteById($id);
//oops product has images in another DB table and in server, so i need to delete it
$success = $this->_deleteProductImages($id);
}
else
{
//redirect because of invalid param
}
//if success TRUE then load the view and display success
//else load the view and display error
}
protected function _deleteProductImages($productId)
{
$this->load->model('productModel');
//return array of images path
$imgs = $this->productModel->getImagesPath($productId);
// after i got the imgs data, then delete the image in DB that references to the $productId
$this->productModel->deleteImage($productId);
foreach($imgs as $imgPath)
{
if(file_exists $imgPath) unlink($imgPath);
}
}
my question is :
in the concept of thin controller and fat model, should i move the method _deleteProductImages($id) to my productModel or should i leave it like that? if you have another better approach then please guide me here
I would have a method in my model for the deletion of products. This method would do ALL of the work required to delete a product (including deleting associated DB records, files, etc).
The method would return TRUE if the operation was successful.
In the event that an associated record or file couldn't be deleted, I'd log that error in it's operation, possibly raise an error message in the UI and continue.
The method may call other methods in other models...for instance, I may have a product_attributes model that stores attributes for all products. That model might have a method: delete_by_product_id(). In that case, my product model would call product_attributes->delete_by_product_id(), which would handle the deletion of the associated records.