Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM can't get relation when key_to and key_from are the same
  • I have two objects, Model_Post and Model_Profile

    In the post model has_one "user" and the profile model has many posts

    in model post

      protected static $_has_one = array(
              'user' => array(
                  'key_from' => 'user_id',
                  'model_to' => 'Model_Profile',
                  'key_to' => 'user_id',
                  'cascade_save' => false,
                  'cascade_delete' => false,
              ),
              'parent' => array(
                  'key_from' => 'parent_id',
                  'model_to' => 'Model_Post',
                  'key_to' => 'id',
                  'cascade_save' => false,
                  'cascade_delete' => false,
              )
          );

    When I try to query the relation its always null even though I can verify the user_id in model_post is there and model_profile exists with the same user_id




  • Is your table and model definition correct?

    This is a has_one relation, which maps the primary key of Model_Post to the foreign key in Model_Profile and Model_Post.

    So the key_from in both definitions must be the primary key, and since they are different, you made a mistake there.

    I assume Model_Profile describes the user, so every Model_Post has a user that created the post. Correct? If so, then one user can have many posts, every post has a user, and one post can have many replies, and every reply has a parent.

    In that case, you need these relations:

    Model_Post:

    protected static $_has_many = array(
        // one post can have many replies
        'children' => array(
            'key_from' => 'id',
            'model_to' => 'Model_Post',
            'key_to' => 'parent_id',
            'cascade_save' => false,
            'cascade_delete' => false,
        )
    );

    protected static $_belongs_to = array(
        // every post belongs to a user
        'user' => array(
            'key_from' => 'user_id',    // the FK in Model_Post
            'model_to' => 'Model_Profile',
            'key_to' => 'id',            // assuming this is the PK of Model_Profile
            'cascade_save' => false,
            'cascade_delete' => false,
        ),
        // a post can be a reply to a parent post
        'parent' => array(
            'key_from' => 'parent_id',
            'model_to' => 'Model_Post',
            'key_to' => 'id',
            'cascade_save' => false,
            'cascade_delete' => false,
        )
    );

    Model_Profile:

    protected static $_has_many = array(
        // one user can have many posts
        'posts' => array(
            'key_from' => 'id',
            'model_to' => 'Model_Post',
            'key_to' => 'user_id',
            'cascade_save' => false,
            'cascade_delete' => false,
        )
    );



Howdy, Stranger!

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

In this Discussion