Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create a new record directly form the base class #50

Closed
mazyvan opened this issue Aug 24, 2018 · 10 comments
Closed

Can't create a new record directly form the base class #50

mazyvan opened this issue Aug 24, 2018 · 10 comments

Comments

@mazyvan
Copy link

mazyvan commented Aug 24, 2018

I need to be able to create a record directly from the base class because of the code base that I have.

Obviously I can't refactor everything with out a step by step process. And also I need to support a type field without a class, like 'other' for instance. I know this issue/proposal was also asked here: #47 and I know the trade offs this could imply. We love this trait but as I've already said, having a large base code we need some way to add functionality in small cycles. I'm not the only one in this situation.

Right now we have a workaround idea. For those how have the same problem.
A Base Class that extends from Model in which we have all of our configuration then the normal class extends from it and then the inheritances. So we can call the base anywhere without problems.

class VehicleBase extends Model
{
    protected $table = "vehicles";

    // Conventional Model configuration goes here
}
use Nanigans\SingleTableInheritance\SingleTableInheritanceTrait;

class Vehicle extends VehicleBase
{
    use SingleTableInheritanceTrait;

    protected static $singleTableTypeField = 'type';
    protected static $singleTableSubclasses = [Car::class, Truck::class];
}
class Car extends Vehicle
{
    protected static $singleTableType = 'car';
}
class Truck extends Vehicle
{
    protected static $singleTableType = 'truck';
}

Anyway, This is a great tool. Thanks for the work

@jonspalmer
Copy link
Owner

Can you illustrate what it is that you really need? What is it that requires:

I need to be able to create a record directly from the base class because of the code base that I have.

I general these use cases are a code smell of some other factoring problem. As stated in #47 there are numerous edge cases that quickly evolve from this kind of approach that are normally solved better in you application logic than they are in this trait.

@jonspalmer
Copy link
Owner

Closing out a long ago asked question. With more details and a PR we can revisit.

@darron1217
Copy link
Contributor

darron1217 commented May 19, 2020

@jonspalmer
I need the same functionality in my project.
What is the best-practice for creating Model from form request?

@jonspalmer
Copy link
Owner

@darron1217 Can you elaborate on the need. Some example code would be helpful to be able to offer advice on the right solution.

@darron1217
Copy link
Contributor

darron1217 commented May 19, 2020

@jonspalmer
As I said, Form Request situation need it.

Imagine that you are registering vehicle with type option.

HTML

<form>
    <select name="type">
        <option>car</option>
        <option>truck</option>
    </select>
    <input type="text" name="color" />
</form>

PHP

public function store(Request $request)
{
    Vehicle::create($request);
    return redirect()->route('vehicle.index');
}

@jonspalmer
Copy link
Owner

As mentioned in #47 the trick is you really need to create an instance of the concrete class. Perhaps you can use the type map to do that it would be something like

$childTypes = Vehicle::getSingleTableTypeMap();
$requestType = $request['type'];
$requestTypeClass = $childTypes[$requestType];
(new $requestTypeClass)->newInstance([], true);
...

@darron1217
Copy link
Contributor

darron1217 commented May 20, 2020

@jonspalmer
That's exactly what I'm doing.

Then, how about adding it as function like Vehicle::createSubclass() and Vehicle::createManySubclass() ?
cuz it's really common situation when using this package.

@jonspalmer
Copy link
Owner

What would the API be on those methods? Can you Pseudo code it? A PR with tests would be welcomed.

@darron1217
Copy link
Contributor

darron1217 commented May 20, 2020

Okay, I'll create PR for it.

BTW, would you re-open this issue?

@darron1217
Copy link
Contributor

@jonspalmer I created PR. Please check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants