colbyr / aware

self-validating models for Laravel\Eloquent

Home Page:http://bundles.laravel.com/bundle/aware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[eloquent 2] Related Saving

colbyr opened this issue · comments

Look into the impact that relation inserts/attaches might have on Aware

As far as I can tell, the new relationship saving should be compatible with Aware. The methods all use the model's save() method.

DERP. ->insert() wont work exactly as expected...

Looks like it pulls out the objects attributes and creates a new one

commented

Some thoughts:

Saving a model using $model->save() will fail if the validation rules were not all met. Related models will be inserted using $model->submodel->insert()which also uses the save() method so it should validate.

However, if there is an error inserting into the submodel one would have to retrieve the errors using $model->submodel->errors. At the moment I am doing a simple array_merge to collect the messages from multiple models and pass them back together in order to display errors in the view.

commented

Can Aware detect whether I am calling save() on a submodel? If yes, calling $model->submodel->save() should let the errors bubble up to the main model.

commented

And one more:

If a model has a submodel with all required fields, this does not mean that the model does in fact require an entry for the submodel, just that when this entry is created, all fields have to be filled out. To get around this one should be able to create a rule in the parent model named after the submodel.

I'm thinking it might make sense to extend laravel's validator with rules for related models.

Maybe something like:

public static $rules = array(
    'related_items' => 'required|valid|max:4'
);