【发布时间】:2014-07-22 08:35:21
【问题描述】:
我一直在开发一个项目管理系统,我正在使用 PHP 和 PostgreSQL 8.4 以及 Model-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 值,因为在 DB 中,这些值可以为空,到目前为止一切都很酷。
当我尝试运行它时,我得到了这个查询:
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() 函数中被包裹在单引号中(它们在数据库中为character varying)。
并且在 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 语句吗?
【问题讨论】:
-
您使用的是什么 pg_*() 函数?展示一个非常简单的~3 行示例。
-
没有“postgre”之类的东西。 The name is "PostgreSQL" or "Postgres" for short.
-
@ErwinBrandstetter,我不好我会更新标题,我没有注意到错字。此外,我将尝试您对准备好的语句的解决方案,它们看起来像是这样做的方法。
-
@DwayneTowell 我正在使用 ADODB 执行 postgres 语句,我将检查该库如何管理准备好的语句。
-
@GabrielVitale:表定义总是是个好主意。
标签: php sql postgresql