Comments (6)
I added a test around saveMany looks like it works fine. There must be something else going on with your actually use case but its pretty hard to know what the difference is. I wonder if something is going wrong with validation or db constraints.
Closing for now. If you can provided a failing test case in a PR I'd be happy to look more.
from single-table-inheritance.
The Scope isn't involved in the saving of the objects in saveMany() except that it manipulates the type
attribute to set it according to the class that is being instantiated. I would imagine in your example that your hydrate
call results in N Car
objects being inserted into the database. Whatever the $data
array has for type will get ignored. This is exactly the desired behavior. To properly create the right objects you must instantiate the appropriate classes directly. It really has to work this way, imagine that the subclasses of Car
have getters, setters and other mutators of the attributes. Those won't get called if you hydrate Car
objects either.
from single-table-inheritance.
You've jumped to a solution before describing the problem. Can you provide the code that does t work or better yet a failing test?
from single-table-inheritance.
Code example:
$user->cars()->saveMany($cars);
Invoking saveMany function does not save new cars into database. If I remove SingleTableInheritanceTrait, it's able to save.
Btw, thanks for this great library!
from single-table-inheritance.
It actually occurs when hydrate function was called i.e.
$cars = Car::hydrate( $data["cars"] );
$user->cars()->saveMany($cars); //Data not inserted into database when objects are instantiated by hydrate function
So I replace it with a manual approach over hydrate function:
$className = 'App\' . $type;
$car = new $className($car_array);
array_push($cars, $car);
from single-table-inheritance.
thanks for the detailed explanation!
from single-table-inheritance.
Related Issues (20)
- "Field 'type' doesn't have a default value" when saving a model HOT 3
- Update Readme HOT 1
- One record two types. HOT 3
- Can we set the type of model using the base model? HOT 5
- hasMany relation doesn't work for parent class
- Can't create a new record directly form the base class HOT 10
- Broken link in README - Mark Smith's website is gone HOT 1
- Laravel 5.7 Support HOT 6
- Type is not inserted when using Laravel factories HOT 1
- Travis Builds are failing HOT 2
- Laravel 5.8 support
- Support the Eloquent `retrieved` event HOT 1
- V0.8.6 causes incorrect attributes to be set HOT 11
- Laravel 6.0 support HOT 1
- Eager loading child model relationship HOT 2
- Enhence performance when querying root model of subclasses HOT 5
- PHP hangs when parent and child inheritance is not setted properly. HOT 3
- `newFromBuilder` does not maintain mutated attributes HOT 2
- onlyTrashed()->forceDelete() seems to lose the SingleTableInheritance scope, deletes way more than intended HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from single-table-inheritance.