Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

I take it boot() is invoked once only, the first time this object is instantiated. It means events can be set up only if needed, so helping performance. That's my understanding.

Hmm, I wonder if this is anything to do with my overriding of the constructor?

Last updated 8 years ago.
0
Solution

Yes, that appears to be it. As is often the case, you can't see the wood for the trees until you try to explain it to someone else. To fix this, the constructor of my adapter must call the constructor of the parent model. It does not need to pass any data to it; it just needs to make sure it is called.

class OrderAdapter extends Order
{
    parent::__construct(); // Events are set up in here

    public function __construct($source_data)
    {
        $this->name = $source_data['order_name'];
        // etc.
    }
}

I override the constructor in the first place because instantiating an Order through the OrderAdapter involves passing it initial data in a completely different form from the format normally expected by an Eloquent model. In this case, it's data from a WooCommerce API call that scans for new orders on remote shops, and is a stdClass and needs some translations of some properties and further API lookups of others.

Last updated 8 years ago.
0

Off topic but I prefer to use observers instead of the boot method http://laravel.com/docs/4.2/eloquent#model-observers

I don't know, it just seems cleaner to keep those events out of the model

0

Sign in to participate in this thread!

Eventy

Your banner here too?

judgej judgej Joined 4 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.