Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Setting up self referencing models
  • The docs on this area is pretty sparse at the moment so hoping someone can point me in the right direction. I'm trying to set up some categories for a web store i'm building. We need these categories to have an unlimited depth of parents.
    Currently I have my table like this:
    CREATE TABLE `categories` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `title` varchar(255) NOT NULL,
      `slug` varchar(255) NOT NULL,
      `category_intro` text NOT NULL,
      `parent_id` int(10) NOT NULL,
      `created_at` int(10) NOT NULL,
      `updated_at` int(10) NOT NULL,
      `active` tinyint(1) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
    

    Where 'parent_id' would set which parent it would belong to. A (int)0 would obviously be a root category with no parent. My category model is setup currently like this:
    <?php
    
    class Model_Category extends Model
    {
     protected static $_properties = array(
      // condensed this array to only show the parent_id setup.
      'parent_id' => array(
       'validation' => array('required'),
       'label' => 'Parent Category',
       'form' => array(
        'type' => 'select'
       )
      ),
     );
    
     protected static $_observers = array( // condensed );
     
     protected $_belongs_to = array(
         'category' => array(
             'key_from' => 'parent_id',
             'key_to' => 'id',
             'cascade_save' => false
         )
     );
     
     protected $_has_many = array(
         'category' => array(
             'key_from' => 'id',
             'key_to' => 'parent_id',
             'cascade_save' => false
         )
     );
    }
    

    My first attempt at adding root categories, gives the following error:
    ErrorException [ Error ]: Access to undeclared static property: Model_Category::$_belongs_to
    
    PKGPATH/orm/classes/model.php @ line 341
    

    Any pointers on what i'm doing wrong?! Cheers
  • The $_has_many & $_belongs_to properties should be static, exactly as the error says it is expecting them.
  • Eurrggh.. Prime example of why not to code and post late at night. (sorry about that!) Should have spotted that. Does lead to a second question though now I got that working. Is it possible at all to define which relationship you wish to pull in this situation.
    I'm currently doing a listing page and would just like to know the first parent category for the current category. With the model how it is at the moment, I'm (correctly) getting all the results from both the has_many and the belongs_to relationships but I only want the belongs_to relationship to get the parent.
    Is this possible at all?
    I've had a poke around the code and the forums / docs / google to see about how to potentially alias the relationship name, I saw a reference to a "name" variable within the relationship classes in the ORM package and tried setting them up but to no avail and also tried setting up the relationships like this:
     protected static $_belongs_to = array(
         'parent' => array(
          'model_to' => 'category',
             'key_from' => 'parent_id',
             'key_to' => 'id',
             'cascade_save' => false
         )
     );
     
     protected static $_has_many = array(
         'children' => array(
          'model_to' => 'category',
             'key_from' => 'id',
             'key_to' => 'parent_id',
             'cascade_save' => false
         )
     );
    

    and then calling it like so:
      $listing = Model_Category::find('all', array(
       'related' => array(
        'parent' => array(
        )
       )  
      ));
    

    Is it possible to achieve this or is it just a case of getting all the data and having to filter it manually? cheers!
  • I'm not really sure what you're asking.
    ...but I only want the belongs_to relationship to get the parent.
    Is this possible at all?
    That is exactly what the belongs-to relationship does. And no relation is ever fetched unless you explicitly include it in the fetch (eager-loaded) or request it later (lazy-loaded). If you just want to get the parent you can do it like your example, or a more readable query:
    $listing = Model_Category::query()->related('parent')->get();
    

    Both will just fetch the parent. If you think they are both fetched, the belongs-to as well as the has-many, you are probably lazy-loading them by accident. You can see a few examples and explanation about the concepts of eager- and lazy-loading in the docs.

Howdy, Stranger!

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

In this Discussion