Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
1st time using Orm, only returns post with id of 1
  • Hi, I'm doing Phil's "Build an Admin Panel with the Fuel PHP Framework " at nettuts. I got to the point of implementing eager loading and am having a problem. The query only returns the first post and it seems to be related to the post id which is 1. I manually changed the id to 8 because that was the next available id and the orm returns an empty array just like it does for the other posts. If I change it back to 1 the orm finds it and returns it. I can't see anything in the code that requires the post id and I'm not sure where all to look for the issue. Here's the code to view the post which runs when I click on a post title. I left in the debug and die lines so you can see where it's returning empty.
    public function action_view($slug)
     {
       // $post = Model_Post::find_by_slug($slug);
      $post = Model_Post::find_by_slug($slug, array('related' => array('user')));
      echo"<pre>";
      print_r($post);
      echo"</pre>";
      die();
        $this->template->title = $post->title;
        $this->template->content = View::forge('blog/view', array(
           'post' => $post,
        ));
     }
    

    Here is the user model
    <?php
    
    class Model_User extends \Orm\Model
    &#123;
     protected static $_has_many = array('posts');
     
     protected static $_properties = array(
      'id',
      'username',
      'password',
      'group',
      'email',
      'last_login',
      'login_hash',
      'profile_fields',
      'created_at',
      'updated_at'
     );
    
     protected static $_observers = array(
      'Orm\Observer_CreatedAt' => array(
       'events' => array('before_insert'),
       'mysql_timestamp' => false,
      ),
      'Orm\Observer_UpdatedAt' => array(
       'events' => array('before_save'),
       'mysql_timestamp' => false,
      ),
     );
    }
    

    And the post model
    <?php
    class Model_Post extends \Orm\Model
    &#123;
     protected static $_belongs_to = array('user');
     
     protected static $_properties = array(
      'id',
      'title',
      'slug',
      'summary',
      'body',
      'user_id',
      'created_at',
      'updated_at',
     );
    
     protected static $_observers = array(
      'Orm\Observer_CreatedAt' => array(
       'events' => array('before_insert'),
       'mysql_timestamp' => false,
      ),
      'Orm\Observer_UpdatedAt' => array(
       'events' => array('before_save'),
       'mysql_timestamp' => false,
      ),
     );
    
     public static function validate($factory)
     &#123;
      $val = Validation::forge($factory);
      $val->add_field('title', 'Title', 'required|max_length[255]');
      $val->add_field('slug', 'Slug', 'required|max_length[255]');
      $val->add_field('summary', 'Summary', 'required');
      $val->add_field('body', 'Body', 'required');
      $val->add_field('user_id', 'User Id', 'required|valid_string[numeric]');
    
      return $val;
     }
    
    }
    

    I tested the relationship with oil console as in the tutorial and it worked there.
    Any idea why or where else I should look to find the problem? Thanks
  • Are you using v1.1-RC1? There was a bug in there that made it only return a single object instead of all for plural relations. An update to the final v1.1 should fix that.
  • The welcome page shows version 1.1 I used the curl method to get the latest from git
    $ curl get.fuelphp.com/oil | sh
    $ oil create blog
    

    I did this just two days ago. Is this the newest version? Thanks
  • Ok, you're on 1.1 final. Nothing looks really wrong with this, could you add this after the fetch: echo DB::last_query();
    That should show us the query and possibly what's wrong.
  • These are the queries in order from the first post (test-post) and then the second post (this-is-another-test-post).
    SELECT `t0`.`id` AS `t0_c0`, `t0`.`title` AS `t0_c1`, `t0`.`slug` AS `t0_c2`, `t0`.`summary` AS `t0_c3`, `t0`.`body` AS `t0_c4`, `t0`.`user_id` AS `t0_c5`, `t0`.`created_at` AS `t0_c6`, `t0`.`updated_at` AS `t0_c7`, `t1`.`id` AS `t1_c0`, `t1`.`username` AS `t1_c1`, `t1`.`password` AS `t1_c2`, `t1`.`group` AS `t1_c3`, `t1`.`email` AS `t1_c4`, `t1`.`last_login` AS `t1_c5`, `t1`.`login_hash` AS `t1_c6`, `t1`.`profile_fields` AS `t1_c7`, `t1`.`created_at` AS `t1_c8`, `t1`.`updated_at` AS `t1_c9` FROM (SELECT `t0`.`id`, `t0`.`title`, `t0`.`slug`, `t0`.`summary`, `t0`.`body`, `t0`.`user_id`, `t0`.`created_at`, `t0`.`updated_at` FROM `posts` AS `t0` ORDER BY `t0`.`id` ASC LIMIT 1) AS `t0` LEFT JOIN `users` AS `t1` ON (`t0`.`user_id` = `t1`.`id`) WHERE (`t0`.`slug` = 'test-post') ORDER BY `t0`.`id` ASC SELECT `t0`.`id` AS `t0_c0`, `t0`.`title` AS `t0_c1`, `t0`.`slug` AS `t0_c2`, `t0`.`summary` AS `t0_c3`, `t0`.`body` AS `t0_c4`, `t0`.`user_id` AS `t0_c5`, `t0`.`created_at` AS `t0_c6`, `t0`.`updated_at` AS `t0_c7`, `t1`.`id` AS `t1_c0`, `t1`.`username` AS `t1_c1`, `t1`.`password` AS `t1_c2`, `t1`.`group` AS `t1_c3`, `t1`.`email` AS `t1_c4`, `t1`.`last_login` AS `t1_c5`, `t1`.`login_hash` AS `t1_c6`, `t1`.`profile_fields` AS `t1_c7`, `t1`.`created_at` AS `t1_c8`, `t1`.`updated_at` AS `t1_c9` FROM (SELECT `t0`.`id`, `t0`.`title`, `t0`.`slug`, `t0`.`summary`, `t0`.`body`, `t0`.`user_id`, `t0`.`created_at`, `t0`.`updated_at` FROM `posts` AS `t0` ORDER BY `t0`.`id` ASC LIMIT 1) AS `t0` LEFT JOIN `users` AS `t1` ON (`t0`.`user_id` = `t1`.`id`) WHERE (`t0`.`slug` = 'this-is-another-test-post') ORDER BY `t0`.`id` ASC
    I did some double checking on the id's. Changed the first post back to 8 again, left the second one as 2 and then the second one returns an object but the first one doesn't. Then changed the second one to 9 and now the third one returns an object.
    So it definitely seems to only be returning the lowest id, what ever the lowest id happens to be. I'm looking at those queries now and my eyes are going crossed lol, but I don't see anything different other than the value of slug.
  • It's a bug in the __callStatic() method. If you do it like this it'll work:
    $post = Model_Post::query()->where('slug', $slug')->related('user')->get_one();
    // or
    $post = Model_Post::find('first', array('where' => array('slug' => $slug), 'related' => array('user')));
    

    The problem is that the t0.slug where condition needs to go on the subquery. But as the __callStatic() stuff puts it into parenthesis it's not (sub-conditions always go onto the whole).
  • Thank you! That fixed it. I'm glad it was a bug because I just knew it was going to turn out to be something dumb I was doing wrong. I'll put it in the comments over at the tutorial.
  • it seems my post was removed for some reason.... But here is what I posted yesterday: Is it possible for someone to post me the finished project of Phils tutorial on admin creation?
    As my oil functionality doesnt seem to work on my linux server... I'm still trying to fix it but in the mean time I would like to work on it.

Howdy, Stranger!

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

In this Discussion