Skip to content

Feature Request: to_array_exclude on ->related(); #12

Description

@alfierivera

Many of us have encountered a couple of situations where excluding certain fields on a related Model dynamically is necessary.

For such purpose, I think that the following feature should be added to future updates of the ORM.

Use case:

We have an API and we want to expose comments like so /api/comments.
We also want to know who was the user that submitted that comment, but we do not want it's email or password.

Current workaround:

### Model_User:
$_to_array_exclude(array('email', 'password'));

### API Controller:
$comments = Model_Comment::query()->related('users')->get();
$result = array();
foreach ($comments as $i => $comment)
{
    $result[$i] = $comment->to_array();
}

return $result;

Note: This workaround has a big disadvantage. We will always exclude those fields when using ->to_array();

Proposed feature:

## Model_User:
We set nothing on the User's Model.

## API Controller:
$comments = Model_Comment::query()->related('users'=>array('to_array_exclude' => array('updated_at')))->get();
$result = array();
foreach ($comments as $i => $comment)
{
    $result[$i] = $comment->to_array();
}

return $result;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions