admin管理员组文章数量:1025745
I have 2 tables, users and roles, as follows:
USERS ROLES
-----------------------------
id id
role_id name
fname ...
...
How do I properly setup the relation between the 2?
In my mapper Users extends \flight\ActiveRecord
I have this constructor:
function __construct($databaseConnection) {
$this->relations = [
'role' => [ self::HAS_ONE, \Main\Models\Roles::class, 'id' ]
];
parent::__construct($databaseConnection, 'users');
}
Based on the docs online, when I run this code in my controller
$user = $this->mapper->find(7);
I'd expect to have $user->role->name with the name of the role assigned to user with id=7 but instead it's NULL.
To pull the correct role name I need to run the following block:
$user = $this->mapper->find(7);
$user->role->find($user->getData()['role_id'])->getData();
and only then I have the proper value in $user->role->name.
Is this how this feature is meant to work or am I doing something wrong in my mapper?
Thank you.
I have 2 tables, users and roles, as follows:
USERS ROLES
-----------------------------
id id
role_id name
fname ...
...
How do I properly setup the relation between the 2?
In my mapper Users extends \flight\ActiveRecord
I have this constructor:
function __construct($databaseConnection) {
$this->relations = [
'role' => [ self::HAS_ONE, \Main\Models\Roles::class, 'id' ]
];
parent::__construct($databaseConnection, 'users');
}
Based on the docs online, when I run this code in my controller
$user = $this->mapper->find(7);
I'd expect to have $user->role->name with the name of the role assigned to user with id=7 but instead it's NULL.
To pull the correct role name I need to run the following block:
$user = $this->mapper->find(7);
$user->role->find($user->getData()['role_id'])->getData();
and only then I have the proper value in $user->role->name.
Is this how this feature is meant to work or am I doing something wrong in my mapper?
Thank you.
Share Improve this question asked Nov 17, 2024 at 4:59 BiusBius 31 bronze badge1 Answer
Reset to default 0I think I see the issue. Based on looking at the docs here it looks like that should reference the local key rather than foreign key id.
So your code should be this:
function __construct($databaseConnection) {
$this->relations = [
'role' => [ self::BELONGS_TO, \Main\Models\Roles::class, 'role_id' ]
];
parent::__construct($databaseConnection, 'users');
}
Hope on the chat if you have any further questions!
I have 2 tables, users and roles, as follows:
USERS ROLES
-----------------------------
id id
role_id name
fname ...
...
How do I properly setup the relation between the 2?
In my mapper Users extends \flight\ActiveRecord
I have this constructor:
function __construct($databaseConnection) {
$this->relations = [
'role' => [ self::HAS_ONE, \Main\Models\Roles::class, 'id' ]
];
parent::__construct($databaseConnection, 'users');
}
Based on the docs online, when I run this code in my controller
$user = $this->mapper->find(7);
I'd expect to have $user->role->name with the name of the role assigned to user with id=7 but instead it's NULL.
To pull the correct role name I need to run the following block:
$user = $this->mapper->find(7);
$user->role->find($user->getData()['role_id'])->getData();
and only then I have the proper value in $user->role->name.
Is this how this feature is meant to work or am I doing something wrong in my mapper?
Thank you.
I have 2 tables, users and roles, as follows:
USERS ROLES
-----------------------------
id id
role_id name
fname ...
...
How do I properly setup the relation between the 2?
In my mapper Users extends \flight\ActiveRecord
I have this constructor:
function __construct($databaseConnection) {
$this->relations = [
'role' => [ self::HAS_ONE, \Main\Models\Roles::class, 'id' ]
];
parent::__construct($databaseConnection, 'users');
}
Based on the docs online, when I run this code in my controller
$user = $this->mapper->find(7);
I'd expect to have $user->role->name with the name of the role assigned to user with id=7 but instead it's NULL.
To pull the correct role name I need to run the following block:
$user = $this->mapper->find(7);
$user->role->find($user->getData()['role_id'])->getData();
and only then I have the proper value in $user->role->name.
Is this how this feature is meant to work or am I doing something wrong in my mapper?
Thank you.
Share Improve this question asked Nov 17, 2024 at 4:59 BiusBius 31 bronze badge1 Answer
Reset to default 0I think I see the issue. Based on looking at the docs here it looks like that should reference the local key rather than foreign key id.
So your code should be this:
function __construct($databaseConnection) {
$this->relations = [
'role' => [ self::BELONGS_TO, \Main\Models\Roles::class, 'role_id' ]
];
parent::__construct($databaseConnection, 'users');
}
Hope on the chat if you have any further questions!
本文标签: flightPHPActive Record relations issue with setupStack Overflow
版权声明:本文标题:flightPHP - Active Record relations issue with setup? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745638061a2160572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论