sedp-mis / base-api

Abstraction for api resources in laravel.
https://sedp-mis.github.io/base-api/
MIT License
0 stars 0 forks source link

Cannot display attributes for comments #1

Closed markiiman closed 8 years ago

markiiman commented 8 years ago

Can you please check if there are some bugs around the method eagerLoadRelations in which I refactored? The sql queries are correct, (I used event listener), the output for relations (relations=comment or relations[]=comments) is correct, but for the url /api/v1/posts?relations[comments][attributes][]=id&relations[comments][attributes][]=text, it returns null.

Attached are some screenshots of the test cases I've encountered. Thanks :)

string_comment array_comment array_comment1 query_comments

ajcastro commented 8 years ago

Is this already resolved, @markii1607?

ajcastro commented 8 years ago

This is considered working

markiiman commented 8 years ago

Yes Kuya @ajcastro it was already resolved and it is already working

markiiman commented 8 years ago

But the problem for that is when the relation defined in the query param is a reference table, it cannot filter the attributes in the relation.

example:

Post model hasMany Comments Comments model belongsTo Post

URL: GET /comments?relations[post][attributes][]=text

since Post model doesnt have comments_id in it's table, it returns an error.

code in base-repository:

if (is_array($rules) && array_key_exists('attributes', $rules)) {
      $eagerLoads[$relation] = function ($q) use ($rules) {
             if (!in_array($fk = $this->model->getForeignKey(), $rules['attributes'])) {
                       array_push($rules['attributes'], $fk);
              }
              $q->select($rules['attributes']);
       };
 }
ajcastro commented 8 years ago

Please change your composer.json. It should be like this. There are major changes in the base-repository which is a dependency. You might need to update your code, because it is backwards incompatible.

image