Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Having trouble relating has_many and belongs_to
  • So I have two ORM models. user and user_network. I keep getting a failure when I try to save a new Model_User Fuel\Core\Fuel_Exception [ Error ]: Related model not found by Has_Many relation "networks": Model_User_Network
    the primary key for the users table is in the user_networks table under the column name "user_id". I don't know what I'm doing wrong trying to relate these two. Any ideas. The Models are as follows. under fuel/app/classes/model/
    class Model_User extends Orm\Model { protected static $_properties = array('id', 'first', 'last', 'email', 'password',
    'attempts', 'lockout', 'created_at', 'updated_at', 'time'); protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array('before_insert'),
    'Orm\Observer_UpdatedAt' => array('before_save'),
    ); protected static $_has_many = array(
    'networks' => array(
    'model_to' => 'Model_User_Network',
    'key_from' => 'id',
    'key_to' => 'user_id',
    'cascade_save' => false,
    'cascade_delete' => false)
    );
    }
    and the second model is under fuel/app/classes/model/user/networks class Model_User_Network extends Orm\Model { protected static $_properties = array('id', 'user_id', 'network_id', 'created_at', 'updated_at');
    protected static $_observers = array(
    'Orm\Observer_CreatedAt' => array('before_insert'),
    'Orm\Observer_UpdatedAt' => array('before_save'),
    ); protected static $_belongs_to = array(
    'user' => array(
    'key_from' => 'id',
    'model_to' => 'Model_User',
    'key_to' => 'user_id',
    'cascade_save' => false,
    'cascade_delete' => false,
    ));
  • **Updated
    If I move the model Model_User_Network into the same directory namespace as Model_User, and rename the model it will work fine. Is this a bug? If not how do you work around this.
  • No, not a bug. If the model you relate to is in another namespace, then define the namespace.
    protected static $_has_many = array(
        'networks' => array(
        'model_to' => '\\namespace\\Model_User_Network',
        'key_from' => 'id',
        'key_to' => 'user_id',
        'cascade_save' => false,
        'cascade_delete' => false
    )
    
  • Oh I see. Thanks!
  • Harro Verton wrote on Friday 1st of July 2011:
    No, not a bug. If the model you relate to is in another namespace, then define the namespace.
    protected static $_has_many = array(
        'networks' => array(
        'model_to' => '\\namespace\\Model_User_Network',
        'key_from' => 'id',
        'key_to' => 'user_id',
        'cascade_save' => false,
        'cascade_delete' => false
    )
    

    I still can't get it to work even with the following code, maybe I'm not understanding the namespace
    protected static $_has_many = array(
        'networks' => array(
        'model_to' => '\\user\\Model_User_Network',
        'key_from' => 'id',
        'key_to' => 'user_id',
        'cascade_save' => false,
        'cascade_delete' => false
    )
    
  • Think I understood you incorrectly. If your model is called Model_User_Network, and is in app, it must be in a file called network.php, stored in app/classes/model/user in order to be autoloaded.
  • Harro Verton wrote on Friday 1st of July 2011:
    Think I understood you incorrectly. If your model is called Model_User_Network, and is in app, it must be in a file called network.php, stored in app/classes/model/user in order to be autoloaded.

    Ok, the problem was that the file that holds the model was named networks.php plural not singular. When I changed it to network.php, it worked fine. Now I did generate the model using oil and I think I must have run
    oil g model user_networks
    

    instead of
    oil g model user_network
    

Howdy, Stranger!

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

In this Discussion