Skip to content

ManyToMany - foreign key is not customizable #87

Description

@tadeck

There is some problem with foreign keys, at least in many-to-many relations.

When I create model 'X_Y_Z' and assign the table named 'z' as the base table, the foreign key name is created using the model name (not the table name) and concatenated with underscore and primary key.

In the described example it would look like 'x_y_z_id' and to alter this the only way is to modify the core.

Please, modify it, eg. by inserting protected property '_foreign_key' in the core (default = NULL) and alter the line no. 777 (https://github.com/sittercity/sprig/blob/master/classes/sprig/core.php#L777) into the following:

if ($this->_foreign_key) {
    $key = $this->_foreign_key;
} else {
    $key = $this->_model.'_'.$this->_primary_key;
}

This deals with the non-standard foreign keys, but also means adding the new property and you may not want to do that and you may want to look for another solution, like changing

$key = $this->model.''.$this->_primary_key;

into

$key = $this->table.''.$this->_primary_key;

The above mentioned will construct foreign key name from table name and the primary key, which is more obvious in my opinion.

Regards,
T.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions