Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to save new record with multiple primary keys
  • Dear sir,

    I have a model defined with multiple primary keys.

    I can not insert new record.

    I refer to following.


    However, I can not insert new record.

    What I did is....

    $model = Model_A::find_by_targetid($targetid,array('related'=>array('something'=>$condition))
    $record = new Model_something(array('a_id'=>$model->id,'no'=>$no));
    $record->setAttributes('create');
    $model->something[] = $record;

    Is there something wrong?

  • I solve myself.


    $model->something['['.$model->id.']['.$no.']'] = $record;

    But, I am not sure of this is right.
  • Harro VertonHarro Verton
    Accepted Answer
    You don't need to add the foreign key, the ORM will do that for you.

    $model = Model_A::find_by_targetid($targetid, array('related'=>array('something'=>$condition));
    $record = Model_something::forge(array('no'=>$no));
    $record->setAttributes('create');
    $model->something[] = $record;
    $model->save();

    Should work fine.
  • I see.

    I did not know that it works that way.

    Thank you :)
  • Sorry again...

    To delete a record, I am doing this way.

    $model->something['['.$a_id.']['.$no.']']->delete();

    Is right way?
  • If you want quick and direct access to the record, you could do it like that, yes.

    I have to say that personally, I always try to avoid compound keys, and go for a single auto-increment 'id' column als primary key. So I don't have a lot of experience with it.
  • Harro VertonHarro Verton
    Accepted Answer
    There is btw a helper method available:

    $fk = Model_Something::implode_key(array('a_id' => $a_id, 'no' => $no));
    $model->something[$fk]->delete();

  • Okay, there is an option for deleting multi key record.

    Possibly, I may use your helper method since it is available.

    Thanks for that.

    I also want to avoid compound keys, but, I can not change the database schema since another system is using it.

    Just let you know.

    Model_Something::implode_key() is wong.
    Model_Something::implode_pk() is correct.
  • Sorry again.

    Here I have another multiple primary key and it seems not working.

    $model = Model_A::find_by_targetid($targetid,array('related'=>array('something'=>$condition))
    $record = Model_something::forge(array('key'=>$key,'no'=>$no));
    $record->setAttributes('create');
    $model->something[] = $record;

    The difference is there are 3 keys are defined as primary key.

  • What exactly doesn't work?

    If Model_Something has a compound primary key with 3 columns, how many are foreign keys? In the forge() array, you should only pass the data for columns that are not foreign keys.

    Also, make sure that your relationship is defined correctly, so if Model_Something has two foreign keys linking it to Model_A, they should both be defined in the has_many and belongs_to definition.
  • Okay.
    I see.
    There is only 1 foreign key, but, 3 keys are defined as primary key.

    So, it seems it should work.
    What exactly doesn't work is I cannot save the data since it gives me mysql error message like duplicate keys.

    So, I did following before save.

    print_r($model->something);

    It showed [123][1][0] bla bla bla.

    $key is set to 10.
    $no is set to 1.

    So, it should show like [123][10][1] bla bla bla.

    Am I right?

    Well...I will double check if relationship is defined correctly.





  • If [123] is the foreign key, and defined as only foreign key in the relationship, then you should indeed pass the other two values to forge().

    Can you do:

    $model = Model_A::find_by_targetid($targetid,array('related'=>array('something'=>$condition))
    $record = Model_something::forge(array('key'=>$key,'no'=>$no)); $record->setAttributes('create');
    $model->something[] = $record;
    print_r($record);
    try:
    {
        $model->save();
    }
    catch \Exception $e:
    {
        # pass
    }
    print_r($record);
    print_r($model->something);

    En compare the _data and _original_data properties of the objects, what happens to the value of the three key columns?
  • Hello there,

    Sorry for late reply.

    I just tried you debug code and got nothing added.

    The keys have following value.
    $key = 10;
    $no = 1;

    $record has following.

    Model_Something Object
    (
        [_is_new:protected] => 1
        [_frozen:protected] => 
        [_sanitization_enabled:protected] => 
        [_data:protected] => Array
            (
                [key] => 10
                [no] => 1
                [attributes] => a:0:{}
                [created_at] => 2017-03-21 10:19:49
                [last_updated] => 2017-03-21 10:19:49
                [duration] => 600
       )
        [_custom_data:protected] => Array()
        [_original:protected] => Array()
        [_data_relations:protected] => Array()
        [_original_relations:protected] => Array()
        [_reset_relations:protected] => Array()
        [_disabled_events:protected] => Array()
        [_view:protected] => 
        [_iterable:protected] => Array()
    )
  • Then $model->something has following.

    Array
    (
     [[123][2][0]]=>Model_Something Object
      (
       [_is_new:protected]=>1
       [_frozen:protected]=>
       [_sanitization_enabled:protected]=>
       [_data:protected]=>Array
        (
         [last_updated]=>0
         [duration]=>0
        )
       [_custom_data:protected]=>Array()
       [_original:protected]=>Array()
       [_data_relations:protected]=>Array(([customer]=>)
       [_original_relations:protected]=>Array(([customer]=>)
       [_reset_relations:protected]=>Array()
       [_disabled_events:protected]=>Array()
       [_view:protected]=>
       [_iterable:protected]=>Array()
      )
  •  [0]=>Model_Something Object
      (
       [_is_new:protected]=>1
       [_frozen:protected]=>
       [_sanitization_enabled:protected]=>
       [_data:protected]=>Array
        (
         [key]=>10
         [no]=>1
        )
       [_custom_data:protected]=>Array()
       [_original:protected]=>Array()
       [_data_relations:protected]=>Array()
       [_original_relations:protected]=>Array()
       [_reset_relations:protected]=>Array()
       [_disabled_events:protected]=>Array()
       [_view:protected]=>
       [_iterable:protected]=>Array()
      )
     []=>Model_Something Object
      (
       [_is_new:protected]=>
       [_frozen:protected]=>
       [_sanitization_enabled:protected]=>
       [_data:protected]=>Array
        (
         [last_updated]=>0
         [duration]=>0
         [customer_id]=>123
        )
       [_custom_data:protected]=>Array()
       [_original:protected]=>Array
        (
         [last_updated]=>0
         [duration]=>0
        )
       [_data_relations:protected]=>Array([customer]=>)
       [_original_relations:protected]=>Array([customer]=>)
       [_reset_relations:protected]=>Array()
       [_disabled_events:protected]=>Array()
       [_view:protected]=>
       [_iterable:protected]=>Array()
      )
    )
  • Oh...possibly...

    $model->something already has some related data and so I got "Integrity constraint violation: 1062 Duplicate entry" when saving data.

    So, I just give up using relation and what I did is just add a record without using relation.

Howdy, Stranger!

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

In this Discussion