Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem with to_array on nested relations
  • Hi I got following Code Snippet:


    $news = \News\Model\News::find('all', array('related' => array('user', 'comments', 'comments.user')));

    foreach ($news as $n) {

    $this->data['news'][] = $n->to_array();

    }

    the orm relations are:
    news-belongsto-user
    news-hasmany-comments
    comment-belongsto-user
    user-hasmany-news

    The strange thing is that on the third level of nesting (comment->user) it has only a relation on the first comment every other comment has no user. If I try it with the Object all relations are fine.

    (Background info: I want to use the exclude_from_array feature on the ORM Model: Passwords, Salt and so on)
  • Which version of Fuel?

    Have you looked at the SQL generated (enable the profiler and db profiling to see it) to see if you can spot what could be wrong?
  • version 1.6, the query from the profiler gets all comments with the related user (passed the query manually in phpmyadmin). I still think the relation gets lost in the to_array() function in the orm Model.

    Besides from that is there another way to exclude select statements on nested relations?
  • Excluding elements only works on data in the current model, it doesn't work on related data.

    Please create a feature request for this, with a use-case, at https://github.com/fuel/core/issues
  • Actually it works with the to_array function even on related data but like I said there is the Problem that the relation gets lost on the third level of nesting.

    http://fuelphp.com/docs/packages/orm/creating_models.html#/to_array_exclude

    Even the Docs say it works on relations. Any suggestions before I make a feature Request?
  • I think the issue is here that you have the same Model in the query twice.

    to avoid recusive issues, to_array() keeps track of the models it has processed. Which I think accounts for the fact that the second 'user' object is skipped.
  • http://bin.fuelphp.com/~dS

    also I tried to cut the relationship between user and news, result is the same. The Object works fine as always.
  • It has nothing to do with the relation.

    The second time it sees a (set of) "Model_User" object(s), it says "already seen that class", and skips it.

Howdy, Stranger!

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

In this Discussion