1

I am attempting to migrate some legacy data models/schemas to a fuel API, and have run into an odd issue with the to_array() method on a model that has two $_belongs_to properties.

When I load the model without the to_array() method, I properly receive both related items with eager loading, but as soon as I pass them through this function to convert the data to make it digestable by the new API, it will strip out the second $_belongs_to property. If I re-order the props in the $belongs_to array, it will show whichever item is first in the array.

My question is, how can I convert this data to an array without losing the second relationship?

Here are some cleaned up examples for ease of reference:

Transaction Model:

protected static $_belongs_to = array(
    'benefactor' => array(
        'key_from' => 'from_user_id',
        'model_to' => 'Model\\Legacy\\User',
        'key_to' => 'id',
    ),
    'user' => array(
        'key_from' => 'user_id',
        'model_to' => 'Model\\Legacy\\User',
        'key_to' => 'id',
    ),
);

Transaction Controller:

$result = array();
$id = $this->param('id');

if (!empty($id)) {
    $transaction = Transaction::find($id, array('related' => array('user', 'benefactor',)));
    if (!empty($transaction)) {

        // Works -- both benefactor and user are returned
        $result['transaction_works'] = $transaction;

        // Does not work -- only the benefactor is returned
        $result['transaction_doesnt_work'] = $transaction->to_array();
    }
}

return $this->response($result);
jahsome
  • 881
  • 8
  • 18
  • I am not so sure this is a to_array issue. It processes all the loaded (and not loaded) relations ($this->_data_relations). Are you sure all relations are eager loaded? Which version do you use? – mark.sagikazar Mar 06 '15 at 10:36
  • Hi Mark, thanks for the response. The error seems to occur when setting the array param at the same time as executing the method, it only returned the first item in the relationship array. Doing it before setting the variable worked, though. – jahsome Mar 09 '15 at 17:50

1 Answers1

0

For any googlers looking for help on this issue, I was seemingly able to return all relationships by simply executing the to_array() method before setting the return/results variable:

$result = array();
$id = $this->param('id');

if (!empty($id)) {
     $transaction = Transaction::find($id, array('related' => array('user', 'benefactor',)));
    if (!empty($transaction)) {
        $transaction->to_array();
        $result['transaction_works'] = $transaction;
    }
}

return $this->response($result);

Good luck!

jahsome
  • 881
  • 8
  • 18
  • Are you simply passing it as an object? I believe `$transaction->to_array();` by itself only creates an array version, and then throws it away, since you don't store it. It looks like it works because `$this->response($result)` has something that does the correct conversion. It looks rather interesting. Also, if you omit the `to_array` line, do you get the same results? – Tamás Barta Mar 11 '15 at 20:48
  • You're correct on both points. It doesn't store it, but setting a variable to its value and returning that variable does the trick. If I omit to_array, it works as expected (returns the object with all relationships), although the data isn's in the format I need. – jahsome Mar 16 '15 at 18:08