Comments (50)
There are currently two PR's from @mdmunir with two different approaches on how to solve this feature request #11493 and #11476 I think the people here should check them and see which oneis easier to implement and use.
from active-record.
can't we delegate this to the extraFields method of the related model and use definitions from there?
We do not need do that!
Please read this: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#autoloading
All we need is support expand
or embed
parameter.
I thought about this. It's not trivial, especially if you also want to support selectively pick up certain fields from relations.
From client side there is no feature to selectively pick up certain fields from relations in expand
/embed
. It will be overcoding.
From model side it can be easily handled like:
class Product extends ActiveRecord
{
public function extraFields()
{
return [
'price',
'quantity',
'categories' => ['id', 'title'], <- LOOK HERE
];
}
}
from active-record.
For the moment, I've worked around this problem by overruling the fields() call in my ActiveRecord model.
class MultiLevelActiveRecord extends \yii\db\ActiveRecord
{
public function fields()
{
$fields = parent::fields();
// Add multi-level expanded fields
$expandFields = explode(',', Yii::$app->request->getQueryParam('expand'));
foreach ($expandFields as $field)
{
if (strpos($field, strtolower($this->formName()).'.') === 0)
{
$fields[] = substr($field, strlen($this->formName()) + 1);
}
}
return $fields;
}
}
That enabled me to use:
?expand=category,author.name
But only based on exact modal names, and not based on relations. It's dirty, but does the job for me.
from active-record.
Please, use 👍 emoticon to the first message in this issue instead of adding more "+1" comments. Thank you!
from active-record.
I thought about this. It's not trivial, especially if you also want to support selectively pick up certain fields from relations.
from active-record.
can't we delegate this to the extraFields method of the related model and use definitions from there?
from active-record.
How will the GET query parameter looks like? How to go from there to the method parameters?
from active-record.
So query like GET /products?expand=price,categories
will give you result:
{
"id": 1,
"title": "Foo",
"price": "$10",
"categories": [
{
"id": 1,
"title": "Bar"
},
{
"id": 2,
"title": "Baz"
}
}
}
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
Where in code YII2 looks for "expand" parameter to choose which extra fields must be shown?
from active-record.
Where in code YII2 looks for "expand" parameter to choose which extra fields must be shown?
https://github.com/yiisoft/yii2/blob/master/framework/rest/Serializer.php#L159
from active-record.
+1
from active-record.
This problem make me cry, really!
I want to get list of "items" with author.
And in author I want to get Full name extra field.
But it's not working at all. Expand not using custom fields.
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
http://domain/restapi/users/1/posts/123 (#3319) +1
from active-record.
+1
from active-record.
I'm currently implementing something to handle this issue. I use scopes, so I define each scope in the model and the client requests the scopes to be returned. The request is
?scope=profile,orders,locations
and in the model I define them as arrays like in fields(). I personally don't think that the client should be able to define exactly what to be returned it should be limited to scopes defined on the server. It can be easily used with oauth server(in my case) which limits the visible scopes for each client.
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
+1
from active-record.
@SilverFire i think the reason is because reactions emojis doesn't get attention.
from active-record.
We have enough members asking for this feature, so we already know that we should take care about this issue. Clicking "+1" is OK to let us know, that you want this kind of enhancement too. We appreciate all kinds of help. We are thankful for showing, what is important for you and will take care of it. GitHub added emotions to help managing daily flow of changes in repo. Thank you
from active-record.
I'm not locking the conversation. We will be happy to hear more feedback from you
from active-record.
+1
from active-record.
+1
from active-record.
Related Issues (20)
- Any chance we could get a DataMapper ORM? HOT 1
- Error after update yiisoft/factory
- After PR #165 connection created every time when create an AR entity HOT 1
- Is it really necessary tableName as non static method? HOT 1
- ActiveRecord::insert(). When writing, it does not write fields. HOT 1
- update link
- Cannot serialize active record
- Use global `\InvalidArgumentException`
- Can ActiveRecord implement the interface ArrayableInterface, like Yii2? HOT 1
- `ActiveQuery` class should not extend `Yiisoft\Db\Query\Query` HOT 1
- Remove flag arguments from AR methods
- The method allPopulate in ActiveQuery will execute populate twice during execution
- PHPUnit 9.6.15 Warning - The configuration file did not pass validation!
- TableName() has no effect HOT 1
- update irc link
- The parameter identical of the method isAttributeChanged in class BaseActiveRecord has no effect when its value is false. HOT 2
- Yiiboot\ActiveRecord\ActiveRecord::populateRelation() not support the scalar type HOT 2
- Remove `psalm83.xml` after raising PHP version to `8.3`
- Summary for `ActiveRecord` HOT 4
- Realize `ReadOnlyActiveRecord` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from active-record.