flightPHP - Active Record relations issue with setup? - Stack Overflow

I have 2 tables, users and roles, as follows:USERSROLES----------------------------

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 badge
Add a comment  | 

1 Answer 1

Reset to default 0

I 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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745638063a4637517.html

相关推荐

  • flightPHP - Active Record relations issue with setup? - Stack Overflow

    I have 2 tables, users and roles, as follows:USERSROLES----------------------------

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信