Coder Social home page Coder Social logo

Comments (6)

jonspalmer avatar jonspalmer commented on August 28, 2024 1

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.

jonspalmer avatar jonspalmer commented on August 28, 2024 1

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.

jonspalmer avatar jonspalmer commented on August 28, 2024

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.

andyyapwl avatar andyyapwl commented on August 28, 2024

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.

andyyapwl avatar andyyapwl commented on August 28, 2024

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.

andyyapwl avatar andyyapwl commented on August 28, 2024

thanks for the detailed explanation!

from single-table-inheritance.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.