Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Model query() function looks for column not defined in model
  • I have the following code (idk if i'm using it the right way or not):

    $query = Model_Tmgroup_Member::query()
                    ->select('*')
                    ->where('tmgroup_id', '=', $group_id)
                    ->where('tmuser_id', '=', $user_id);
    return $query->get();


    The model only has two columns ( the ones inside the ->where() )...
    However, when running the query, it also looks for the column 'id' even though it does not exist in the model. The model had an 'id' column, which I deleted though.
    How can I run this query correctly?
  • HarroHarro
    Accepted Answer
    Every ORM Model must have a primary key, and if you haven't set one, the model defaults to "id".

    What is your intention with this model? This seems to be a relation table in a many-to-many relation between users and groups, which the ORM supports without having to define a model for that table?
  • Yes.. This is a table that relates two other tables in a many to many...
    I think I defined the primary keys. This is the migration:

    public function up()
    {
    \DBUtil::create_table('tmgroup_member', array(
    'tmgroup_id' => array('constraint' => 11, 'type' => 'int'),
    'tmuser_id' => array('constraint' => 11, 'type' => 'int'),

    ), array('tmgroup_id', 'tmuser_id'),
                       true,                //if_not_exists
                       'InnoDB',            //engine
                       'utf8_unicode_ci',   //charset
                    
                    // foreign keys
                    array(
                        array( 'constraint' => 'FK_tmgroupMember_tmgroup',
                               'key'        => 'tmgroup_id',
                               'reference'  => array('table' => 'tmgroup', 'column' => 'id'),
                               'on_update'  => 'CASCADE',
                               'on_delete'  => 'CASCADE',
                            ),
                        array( 'constraint' => 'FK_tmgroupMember_tmuser',
                               'key'        => 'tmuser_id',
                               'reference'  => array('table' => 'tmuser', 'column' => 'id'),
                               'on_update'  => 'CASCADE',
                               'on_delete'  => 'CASCADE',
                            ),
                    ));
                
    }



    I read the documentation for may to many... that's the way to go I am guessing?
  • If you want to use the relation properly using ORM. And while you're at it, remove the constraints, ORM doesn't release like those on foreign keys.

    The ORM is smart enough not to join the child table if you run a query on a foreign key.
  • I am not sure I understand what you are saying.. So, I should remove the FK constraints? That is, no FK defined in DB?
  • HarroHarro
    Accepted Answer
    Correct.

    Depending on the operation performed, the ORM sometimes sets a column to NULL before deleting a record, which fails in a spectacular way if you have constraints.

    But you can leave them on, see if you run into this problem, and remove them only if you do?

Howdy, Stranger!

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

In this Discussion