You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
class Category extends Model
{
use Translatable;
protected $primaryKey = 'hash';
}
However, when setting primary key like this, when calling the method getRelationKey(), it also returns hash:
public function getRelationKey()
{
if ($this->translationForeignKey) {
$key = $this->translationForeignKey;
} elseif ($this->primaryKey !== 'id') {
$key = $this->primaryKey;
} else {
$key = $this->getForeignKey();
}
return $key;
}
I don't quite understand how the elseif ($this->primaryKey !== 'id') works here and why is it implemented like that? I would have thought the following:
I know I can make use of $translationForeignKey but I was just curious about for which cases the elseif was usefull implemented like this. Of course I would be happy to make a pull request if something is wrong here.
Cheers for the great package!
PS: I also think that the Laravel function getForeignKey should depend on the primary_key:
public function getForeignKey()
{
return Str::snake(class_basename($this)).'_' . $this->primaryKey;
}
The text was updated successfully, but these errors were encountered:
Hi @ElMatella and thank you for sharing your thought. This is an interesting question. It seems that adding the table name to the relationship key makes sense. However the wished behavior can be different from project to project. If I remember correctly, this was implemented like this because it was convenient to the first developer that asked for this feature. Then others wanted a different value, so that's why $translationForeignKey was created.
A PR would make sense but that would be a breaking change and I wouldn't make a major release just for that. Also changing this in a major release would introduce nasty bugs to those who would not pay attention to the incompatibility.
I understand the idea. Maybe we could still keep it somewhere for the next major major release. As a matter of fact, someone made a pull request to the laravel framework core to change the framework behaviour on that subject, you can view the pull request here: laravel/framework#16396
As this is a Laravel Package, I assume that it would be a good idea to follow the framework and that it would not break any build if we integrate it when Laravel 5.4 is released.
This would simplificate to:
public function getRelationKey()
{
if ($this->translationForeignKey) {
return $this->translationForeignKey;
}
return $this->getForeignKey();
}
Hi, I needed to change primary key for a model:
However, when setting primary key like this, when calling the method
getRelationKey()
, it also returnshash
:I don't quite understand how the
elseif ($this->primaryKey !== 'id')
works here and why is it implemented like that? I would have thought the following:I know I can make use of
$translationForeignKey
but I was just curious about for which cases the elseif was usefull implemented like this. Of course I would be happy to make a pull request if something is wrong here.Cheers for the great package!
PS: I also think that the Laravel function
getForeignKey
should depend on theprimary_key
:The text was updated successfully, but these errors were encountered: