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

Cannot access $_related in Child class of \yii\db\ActiveRecord #842

Closed
mfrancis107 opened this issue Sep 4, 2013 · 3 comments · Fixed by #1125
Closed

Cannot access $_related in Child class of \yii\db\ActiveRecord #842

mfrancis107 opened this issue Sep 4, 2013 · 3 comments · Fixed by #1125
Assignees
Milestone

Comments

@mfrancis107
Copy link

Currently $_related is a private property and there is no public or protected method to get $_related without specifying a key. Either $_related needs to be made protected or there needs to be a getRelated function that returns all. Without either of those, a class extending ActiveRecord can not access all of $_related.

@ghost ghost assigned qiangxue Sep 4, 2013
@cebe
Copy link
Member

cebe commented Sep 4, 2013

Why do you need to access it? You can get all relation data via $model->relationName and populate data via $data->populateRelation('relationName', $data)

@mfrancis107
Copy link
Author

As an example.

Creating an API that returns json.

Let's say Users have Orders. In which we want a GET request that returns the user object with orders.

{ id: 1,
 name: 'Bob',
 orders: [
    {id: 1, name: 'Order 1'},
    {id: 2, name: 'Order 2'},
 ]
}

Currently, Json::Encode($user); will only output the database columns of the user table, because of the toArray method is limited. If one wanted to override the toArray method to include the $_related, they could not do so without specifically building a new array object that is manually populated like below:

public function toArray(){
    $data = new Array();
    $data = $this->attributes();
    $data['users'] = $this->users;
    return $data;
}

It would be nice to be able to do something like:

public function toArray(){
    $data = new Array();
    $data = $this->attributes();
    $data = array_mrege($data, $this->_related);
    return $data;
}

That way if I chose to do $user = User::find()->with('orders')->with('phone_numbers'); Both the orders and phone_numbers would be output in the json.

@cebe
Copy link
Member

cebe commented Sep 4, 2013

Yeah I see your point. What I also found to be nice would be when __isset() would consider relational data as set when loaded and not set when not loaded.

@ghost ghost assigned cebe Nov 3, 2013
cebe added a commit to cebe/yii2 that referenced this issue Nov 3, 2013
fixes yiisoft#842

- allows checking whether a relation has been populated
- getting a list of relation names that have been populated
- getting all populated relation data

todo:

- [] add phpdoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants