Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to use Orm Model $_to_array_exclude on the fly.
  • I am trying to find the best way to limit data for a specific query when using Orm_Model::to_array().

    In the case below, I would like to limit properties in the "accounts" model.

    Generally in cases like this I either just build my own array only keeping the parts I want to reveal, or once in awhile I might do the opposite, and use Orm_Model::to_array() and unset the elements I want to hide after that.

    But I was thinking how can I set the $_to_array_exclude properties on the fly before executing my query, and then reset it to it's original value.

    $results = Model_Accounting_Invoice::query()
    ->related('items')
    ->related('accounts')
    ->where('account_id', $account_id)
    ->where('id', $invoice_id)
    ->get_one();
  • If you mean you want to be able to filter the result of to_array(), like the ORM equivalent can, it's probably best to create a Model_Crud base model, add a method to define the filter, and overload the to_array() method to honour the defined filter.
  • So I need this model to remain an Orm Model.

    So thinking out loud...

    Do you think I can extend my Orm Model and have that define _to_array_exclude?

    And when I only want those fields I use that Model...

    So for instance...

    class Model_Accounts_Limitied extends Model_Accounts{

    protected static $_to_array_exclude = array(
    'sensitive_info'
    );

    }

    Or does that seem to odd....

  • HarroHarro
    Accepted Answer
    That is what that array is for, see also my answer to your related question.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion