Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Related Model_Soft cannot delete.
  • Hello.
    There are 3 models "User",  "Computer", and "OS".
    "Computer" related to "User" By user_id, and it has "os_id" to join "OS".
    I wont to (soft) delete a "Computer" record.

    ------------------------------------------------------
    "Computer" =[ many : 1 ]=> "User"
    "Computer" =[ 1:1 ]=> "OS"

    ------------------------------------------------------
    <Model Computer>
    ------------------------------------------------------
    class Model_Computer extends \Orm\Model_Soft {

        protected static $_soft_delete = array(
            'deleted_field' => 'delete_time',
        );

        protected static $_belongs_to = array(
            'user' => array(
                'model_to' => 'Model_User',
                'key_from' => 'user_id',
                'key_to' => 'id',
                'cascade_save' => false,
                'cascade_delete' => false
            ),
        );


        protected static $_has_one = array(
            'os' => array(
                'model_to' => 'Model_Os',
                'key_from' => 'os_id',
                'key_to' => 'id',
                'cascade_save' => false,
                'cascade_delete' => false
            ),
        );
    }

    ------------------------------------------------------
    <Controller>
    ------------------------------------------------------
    function action_delete($id=null)
    {

    // $id is a GEP parameter, user can set as he(she) want.
    // Restrict deletable scope to login admin shop data only.
    $shop_id = $login_admin->get_shop_id();

    $computer = Model_Computer::query()
                          ->related('user', array(
                                                         'where' => array(
                                                               array('user.shop_id', '=' $shop_id;)
                                                         )
                                                     )

                            )
                           ->get_one();

    $computer->delete();

    }

    ------------------------------------------------------
    <Result>
    ------------------------------------------------------

    Fuel\Core\FuelException [ Error ]: Primary key on model Model_Os cannot be changed.
    PKGPATH/orm/classes/model.php @ line 1131

    ------------------------------------------------------

    I did't set "os" ORM query builder "related".
    What's wrong?
    please help me...
  • HarroHarro
    Accepted Answer
    This is usually caused by incorrect definition of the relation.

    As a rule of thumb, the model that contains the foreign key should have the "belongs_to". So if your "Computer" model contains "os_id", it has the FK.

    The correct definition is therefore:
    OS has_many Computer
    Computer belongs_to OS
  • Thank you, I understand:)

Howdy, Stranger!

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

In this Discussion