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

Relationships #27

Closed
harikt opened this issue Jul 14, 2016 · 4 comments
Closed

Relationships #27

harikt opened this issue Jul 14, 2016 · 4 comments

Comments

@harikt
Copy link
Contributor

harikt commented Jul 14, 2016

Hi Paul,

I came across a strange issue in relationship. Was trying to write a unit test, but reading https://github.com/atlasphp/Atlas.Orm#relationships I got a bit confused so thought of discussing the same.

We have an articles table and users table.

Articles Table

id user_id title body
1 1 something something
2 1 another another

Users Table

id name
1 James
2 Bond

The mapper was defined as

class ArticleMapper extends \Atlas\Orm\Mapper\AbstractMapper
{
    protected function setRelated()
    {
        $this->manyToOne('user', UserMapper::CLASS);
        //    ->on([
        //        'user_id' => 'id'
        //    ]);
    }
}

class UserMapper extends \Atlas\Orm\Mapper\AbstractMapper
{
    protected function setRelated()
    {
        $this->oneToMany('articles', ArticleMapper::CLASS); 
        // added on later
    }
}

Now when you fetch article id 2 it returns Bond as user, but it should return James instead.

$record = $atlas->fetchRecord(ArticleMapper::CLASS, 2, ['user']);
echo $record->user->name;

When we add on it works as expected. I noticed when we change article id to 3, it tries to fetch user with id 3. Is that expected ? That is where I got confused with the docs https://github.com/atlasphp/Atlas.Orm#relationships

By default, in all relationships except many-to-one, the relationship will take the primary key column(s) in the native table, and map to those same column names in the foreign table. In the case of many-to-one, it is the reverse; that is, the relationship will take the primary key column(s) in the foreign table, and map to those same column names in the native table.

Thank you

@akDeveloper
Copy link

akDeveloper commented Jul 14, 2016

In your UserMapper class i see that you define oneToMany as
$this->oneToMany('user', ArticleMapper::CLASS);
Is this a typo?
I think it should be like this
$this->oneToMany('articles', ArticleMapper::CLASS);

@harikt
Copy link
Contributor Author

harikt commented Jul 14, 2016

@akDeveloper yes that was a typo in what I have written here not in source code.

Have updated the comment.

Thank you.

@jakejohns
Copy link
Contributor

I think the issue is that it's mapping articles.id to users.id, but you want it to map articles.user_id to users.id

Docs say:

[...] same column names in the foreign table.
[...] map to those same column names in the native table.

So you gotta use the on method like you said if you want to use the terse id form for primary keys,

or you can change the db structure to use the same explicit column names if you want to use the "auto mapping" .... so like this:

article table

article_id user_id title body
1 1 something something
2 1 another another

user table

user_id name
1 James
2 Bond

So user_id is the name of the column in both tables.

@harikt
Copy link
Contributor Author

harikt commented Jul 15, 2016

@jakejohns yes. After your post, I re-read the docs once again today and sounds right.

Thank you.

@harikt harikt closed this as completed Jul 15, 2016
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

3 participants