-
Notifications
You must be signed in to change notification settings - Fork 394
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
Related relations #257
Related relations #257
Conversation
src/Auditable.php
Outdated
$related_relations_arr = []; | ||
$broad_relationship_types_arr = ['BelongsTo', 'HasMany', 'BelongsToMany', 'HasOne']; | ||
|
||
if ( ! $this->auditIncludeRelated) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Expected 0 spaces after opening bracket; 1 found
- Expected 1 space after closing parenthesis; found 9
src/Auditable.php
Outdated
{ | ||
return $related_relations_arr; | ||
} | ||
foreach ($broad_relationship_types_arr as $broad_relationship) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after closing parenthesis; found 9
src/Auditable.php
Outdated
return $related_relations_arr; | ||
} | ||
foreach ($broad_relationship_types_arr as $broad_relationship) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 newline after opening brace; 2 found
src/Auditable.php
Outdated
{ | ||
|
||
$broad_relationship_method = 'get'.ucfirst($broad_relationship) . 'Arr'; | ||
if ( ! method_exists($this, $broad_relationship_method)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Expected 0 spaces after opening bracket; 1 found
- Expected 1 space after closing parenthesis; found 13
src/Auditable.php
Outdated
} | ||
$related_relations_arr[$broad_relationship] = []; | ||
$relationship_arr = $this->$broad_relationship_method(); | ||
foreach ($relationship_arr as $relationship) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after closing parenthesis; found 13
src/Auditable.php
Outdated
$relationship_arr = $this->$broad_relationship_method(); | ||
foreach ($relationship_arr as $relationship) | ||
{ | ||
if ( ! method_exists($this, $relationship)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Expected 0 spaces after opening bracket; 1 found
- Expected 1 space after closing parenthesis; found 17
src/Auditable.php
Outdated
continue; | ||
} | ||
$RelationshipObjArr = $this->$relationship; | ||
if ( ! is_iterable($RelationshipObjArr)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Expected 0 spaces after opening bracket; 1 found
- Expected 1 space after closing parenthesis; found 17
src/Auditable.php
Outdated
{ | ||
$RelationshipObjArr = [$RelationshipObjArr]; | ||
} | ||
foreach ($RelationshipObjArr as $RelationshipObj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after closing parenthesis; found 17
src/Auditable.php
Outdated
'source_relation' => get_class($this), | ||
'source_relation_id' => $this->{$this->primaryKey}, | ||
]; | ||
if($RelationshipObj == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Expected 1 space after IF keyword; 0 found
- Expected 1 space after closing parenthesis; found 21
src/Auditable.php
Outdated
* this can happen when a foreign key constraint referances the self-same table. | ||
*/ | ||
$x=1; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after closing brace; newline found
src/Auditable.php
Outdated
*/ | ||
$x=1; | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after ELSE keyword; newline found
src/Auditable.php
Outdated
$related_relations_arr = []; | ||
$broad_relationship_types_arr = ['BelongsTo', 'HasMany', 'BelongsToMany', 'HasOne']; | ||
|
||
if (! property_exists($this,'auditIncludeRelated') || ! $this->auditIncludeRelated) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- No space found after comma in function call
- Expected 1 space after closing parenthesis; found 9
src/Auditable.php
Outdated
/** | ||
* this can happen when a foreign key constraint referances the self-same table. | ||
*/ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after closing brace; newline found
src/Auditable.php
Outdated
* this can happen when a foreign key constraint referances the self-same table. | ||
*/ | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 space after ELSE keyword; newline found
Hi @jschlies, Thanks for trying to come up with a solution to the many-to-many relation auditing. On your TODO, you state that there should be a Also, the logic you're proposing is using relation types ( And how do you deal with a |
It would be extremely hard to extract the data needed to run $somethingRelatedToodel->audits() for not only the object in question but it's relations. As I stated above, this fix runs the risk of overloading the DB w/ insert requests. Perhaps there is a way to bundle these inserts or get them offline somehow.
This is our current implementation and it serves us well. I will explore using events as you indicate. Can you point me in a direction on this?
I don't but if I did, (and I will research doing so), each relationship would get it's own related_relation record. As I stated above, this fix runs the risk of overloading the DB w/ insert requests. Perhaps there is a way to bundle these inserts or get them offline somehow. -=-=-=-=-=-=- |
See attached for our current implementation of $this->getAuditArr(). Ignore the sorting stuff. |
In regards to the usage of events, I commented in the open issue #58. The idea would be to fire an event from a repository and that would kickstart the audit process. |
You totally lost me with the |
I think that a great idea (events and potentially queuing) and will implement but I think that a separate topic to be taken up later. |
AuditableTrait.php.txt contains our implementation of getAuditArr(). |
The code, is confusing. |
I'll spend some time commenting.
|
Closing it, since we're currently working on version 4.1. |
This pull request is my initial attempt to audit the relationships of a particular model. Please comment but I am unwilling to push this intil I deal with the 3 Todo's.
Be warned: This can have negative impact on DB performance. Be very cautious to not use this feature on too many models (and those models relationships). This PR offers controls on both.
Todo:
Create table related_relations and use this rather that audits:related_relation_json
Integrate this into $model->audits()
Better unit tests
Todo (far future)
Make this Event/Queue friendly to try to deal w/ the DB load this this can generate.
To use:
Run the migration adding related_relation_json to audits table
All models for which you want to track related_relations need: