Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Right ORM model class names
  • I don't quite understand how to properly name ORM models? I have following relation: polls has many poll_options. Table names are "polls" and "poll_options". I have created 2 ORM models: 1. Poll model
    class Model_Poll extends \Orm\Model
    {
     protected static $_properties = array(
      'id',
      'title',
      'created_at'
     );
     
     protected static $_has_many = array(
         'polloptions' => array(
             'key_from' => 'id',
             'model_to' => 'Model_Poll_Option',
             'key_to' => 'poll_id',
             'cascade_save' => true,
             'cascade_delete' => false,
         )
     );
    }
    

    2. Pool_option model
    class Model_Poll_Option extends \Orm\Model
    {
     protected static $_properties = array(
      'id',
      'poll_id',
      'title'
     );
     
     protected static $_belongs_to = array(
         'poll' => array(
             'key_from' => 'poll_id',
             'model_to' => 'Model_Poll',
             'key_to' => 'id',
             'cascade_save' => true,
             'cascade_delete' => false,
         )
     );
    }
    

    This would fail when calling for $poll->pool_options with error message: "Fuel\Core\FuelException [ Error ]: Related model not found by Has_Many relation "poll_options": Model_Poll_Option". I figured out the Model_Pool_Option model naming is wrong. But can't figure out how to properly name it? Tried Model_Pool_option... doesn't work either. At the end I have tried my models like this and it is working. 1. Pool model
    class Model_Poll extends \Orm\Model
    {
     protected static $_properties = array(
      'id',
      'title',
      'created_at'
     );
     
     protected static $_has_many = array(
         'polloptions' => array(
             'key_from' => 'id',
             'model_to' => 'Model_Polloption',
             'key_to' => 'poll_id',
             'cascade_save' => true,
             'cascade_delete' => false,
         )
     );
    }
    

    2. Poll_option model
    class Model_Polloption extends \Orm\Model
    {
     protected static $_table_name = 'poll_options';
    
     protected static $_properties = array(
      'id',
      'poll_id',
      'title'
     );
     
     protected static $_belongs_to = array(
         'poll' => array(
             'key_from' => 'poll_id',
             'model_to' => 'Model_Poll',
             'key_to' => 'id',
             'cascade_save' => true,
             'cascade_delete' => false,
         )
     ); 
    }
    

    I would like to understand why the above naming "Model_Pool_Option" fails and how to properly fix that?
  • This is the way FuelPHP's cascading filesystem works. The namespace of the class and the class name together determine where in the filesystem this class should be stored. When determining this, underscores will be translated to directory separators In case of Model_Poll_Option, in app, it should be stored in a file called app/classes/model/poll/option.php, because of the underscore between Poll and Option.
  • have any opinion / advice to fix this problem?i have exact same problem , my table have underscore letter 
  • There is no direct relation between model names and table names, you can just specify the table that belongs to the model in the $_table_name property. So you can name models whatever you want.

    I use the Model sub-namespace for my models, instead of the underscore, and use a single name for the model itself. So in the example above my model would have been called \Model\Pooloption instead of Model_Pooloption.

Howdy, Stranger!

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

In this Discussion