Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem fetching with relations
  • Ok, here I am again, I'm trying to get done with te ORM classes and I got this so far: post.php -- table: post
    class Model_Post extends \ORM\Model {
     protected static $_properties = array('id','date','title','post','draft','open');
     protected static $_table_name = 'post';
     protected static $_has_many = array('comments');
    }
    

    comment.php -- table: comments
    class Model_Comment extends \Orm\Model {
    
     protected static $_properties = array('id', 'name', 'email', 'date', 'url',
      'comment', 'ip', 'approved', 'post_id');
     protected static $_table_name = 'comments';
     protected static $_belongs_to = array('post');
    }
    

    so If I get a post like this (I prefere new Object rather than factory)
    $post = new Model_Post;
    $post = $post->find(); //should get the 1st one and actually it does
    $post->comments; //it's null!!! instead of having 2 comments
    

    FK in comments table is post_id I'm just making the helloworld blog app x'D but I don't get why I'm not fetching the comments :/ thanks in advance!
  • The problem is that your Post model is named Post_Model but in classes/model/post.php (I assume). As such it's not found and never loaded. In the latest ORM version this would throw an exception but in RC2(.1) this would probably return null.
  • actually it's Model_Post :$ I can echo $post->title or $post->date, the problem might be with comments $post->comments
  • Post your actual code here, not a rewrite or you might just be missing something I can't check. Most likely what I said still holds true in some way, or $post->comments would return array() or at least show an error of some kind.
  • Table structure described in class (properties declared just for phpdoc purposes) app/classes/model/post.php
    http://pastebin.com/eNEGXSuv app/classes/model/comment.php
    http://pastebin.com/xfrx2HWU
  • Models work because their properties don't exist and requests are caught by __set() & __get() magic methods. By declaring them you have made this impossible and nothing will work anymore. Thus you have to remove those properties for the model to work as expected.
  • Ok now it works, then I'll use @property for phpdoc with every model :) thanks!

Howdy, Stranger!

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

In this Discussion