Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Related model not found by Has_Many relation
  • Hi,

    I use 2 Models in a Module called "Blog".
    First Model: "Model_Post":


      
    namespace Blog;

    class Model_Post extends \Orm\Model {
    protected static $_table_name = 'blog_post';

    protected static $_has_many = array(
    'comments' => array(
    'key_from' => 'id',
    'model_to' => 'Model_Comment',
    'key_to' => 'post_id',
    'cascade_save' => true,
    'cascade_delete' => false,
    )
    );
    }



    Second Model: "Model_Comment"
    namespace Blog;

    class Model_Comment extends \Orm\Model {
    protected static $_table_name = 'blog_post_comment';

    protected static $_belongs_to = array(
    'post' => array(
    'key_from' => 'post_id',
    'model_to' => 'Model_Post',
    'key_to' => 'id',
    'cascade_save' => true,
    'cascade_delete' => false,
    )
    );
    }

    If I try to acces $post->comments in a view, I get the following error:


    Fuel\Core\FuelException [ Error ]:
    Related model not found by Has_Many relation "comments": Model_Comment

    Both Models work, if use for instance:


    $posts = Model_Post::find('all');
    $comments = Model_Comment::find('all');
    in a Controller.

    What is the problem in the relation?
  • Ok, got it ;)


    'model_to' => 'Blog\Model_Post',


    I need to add Blog before Model_Post.
  • HarroHarro
    Accepted Answer
    Yes, class names in strings always have to be fully qualified, including the complete namespace.

Howdy, Stranger!

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

In this Discussion