Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Primary key on model Model_Content_Type cannot be changed.
  • I have read similar posts but none explain why i am getting this error.

    I have the following classes: slide, content, type.

    I am getting this error when i try to delete a Slide:  $slide->delete(); (The error is actually caused by $content->delete())

    A Slide has many Content. A Content has one Type. Here are my relations:

    class Model_Content extends \Orm\Model
    {

        protected static $_has_one = array(
            'type' => array(
                'key_from' => 'type_id',
                'model_to' => 'Model_Content_Type',
                'key_to' => 'id',
                'cascade_save' => false,
                'cascade_delete' => false,
            ),
        );


        protected static $_belongs_to = array(
            'slide' => array(
                'key_from' => 'slide_id',
                'model_to' => 'Model_Slide',
                'key_to' => 'id',
                'cascade_save' => true,
                'cascade_delete' => true,
            ),
        );

    }


    class Model_Slide extends \Orm\Model
    {
        protected static $_has_many = array(
            'contents' => array(
                'key_from' => 'id',
                'model_to' => 'Model_Content',
                'key_to' => 'slide_id',
                'cascade_save' => true,
                'cascade_delete' => true,
                'conditions' => array(
                    'order_by' => array(
                        'display_order' => 'ASC'
                    )
                ),

            ),

        );
    }

    class Model_Content_Type extends \Orm\Model
    {

        protected static $_belongs_to = array(
            'content' => array(
                'key_from' => 'id',
                'model_to' => 'Model_Content',
                'key_to' => 'type_id',
                'cascade_save' => false,
                'cascade_delete' => false,
            ),
        );
    }



    If I remove the has_one type, from Content, then the delete works. but then i cannot access the relation with $content->type->title for example.

    I have gone around in circles redefining the relations etc but it makes no sense as to why the primary key on Model_Content_Type is being updated..!?

    I sincerely hope someone can shed some light and make this understandable. Thanks.
  • I might add: The delete does actually work, and $slide->delete() does delete its contents (and the contents related data) etc. Its just that the error shows!
  • So. Update.

    I delved into the ORM class "model.php", and found what i think is causing the issue described above.

    there is some duplicate code, that performs exactly the same loop twice, once before a delete and once after. i think the relationships are broken after the forst loop, the second one barfs.  I may be wrong but ive commented out the second lot of code and my errors are gone, and all is working as expected.

    UPDATE: I was incorrect. the code is not exactly the same.
  • My find was incorrect, there is a minor difference in the code. Tired annoyed brain.

    So why, when i am doing even a $content->delete(), does the error "Primary key on model Model_Content_Type cannot be changed." happen...

    Content has a type_id, and a relation set to _has_one. 

    A _belongs_to on Type matters not, still get the error.

    If i remove the _has_one relation on Content, the error goes away.  But then i cannot access $content->type->whatever...

    Confused!
  • Makes me think that I should have a "types" table, then a content_types pivot table, which is just a map of content_id to type_id, even though content can only ever have one (hence type_id in contents table).

    At least the delete would work that way, so Content could _has_one Type, and delete it when Content gets deleted.  This is even odder because cascade_* are set to false.

    Am I really confused here?
  • MIssed it.

    removed the relation of content to type entirely. Content still has a type_id.

    Created a function in Content called Type() which will create a Type object from this->type_id and return its title. woo.

    I normally set relations like in the OP and have never had this issue, maybe i'm missing something so stupid, i don't know, but i've finished an entire thread to myself. hungry, must be.
  • HarroHarro
    Accepted Answer
    You can not have a PK that is also an FK.

    If you disconnect a relation (because you delete the parent), the FK will be set to NULL, which is not an allowed operation for a PK.

Howdy, Stranger!

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

In this Discussion