Update the line using idiorm and php

I have this function to update a record, but I cannot skip it and send me the message "The primary key ID is missing in the string or is null", how can I fix it?

public static function update_child($data)
{
    try
    {
        $update= ORM::for_table("dm_child",DM_TAG)
                        ->where_equal($data["id_child"]);

        $update -> set(array(
            "gender" => $data["gender"]
            "age_year" =>$data["year"]
            "age_month" => $data["month"]

        ));
        $update -> save();
    }
    catch(PDOException $ex)
    {
        ORM::get_db()->rollBack();
        throw $ex;
    }
}

      

+3


source to share


6 answers


Idiorm assumes that the primary key name is "id", which is not the case in your case. Therefore, you must explicitly specify it in Idiorm:

<?php
    ORM::configure('id_column_overrides', array(
    'dm_child' => 'id_child',
    'other_table' => 'id_table',
));

      



See Docs> Configuration .

+3


source


I just came across this issue 2 minutes ago. The real reason is that you forgot the id field in the request. demo:

$demo = ORM::for_table('demo')->select('field_test')->find_one($id);
$demo->field_test = 'do';
$demo->save();

      

You will receive an error message. change to:

$demo = ORM::for_table('demo')->select('field_test')->select('id')->find_one($id);

      



It will fix the problem.

Some advice in the docs: https://github.com/j4mie/idiorm/blob/master/test/ORMTest.php

/ ** * These next two tests are necessary because if you have select () ed some fields * but not the primary key, then the primary key is not available for * update / delete query - see question # 203. * We have to change the primary key here to something other than id

   * becuase MockPDOStatement-> fetch () always returns id. * /

+1


source


The answer is indeed the one provided by @iNpwd for changing the default "id" column name for queries for each table:

ORM::configure('id_column_overrides', array(
    'table_name'  => 'column_name_used_as_id',
    'other_table' => array('pk_1', 'pk_2') // a compound primary key
));

      

What bit me was that I recognized my query was WHERE, I change the values ORM::configure

. I was not in the correct file.

A deeper reference to the specific configuration of the ID column: http://idiorm.readthedocs.org/en/latest/configuration.html#id-column

+1


source


I've never used an idiom, so I can't guarantee that my answer will work for you, but from this page and in the Updating Posts section, we have an example that is similar, but slightly different from yours.

// The 5 means the value of 5 in the primary-key column
$person = ORM::for_table('person')->find_one(5);

// The following two forms are equivalent
$person->set('name', 'Bob Smith');
$person->age = 20;

// This is equivalent to the above two assignments
$person->set(array(
    'name' => 'Bob Smith',
    'age'  => 20
));

// Syncronise the object with the database
$person->save();

      

0


source


I'm sure I'll find out the reason for this, but let me tell you everything I understand at the moment and how I "fixed" it.

Here is the start of the idiorm save function :

public function save() {
    $query = array();
    // remove any expression fields as they are already baked into the query
    $values = array_values(array_diff_key($this->_dirty_fields, $this->_expr_fields));
    if (!$this->_is_new) { // UPDATE
        // If there are no dirty values, do nothing
        if (empty($values) && empty($this->_expr_fields)) {
            return true;
        }
        $query = $this->_build_update();
        $id = $this->id(true);

      

Right there on that last line, when you try to access $ this-> id , you get an exception:

throw new Exception('Primary key ID missing from row or is null');

      

$ this does not contain an id property. I'm not really sure how this is possible. The example given both on their home page and in the docs doesn't do anything special to fix this issue. Actually I copy them 1: 1 and still bring in the same error as you do.

So with all that said, I fixed this error by simply adding my own id:

$crop = ORM::for_table('SCS_Crop')->find_one($id);
$crop->id = $id;
$crop->Name = "Foo";
$crop->save();

      

0


source


This also happens when the id field name is ambiguous, eg. when joining two tables that have an identity column. This applies to referenced tables

Model::factory('tableOne')
->left_outer_join('tableTwo', array('tableOne.tableTwo_id', '=', 'tableTwo.id'))
->find_one($id);

      

In these cases, set the column to the id column of the parent table One to be accessed later on save. Make sure you also select other columns you need - for example by -> select ('*'):

Model::factory('tableOne')
->select('*')
->select('tableOne.id', 'id')
->left_outer_join('tableTwo', array('tableOne.tableTwo_id', '=', 'tableTwo.id'))
->find_one($id);

      

0


source







All Articles