最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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!

Post a comment

comment list (0)

  1. No comments so far