php - 如何从 PHP 向 Postgres 插入 null

标签 php sql postgresql

我一直在开发一个项目管理系统,并使用 PHP 和 PostgreSQL 8.4 以及模型- View - Controller 对其进行编码。

为了让大家了解具体情况,我将从根本上进行解释(尽可能简短)。

我有一个名为 Actividad 的类,它扩展了数据库抽象类来执行查询。

    class Actividad extends AbstraccionBD
    {      
    
    /**
     * Constructor de la clase
     *
     * @param String  $nom  Nombre 
     * @param String  $des  Descripción
     * @param String  $fi   Fecha de inicio
     * @param String  $fc   Fecha de culminación
     * @param Float   $pon  Ponderación
     * @param Boolean $crit Crítica
     * @param String  $le   Lugar Ejeucución
     * @param String  $rec  Recursos
     * @param Integer $pe   Presupuesto estimado
     * @param String  $est  Estado
     * @param Integer $cal  Calificación
     * @param Integer $peje Presupuesto ejecutado
     * @param Integer $asis Asistencia
     */
    public function __construct($nom, $des, $fi, $fc, $pon, $crit, $le, $rec, 
                                $pe, $est, $cal, $peje, $asis)
    {

        //Asignamos todos los valores especificados al 
        //objeto
        $this->nombre = $nom;
        $this->descripcion = $des;
        $this->fecha_inicio = $fi;
        $this->fecha_culminacion = $fc;
        $this->ponderación = $pon;
        $this->es_critica = $crit;
        $this->lugar_ejecucion = $le;
        $this->recursos = $rec;
        $this->presupuesto_est = $pe;
        $this->estado = $est;
        $this->calificacion = $cal;
        $this->presupuesto_ejec = $peje;
        $this->asistencia = $asis;
        
    }

    public function insertarObjeto()
    {
        //Construimos el query de inserción de datos en la tabla
        //actividad; nótese que se llama al procedimiento almacenado
        //sga.max_num_actividad(int,int) para determinar el número
        //de la actividad que se esta insertando.
        $this->_query = 
            "INSERT INTO sga.actividades " . 
                "(proyecto, ejec_proyecto, num_actividad, nombre, " . 
                "descripcion, fecha_inicio, fecha_culminacion, " .
                "ponderacion, critica, lugar_ejecucion, recursos, " . 
                "prepuesto_estimado, estado, calificacion, " . 
                "presupuesto_ejecutado, asistencia) " . 
            "VALUES " . 
                "($this->proyecto, $this->ejec_proyecto, " . 
                "sga.max_num_act($this->proyecto, $this->ejec_proyecto) + 1, " . 
                "'$this->nombre', " . 
                "'$this->descripcion', '$this->fecha_inicio', " . 
                "'$this->fecha_culminacion', $this->ponderación, " . 
                "$this->es_critica, '$this->lugar_ejecucion', " . 
                "'$this->recursos', $this->presupuesto_est, " . 
                "'$this->estado', $this->calificacion, " . 
                "$this->presupuesto_ejec, $this->asistencia);";
                
        //Ejecutamos el query de inserción
        $this->ejecutarQuery();
    }

(项目要求我每行保留 80 个字符。)

表定义(不考虑 FK):

CREATE TABLE sga.actividades
(
    proyecto integer NOT NULL,
    ejec_proyecto integer NOT NULL,
    num_actividad smallint NOT NULL,
    nombre character varying(150),
    descripcion character varying(500),
    fecha_inicio date NOT NULL,
    fecha_culminacion date NOT NULL,
    ponderacion numeric (3,2) NOT NULL DEFAULT 0.00,
    critica boolean NOT NULL DEFAULT FALSE,
    lugar_ejecucion character varying(100),
    recursos character varying(250),
    prepuesto_estimado integer,
    estado sga.estados_actividad NOT NULL, -- Dominio estados_actividad
    calificacion integer,
    presupuesto_ejecutado integer,
    asistencia smallint, 
    CONSTRAINT actividad_pkey
        PRIMARY KEY(proyecto, ejec_proyecto, num_actividad)
);

现在,我想做的是传递模型中的值,像这样的“东西”:

    $a = new Actividad('Actividad1','DescA1', '14-07-14','14-07-14',0.00, 
                'false',null,null,0,'ACT',null, null, null);

    //Dont worry im using __set method
    $act->proyecto = 1;
    $act->ejec_proyecto = 1;

    $a->insertarObjeto();        

正如您所看到的,我在构造函数中传递了一些 NULL 值,因为在数据库中这些值可以为 null,到目前为止一切都很酷。

当我尝试运行它时,我收到以下查询:

INSERT INTO sga.actividades 
    (proyecto, ejec_proyecto, 
    num_actividad, nombre, 
    descripcion, fecha_inicio, 
    fecha_culminacion,
    ponderacion, critica, 
    lugar_ejecucion, recursos, 
    prepuesto_estimado, estado, 
    calificacion, presupuesto_ejecutado, 
    asistencia) 
VALUES 
    (1, 1, sga.max_num_act(1, 1) + 1, 
    'Actividad1', 'DescA1', '14-07-14', 
    '14-07-14', 0, false, '', '', 0, 'ACT', , , );

这是我的问题:这个查询永远不会运行,因为我使用 PHP 中的 NULL 关键字,当它在“串联疯狂”中转换为字符串时,它保持为空(字符串中没有任何内容) ),因此 Postgres(如预期)在 (, , ,) 部分发送语法错误。

我需要用 NULL 关键字替换那些空字符串 (''),以便 Postgres 正确识别并插入它。

此外,构造函数中传递的一些值在 insertarObjeto() 函数中用单引号括起来(它们在数据库中是字符变化)。

在 Postgres 中 ''NULL 不同。

我尝试解决这个问题

一种方法是放置 (N) if-else 语句并在每种情况下连接正确的语句(这对于将来的代码维护和系统扩展来说有点难看),如下所示:

if(is_null($this->asistencia))
{
    $this->_query .= "null, ";
}
else
{
    $this->_query .= "$this->asistencia, ";
}

这种方法的问题是有很多属性(我的类比这个属性多得多)。

我看到的另一种方法是在构造函数中将 null 用单引号括起来。这适用于整数(我知道不好的解决方案),但是函数中已经用单引号括起来的那些会在查询中抛出“null”,这也是错误的,即:

INSERT INTO sga.actividades 
        (proyecto, ejec_proyecto, 
        num_actividad, nombre, 
        descripcion, fecha_inicio, 
        fecha_culminacion,
        ponderacion, critica, 
        lugar_ejecucion, recursos, 
        prepuesto_estimado, estado, 
        calificacion, presupuesto_ejecutado, 
        asistencia) 
    VALUES 
        (1, 1, sga.max_num_act(1, 1) + 1, 
        'Actividad1', 'DescA1', '14-07-14', 
        '14-07-14', 0, false, 'null', 'null', 0, 'ACT', null ,null ,null );

有没有更好的解决方案?除了 (n) 个 if-else 语句之外?

最佳答案

两种可能的解决方案:

1。省略列

如果您没有定义列默认值,则默认值(原文如此!)为 NULL。您可以省略应该为 NULL 的列:

INSERT INTO sga.actividades 
        (proyecto, ejec_proyecto, 
        num_actividad, nombre, 
        descripcion, fecha_inicio, 
        fecha_culminacion,
        ponderacion, critica, 
        prepuesto_estimado, estado) 
VALUES 
        (1, 1, sga.max_num_act(1, 1) + 1, 
        'Actividad1', 'DescA1', '14-07-14', 
        '14-07-14', 0, false, 0, 'ACT');

当然,如果您更改此处未提及的列的默认值,您也会更改此 INSERT 的结果 - 这可能是理想的,也可能不是理想的。

2。使用准备好的语句

然后您可以使用 PHP NULL 值。无论如何,清理输入并防止可能的 SQL 注入(inject)都是一个好主意。

pg_prepare($pg_conn, 'insert1', "INSERT INTO sga.actividades (proyecto, ejec_proyecto, ..., lugar_ejecucion, ...) VALUES ($1, $2, ...)");

pg_exec($pg_conn, 'insert1', array(1, 1, ..., NULL, ...));

有关 pg_prepare 的手册中有更多内容和 pg_execute .

关于php - 如何从 PHP 向 Postgres 插入 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876193/

相关文章:

PHP: Laravel 如何急切加载查找方法

php - 在 php 函数中使用循环并使用 JSON 返回结果

php - codeigniter 中的安全性

mysql - 在 PHPMyAdmin 中设置触发器

php - 检索所有用户(userId 和用户名)

postgresql - 如何为psql动态创建输出文件

python - 类不以多态方式加载

php - MySQL:Select LIKE 无法正常工作

mysql - sql 如果存在则插入,如果不存在则插入

python - 继承后如何删除抽象模型的模型属性的唯一约束?