我正在开发一个工具,可以根据数据库中的表创建 PHP 类。大多数功能都基于来自 SHOW COLUMNS FROM $table
的数据。每列通常与正在创建的新类中的一个实例变量相关。
我现在遇到的情况是,如果用户为与 INT 类型的列相关的变量传入“12”,它不会失败,但数据库中的值变为 0。
我想要的是该类返回 false,表明 UPDATE
或 INSERT
无法完成。
为此
,您建议采用什么方法? 我现在正在尝试构建一个列名称数组,其中包含具有数字数据类型(INT、FLOAT 等)的每一列。当用户尝试设置变量时,它会检查该变量是否在此列表中,然后检查正在设置的值以确定它是否是数字。
使用 MySQLi 的 PHP 5.3.3
我假设任何非数字列都将接受文本数据。
I am developing a tool that creates PHP classes based on tables in my database. Most of the functionality is based on data from SHOW COLUMNS FROM $table
. Each column typically relates to an instance variable in the new class being created.
What I have now is a situation where if a user passes in "twelve" for a variable related to a column of type INT, it doesn't fail, but the value in the database becomes 0.
What I would like is for the class to return false, identifying that the UPDATE
or INSERT
could not be completed.
What approach would you recommend for this?
What I'm trying now, is to construct an array of column names that contains each column that has a numeric data type (INT, FLOAT etc.). And when the user tries to set a variable, it checks to see if the variable is in this list and then checks the value being set to determine if it is a number.
PHP 5.3.3 using MySQLi
I'm assuming any column that isn't numeric will accept text data.
发布评论
评论(4)
你问的就是 ORM。 PHP 中有一些很好的 ORM 库。
顺便说一句,我回答你的问题。
如果设置了任何无效数据,您可以抛出异常。
查看示例。
What are you are asking is called ORM. There are some good ORM libraries in PHP.
BTW, I answer your question.
If any invalid data is set you can throw an Exception.
See a sample.
我的方法是为每个字段不仅为值创建一个变量,还为验证函数创建一个变量。
现在有一些静态验证函数,例如
MyClass::ValidateInt()
、MyClass::ValidateFloat()
和朋友,INT
的验证函数变量code> 字段将具有MyClass::ValidateInt
,在生成用于更新或插入的 SQL 时调用它My approach would be for each field to create not only a variable for the value, but also one for the validation function.
Now have some static validation functions such as
MyClass::ValidateInt()
,MyClass::ValidateFloat()
and friends, the validation function variable for anINT
field would haveMyClass::ValidateInt
, call this while generating SQL for updating or inserting如果您想验证输入,也许您可以将 jQuery Validate 插件集成到创建数据库实例的内核类中。
例如,您有一个 INT 列,使用 SHOW COLUMNS 您可以确定该列必须具有所需的十进制数,这样如果验证不正确,就没有人可以提交表单然后将其插入数据库。
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
If you want to validate the input, maybe you can integrate jQuery Validate plugin into your kernel class that creates the instance of the database.
For example, you have a column that is INT, with SHOW COLUMNS you can to stablish that this column must have a required decimal number so no one can to submit the form and then insert it to the database if is not correctly validate.
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
UI 拥护者可能会说接受“12”,并将其转换为“12”。
从编程的角度来看,如果必须指示函数当前范围之外的失败,处理此问题的典型方法是引发异常并让调用者处理这种情况。
UI advocates might say accept "twelve", and convert it to "12".
From a programming standpoint, if you must indicate failure outside of the current scope of the function, the typical way to handle this is to raise an exception and let the caller handle the situation.