Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Must tables in ORM have an 'id' column?
  • I want a table with a composite primary key consisting of two foreign keys.. There is no point in having an 'id' column... 
    But, the model does not let me save. It gives a  Property "id" not found error. It looks for the property 'id' even though it is not defined in the model. How can I solve this without having to add an id column to the table??

  • You haven't defined the primary key in your model. If you don't, it defaults to "id".
  • when in a many to many relationship, does the $_many_many have to go on both related models? It seems obvious, but the documentation does not explicitly mention this.

    E.g.:
    Model_Student (M)  - - - - - - - - - - - - - - - - - - - -  (M) Model_Subject
    $_many_many = array('subjects)                       $_many_many = array('students')

    ...and, the table relating both: "students_subjects" (lower in strcmp first)?
    is that correct?
  • You always define a relation both ways.

    As for the table name, I'm not sure, I always explicitly define it. Just try, if it works differently it will tell you the name of the missing "through" table.
  • Ok.. I did that..

            protected static $_many_many = array(
                'tmusers' => array(
                    'key_from' => 'id',
                    'key_through_from' => 'tmgroup_id',
                    'table_through' => 'tmgroups_tmusers',
                    'key_through_to' => 'tmuser_id',
                    'model_to' => 'Model_Tmuser', 
                    'key_to' => 'id',
                    'cascade_save' => true,
                    'cascade_delete' => false,
                ));


    //---------------------------------------------------------------------------------//

            protected static $_many_many = array(
                'tmgrops' => array(
                    'key_from' => 'id',
                    'key_through_from' => 'tmuser_id',
                    'table_through' => 'tmgroups_tmusers',
                    'key_through_to' => 'tmgroup_id',
                    'model_to' => 'Model_Tmgroup', 
                    'key_to' => 'id',
                    'cascade_save' => true,
                    'cascade_delete' => false,
                ));

    //-------------------------------------------------------------------------------//

    Now I'm running this query:

    check_isgroupmember($group_id, $user_id) {
       $result = Model_Tmgroup::query()
                    ->where('id', '=', $group_id)
                    ->related('tmusers')
                    ->where('id', '=', $user_id)
                    ->count();
    ;
       return $result;
    }

    I want all tmusers that are members of tmgroup.id = $group_id

    However, it retuns all tmgroups whose tmgroup.id = $userid...
    It seems that the query interprets 'id' in ( ->where('id', '=', $user_id) ) as the tmgroup.id, and not tmuser.id as it is intended..

    Thanks..
  • HarroHarro
    Accepted Answer
    You need to tell the where() which id column you mean:

    check_isgroupmember($group_id, $user_id) {
       $result = Model_Tmgroup::query()
                    ->where('id', '=', $group_id)
                    ->related('tmusers')
                    ->where('tmusers.id', '=', $user_id)
                    ->count();
    ;
       return $result;
    }

Howdy, Stranger!

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

In this Discussion