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

Relation Handling #20

Open
mootensai opened this issue Sep 22, 2023 · 3 comments
Open

Relation Handling #20

mootensai opened this issue Sep 22, 2023 · 3 comments

Comments

@mootensai
Copy link

mootensai commented Sep 22, 2023

I could get the relation response if I use join() to add expand to the request. But it get error when it try to populate the model like this image below

image

I've follow the convention mentioned in the readme that uses both the primary model & the child model. using array declaration when its one to many, but still, the problem persists.

 * @property-read MyOtherModel $myOtherModel

do you know how to fix it? or have you encounter similar problem?

@simialbi
Copy link
Owner

What do your classes look like? DemoAddress and User?

@mootensai
Copy link
Author

mootensai commented Sep 22, 2023

let's use User & Role. User has many Role(s)
image

use simialbi\yii2\rest\ActiveRecord;

/**
 * User
 * 
 * @property integer $id
 * @property string $username
 * @property string $email 
 * @property string $provider 
 * @property string $repeatPassword 
 * @property string $password 
 * @property string $resetPasswordToken 
 * @property string $confirmationToken 
 * @property bool $confirmed 
 * @property bool $blocked 
 * @property string $createdAt 
 * @property string $updatedAt  
 * 
 * @property-read Role[] $roles
 */
class User extends ActiveRecord {

    public $repeatPassword;

    public static function primaryKey(): array
    {
        return ['id'];
    }
}

and for Role

use simialbi\yii2\rest\ActiveRecord;

/**
 * Role
 * 
 * @property integer $id
 * @property string $name
 * @property string $description 
 * @property string $type 
 * @property string $createdAt 
 * @property string $updatedAt 
 * 
 * @property-read User $user
 */
class Role extends ActiveRecord {

    public static function primaryKey(): array
    {
        return ['id'];
    }
}

and use this command to run the query

User::find()->join('LEFT JOIN','role')->where(['id' => $id])->one()

@mootensai
Copy link
Author

mootensai commented Sep 22, 2023

I could add function getRole() to make it work

class User extends ActiveRecord {

    public static function primaryKey(): array
    {
        return ['id'];
    }

    public function getRole() {
        return $this->hasMany(Role::class, []);
    }
}

but I don't know if this is according to your design or not.

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