【问题标题】:Update a row using idiorm and php使用 idiorm 和 php 更新一行
【发布时间】:2015-01-12 22:36:26
【问题描述】:

我有更新记录的功能,但我不能它失败并向我发送“行中缺少主键 ID 或为空”消息,我该如何解决?

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;
    }
}

【问题讨论】:

    标签: php mysql idiorm


    【解决方案1】:

    Idiorm 假定主键的名称是“id”,但在您的情况下并非如此。 因此,您必须将其显式指定为 Idiorm:

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

    Docs>Configuration

    【讨论】:

    • 是的,这也可能是原因,如果您使用 idiorm,请记住如果您的主 id 名称不是 'id' 时这样做!
    【解决方案2】:

    答案确实是@iNpwd 提供的,用于更改基于每个表的查询的默认“id”列名:

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

    让我无法识别我的查询的问题是我在哪里更改 ORM::configure 值。我不在正确的文件中。

    具体到 ID 列配置的更深链接:http://idiorm.readthedocs.org/en/latest/configuration.html#id-column

    【讨论】:

    • 这个回答很漂亮,非常感谢所有表格配置
    【解决方案3】:

    我 2 分钟前刚遇到这个问题。真正的原因是,您在查询中忘记了选择 id 字段。 演示:

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

    你会得到错误。 改为:

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

    它会解决问题。

    文档中的一些提示: https://github.com/j4mie/idiorm/blob/master/test/ORMTest.php

    /** * 需要接下来的两个测试,因为如果您选择()了一些字段, * 但不是主键,则主键不可用 * 更新/删除查询 - 见问题 #203。 * 我们这里需要把主键改成id以外的东西 * 因为 MockPDOStatement->fetch() 总是返回一个 id。 */

    【讨论】:

    【解决方案4】:

    我从来没有使用过 idiorm,所以不能保证我的答案对你有用,但是从这个 page 和“更新记录”下,我们有一个与你的相似但略有不同的例子。

    // 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();
    

    【讨论】:

    • 我也试过这个例子,但我得到了同样的结果:/
    • 也遇到了这个问题。
    • 你设置了主键列吗?我个人手动执行所有 SQL 语句(即“UPDATE table SET column_1 = '$value_safe'!”),其中 $value_safe = mysqli_real_escape_string($value)。无论如何,我会用可能与您无关的内容来分散您的注意力,但是诸如不设置主键、向唯一字段添加重复值等小事经常被忽略。
    【解决方案5】:

    我相信我会了解这背后的原因,但让我告诉你我目前所了解的一切,以及我是如何“修复”它的。

    这里是idiorm's 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);
    

    就在最后一行,当您尝试访问 $this->id 时,您会遇到异常:

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

    $this 不包含 id 属性。我不确定它是怎么做到的。给出on their homepagein the docs 的示例并没有做任何特别的事情来解决这个问题。事实上,我正在 1:1 复制它们,但仍然产生与您相同的错误。

    所以,说了这么多,我通过添加我自己的 id 来修复这个错误:

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

    【讨论】:

      【解决方案6】:

      id 字段名称不明确时也会发生这种情况,例如当连接两个表时都有一个 id 列。引用表就是这种情况

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

      在这些情况下,为父 tableOne 的 ID 列设置一个别名,以便以后在保存时访问它。确保您还选择了您需要的其他列 - 例如通过->选择('*'):

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

      【讨论】:

        【解决方案7】:

        如果表中的主键/字段名不是 id,则需要以下 id 列覆盖 默认 id (primary_key) 替换为其他 id 名称 (primary_key)

            ORM::configure('id_column_overrides', array(
                'user'  => 'user_id',
            ));
        
            $update = ORM::for_table('user')->find_one(1);
            $update->name = "dev";
            try{
                $update->save();
            }catch(Exception $e){
                echo $e;
            }
        
            print_r($update);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-10
          • 2017-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-22
          相关资源
          最近更新 更多