Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
CRUD relation models
  • Hi, I can't seem to find an example of how the CRUD works with relating models... Has anyone an example of a simple crud between 2 relating models? Regards EDIT: Problem is that I use
    Auth::instance()->create_user()
    
    to create users, but I have another table which is related to the simpleusers table. So how would you combine it so when user is logged in, you can insert other properties in the second table? I wasn't sure if another model was needed for the simpleusers table (but as documented, its says you need to have both belongs_to and has_many for it to work) So I have teh following to models: user:
    <?php
    
    class Model_User extends Orm\Model &#123;
    
     protected static $_table_name = 'simpleusers';
     protected static $_properties = array('id', 'username', 'password', 'email', 'profile_fields', 'group', 'last_login', 'login_hash');
     
        $_has_many = array(
        'api' => array(
            'key_from' => 'id',
            'model_to' => 'Model_Api',
            'key_to' => 'user_id',
            'cascade_save' => true,
            'cascade_delete' => false,
        ));
    }
    

    api
    <?php
    
    class Model_Api extends Orm\Model &#123;
    
     protected static $_table_name = 'simpleusers_api';
     protected static $_properties = array('id', 'api_id', 'api_key', 'user_id');
        
        $_belongs_to = array(
        'user' => array(
            'key_from' => 'user_id',
            'model_to' => 'Model_User',
            'key_to' => 'id',
            'cascade_save' => true,
            'cascade_delete' => false,
        ));
    )
    
  • updated
  • Relating models is very easy:
    $api = Model_Api::find(5); // fetch api with pk = 5
    
    $user->apis[] = $api;
    $user->save();
    
    // after saving the api relation to the user has been saved and reindexed to have array key = PK (thus 5)
    // if we want to delete it again:
    unset($user->apis[$api->id]); // or just 5 instead of $api->id
    $user->save();
    
    // for singular relations (HasOne, BelongsTo) its similar:
    $api->user = $user; // relate api to user
    $api->save();
    $api->user = null; // unrelate the user
    $api->save();
    
  • Well in the mean time I checked out the stationwagon script and would this also work (regarding cascade deleting for example)? So as when someone registers the user needs to fill teh apiID and key in the same form.
    So after validation I do teh following:
    if (Auth::instance()->create_user($val->validated('username'), $val->validated('password'), $val->validated('email'), 1))
                {
        Auth::instance()->login($val->validated('username'), $val->validated('password'));
        
        $api = new Model_api(array(
         'api_id' => $val->validated('api_id'),,
         'api_key' => $val->validated('api_key'),,
         'user_id' => $this->user_id,
        ));
        
        $api->save();
        
                    Session::set_flash('success', 'Thanks for registering!');
                    Response::redirect('/');
                }
    

    Is this correct? Problem is I can't test it out for now as I'm at work.
  • That should work without problems.
  • k, FF fireftp is usefull when you don't have permission to install exe ftpclients :D So I'm finally able to upload my codes. Now I tried adding the information to my second table but failed. This is the code I'm using
    if (Auth::instance()->create_user($val->validated('username'), $val->validated('password'), $val->validated('email'), 1))
                {
        Auth::instance()->login($val->validated('username'), $val->validated('password'));
        
        $api = new Model_api(array(
         'api_id' => $val->validated('api_id'),
         'api_key' => Model_Api::encryptApiKey($val->validated('api_key')),
         'user_id' => $this->user_id,
        ));
        
        $api->save();
        
        Model_Api::saveCharacters($api_id, $api_key);
        
                    Session::set_flash('success', 'Thanks for registering!');
                    Response::redirect('/');
                }
    

    With this I get the error that user_id may not be NULL. Is it cause the actual user isn't instantiated/created till after the "if" clausule? Which means the id doesnt exist?
  • Didn't notice that, is this code in your controller? And if so, why are you expecting your controller to have a user_id property?
  • well as i need to save the api keys and set the FK (user_id) to id of the user table.
    But as the user is created by the AUTH create method, I can't really user any other method, no?
  • I'm not exactly sure how that answered my question, I didn't ask why you need it (that I userstand). But to explain it: for some reason your example seems part of a controller that you expect to magicly know the user id that was just created without there being any reason for the controller to actually have that property.
    'user_id' => $this->user_id,
    

    The create user method does however return the new id, thus by changing your code to the following might work:
    // assign the user ID variable while creating the user:
    if ($user_id = Auth::instance()->create_user($val->validated('username'), $val->validated('password'), $val->validated('email'), 1))
                {
        Auth::instance()->login($val->validated('username'), $val->validated('password'));
        
        $api = new Model_api(array(
         'api_id' => $val->validated('api_id'),
         'api_key' => Model_Api::encryptApiKey($val->validated('api_key')),
         'user_id' => $user_id, // <== USER ID
        ));
        
        $api->save();
        
        Model_Api::saveCharacters($api_id, $api_key);
        
                    Session::set_flash('success', 'Thanks for registering!');
                    Response::redirect('/');
                }
    

Howdy, Stranger!

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

In this Discussion