Comments (6)
@robbydooo I'm curious about what you database schema looks like. This package is meant to introduce Single Table Inheritance to Eloquent, which means that Content, Review, Posts, and Pages should all be in the same table where each row would have a unique primary key. Review::destroy(1)
should only delete one record if this is the case.
from parental.
@dallincoons I am attempting to use this on a table where the id and the type field are unique. The table is to store content related to locations so the id field is a foreign key to a locations table that all the STI relate to.
E.g London (Location Model)--->Content (parent model) -> Reviews, Posts, Pages (Child Models)
There is only one child type per location as its just holding JSON data imported from an API.
My understanding was that the global scope being applied to all queries would have meant Review::destroy(1)
or the delete()
should have added the "where type = xxx" clause but that clearly isn't the case :-).
Sounds like i might need to move the location id to a separate field and keep id unique for this package to work? I can then have a unique constraint on the location_id and type fields.
Thanks,
Rob
from parental.
Hey @robbydooo - yes, currently this package only supports the standard: id's as unique primary keys.
I'd be open to supporting non-unique ids like your case. Let me know if you can do some exploration on this feature and maybe PR it - thanks.
from parental.
@calebporzio thanks for your work on this. I will try and do some research on this in the new year. I need to understand global scopes a lot better than I currently do to submit any PR.
Thanks
from parental.
I have been working on this issue for some time now and finally understand whats happening.
In general, this library supports composite keys.
The area where it does not is in saving/deleting of data to the database. This is because the global scopes and event listeners used in the trait only apply to "SELECT" queries.
Update/delete queries are currently ignored in these scopes/listeners.
To solve this i have found that adding this code to the parent model fixes this issues.
/**
* REQUIRED FOR Tighten/PARENTAL library to work with composite keys.
*
* The parental library does not support composite keys out of the box but overriding this function solves the problem.
* The global scopes update the queries but not the commits to the database. This sets the keys in the write queries to the composite key.
*
* If this is removed, all records for an id will get overwritten each time a save is executed.
*
* See https://github.com/tightenco/parental/issues/25
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery($query)
{
$query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery())
->where($this->getInhertanceColumn(), '=', $this->classToAlias(get_class($this)));
return $query;
}
This function overrides the setKeysForSaveQuery method that normally only returns the models id field and instead adds the type field to the where clause.
I am not sure if the best route forward is to just document this somewhere so other people can find it if they are using composite keys or whether we should have a boolean flag for composite keys that then modifies the setKeysForSaveQuery method.
Just to be clear, the issue this solves is when you are using the type field and the id field as a composite key so you can store different types of data on a related table. E.g Products, Users etc.
from parental.
Thanks for posting the solution. Yeah, I'm not sure what to do with this. I'm going to close the issues for now. You are welcome to PR something to the bottom of the docs as a caveat, or something to package that wouldn't cost anything in performance or cause other issues.
At least someone will be able to find this fix by googling for it though. Thanks again.
from parental.
Related Issues (20)
- How to properly use the childTypes variable for authenticated routes HOT 3
- withCasts does not work HOT 2
- [Feature request] Merge fillable columns with parent HOT 1
- Is there a way to filter query results by `getInheritanceColumn` when the parent is also a child
- Child relationships don't work with a parent collection. HOT 2
- Booted method of parent called
- Add .gitattributes
- I applaud your geniusness HOT 1
- Empty Fillable Array Causes Mass Assignment Exception HOT 1
- Bypass Totally Guarded Exception in HasChildren@getChildModel HOT 1
- Multiple types for the same user HOT 2
- Fillable on parent and child HOT 1
- Eager loading issue :( HOT 3
- Set attributes on child Models HOT 2
- Use parent's fillables in children models HOT 3
- Laravel notification users HOT 2
- Incompatible with Laravel 7.1 HOT 3
- Infinite recursion when setting parent in childTypes HOT 1
- Documentation tweak HOT 1
- FR: Determining Model from type column in relationship. HOT 1
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 parental.