Skip to content
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

Unexpected behavior in Mappable trait #139

Closed
jonahgeorge opened this issue Nov 15, 2016 · 1 comment
Closed

Unexpected behavior in Mappable trait #139

jonahgeorge opened this issue Nov 15, 2016 · 1 comment

Comments

@jonahgeorge
Copy link

jonahgeorge commented Nov 15, 2016

It seems that the Mappable trait only exposes additional getters/setters for the mapped columns. Is there any intention to make this trait behavior similar to laravel/framework#8200 where the "bad" column name is actually removed as a getter/setter?

Example:

MariaDB> describe cities;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| region        | varchar(5)       | NO   |     | NULL    |                |
| region_id     | int(11)          | NO   |     | NULL    |                |
| name          | varchar(100)     | NO   |     | NULL    |                |

MariaDB> select * from cities;
+----+--------+-----------+-----------------+
| id | region | region_id | name            |
+----+--------+-----------+-----------------+
|  1 | NY     |         1 | Brooklyn        |

MariaDB> describe regions;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name          | varchar(100)     | NO   |     | NULL    |                |

MariaDB> select * from regions;
+----+------------------+
| id | name             |
+----+------------------+
|  1 | New York         |
class City extends Model
{
    use Eloquence, Mappable;

    public $timestamps = false;
    protected $maps = [
        "region_name" => "region",
    ];
 
    public function region()
    {
        return $this->belongsTo(Region::class);
    }
}

echo City::with('region')->find(1)->toJson();
/*
{
    "id": 1,
    "region": {
        "id": 1,
        "name": "New York"
    },
    "region_id": 1,
    "name": "Brooklyn"
}
*/

echo City::with('region')->find(1)->region; 
/*
"NY"
*/

The first example behaves as I would expect; however, in the second example I would expect it to return a class of type Region. Instead it's finding the underlying getter and fetching the region name.

@jarektkaczyk
Copy link
Owner

PR welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants