Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Composite Primary Key error
  • Hi,
    I'm using a composite key instead of primary key. Its client requirements I can't alter table :(.
    My table structure is below.
    meetingrooms_id, 
    languages_id,
    title,
    subtitle.

    Here first two columns is my composite key.
    I'm passing following values to the model.

    $meetingroomstranslations = Model_Meetingroomtranslation::forge(array(
    'meetingrooms_id' => $meetingrooms->id,
    'languages_id' => $value['lang_id'],
    'title' => $value['title'],
    'subtitle' => $value['subtitle'],
    ));

    Upon submit, I find the following error.
    OutOfBoundsException [ Error ]: Property "id" not found for Model_Meetingroomtranslation.

    Can anyone have idea how to handle it ?

    Thanks in advance.

  • HarroHarro
    Accepted Answer
    Sounds like you haven't defined the primary keys in your model?
  • Well,

    The above design works well for saving while doesn't work for delete.
    When I want to delete a record from parent table i.e
    if ($meetingroom = Model_Meetingroom::find($id)) {
    $meetingroom->delete();
    }

    I find the following error.

    Fuel\Core\FuelException [ Error ]: Primary key cannot be changed.


    Can you please help ?Thanks in advance.

  • Most common cause for that error is incorrect relation definition, where this error is not generated on the model object itself, but on a related object.

    Related objects should all have a belongs_to to Model_Meetingroom. If not, the ORM will try to detach the related records when you delete, which happens by setting the FK to NULL. Which can't happen, because for non "belongs_to" relations, the FK will be the PK, and you can't change that.
  • Well,

    The deletion works perfectly , means records from parent and child gets delete but
    also throws this exception :( .
    If I had some error in relation, the deletion shouldn't have worked.


  • Can you post the backtrace of the exception? It will show you where in the delete process it is generated.
  • This is the delete method.
    public function action_delete($id = null) {
        is_null($id) and Response::redirect('/admin/meetingrooms/');
    if ($meetingroom = Model_Meetingroom::find($id)) {
    $meetingroom->delete('meetingroomtranslation');
    Session::set_flash;
    } else {
    Session::set_flash
    }
    Response::redirect('admin/meetingrooms');
    }
  • You see it generates the exception in hasmany, indicating it's not failing on the Model_Meetingroom object itself, but on a related object.

    Check your has_many relations, the other side of each of them must be a belongs_to, with the correct key definitions. If not, the ORM will try to "unassign" the related object, and while doing that try to set the PK to NULL instead of the FK. Which will generate this exception.

Howdy, Stranger!

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

In this Discussion