-
Notifications
You must be signed in to change notification settings - Fork 349
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 get identifiableName of foreign key relationship #270
Comments
Just to be sure, are you saying the revision history for Status is showing status_id? Are you expecting the status field to show the actual status name/a field from the relationship? If so, In the not too distant future, I expect to push a v2 release on revisionable with some of the pending merge requests + feature changes - v2 will be Laravel 5.3+ most likely. If you want more info, I could paste some examples if possible.. which Laravel version are you on? |
Yes, i was expecting $history->newValue() and $history->oldValue() to trigger the identifiableName() method overwritten in the Status model, given that the actual value thats updated (status_id) is a foreign key. However, identifiableName() is never triggered :( At least thats what i got of the docs... Im using Laravel 5.2! note: i am NOT following laravels naming conventions for foreign keys! The code snippet i posted has simplified names for models and fields. |
Any progress on this issue? I'm also curious |
Same here! |
I have similar case with a model that has relationship to another model twice using different foreign keys. And indeed, the identifiableName method on the product is not working in either case, it just returns the id. class Project extends Model
{
use RevisionableTrait, Filterable;
public function productRelationOne()
{
return $this->belongsTo('App\Product', 'foobar_product_id');
}
public function productRelationTwo()
{
return $this->belongsTo('App\Product', 'foobaz_product_id');
}
} |
I got a (bad) workaround for the issue with multiple foreign keys. Maybe it helps others, maybe i got helpful feedback for a better solution. I've written a history method in my DepartmentsController to display all revisions grouped by the created_at timestamp. The
It's possible to add more |
@astierler any progress with this issue on how to use relationships between models? |
Ran into the same problem. I'm using non-standard names for the foreign keys as well, and Revisionable does not seem to have a way around this. To clarify, the problem arises from around here: revisionable/src/Venturecraft/Revisionable/Revision.php Lines 137 to 146 in 1b7c48b
This works great if your database table has, say, a "product_id" column and your model has a |
OK, I've ended up forking Revisionable and changing the getRelatedModel function to private function getRelatedModel()
{
$main_model = $this->revisionable_type;
$main_model = new $main_model();
// searches the declared relatedModels
if (is_array($main_model->relatedModels)) {
// this should work with array_keys, but somehow it's not working
foreach ($main_model->relatedModels as $k => $v) {
if ($k == $this->key) {
return $v;
}
}
}
// if none found, try to guess it
$idSuffix = '_id';
return substr($this->key, 0, strlen($this->key) - strlen($idSuffix));
} Now I can declared |
@andrechalom
So in my case I had this relationship between an Establishment and an EstablishmentType: class Establishment extends Model
{
protected $table = 'establishments';
public $relatedModels = ['establishment_type_id' => 'type'];
public function type()
{
return $this->belongsTo(EstablishmentType::class, 'establishment_type_id');
}
} class EstablishmentType extends Model
{
protected $table = 'establishments_types';
public function identifiableName()
{
return $this->typeName;
}
} |
@andrechalom your fork seem like a good fix |
Hey Chris,
I have two models, Task and Status:
And i'm looping through the history just like in the docs:
However, i'm still getting the foreign key values in
$history->oldValue()
and$history->newValue()
.For ex. 'John changed status_id from 1 to 2'.
Where have i gone wrong? Thanks in advance!
ps. cool package
The text was updated successfully, but these errors were encountered: