基于 MySQL 列数据类型的动态 PHP 代码创建

发布于 2024-12-28 23:20:16 字数 451 浏览 3 评论 0 原文

我正在开发一个工具,可以根据数据库中的表创建 PHP 类。大多数功能都基于来自 SHOW COLUMNS FROM $table 的数据。每列通常与正在创建的新类中的一个实例变量相关。

我现在遇到的情况是,如果用户为与 INT 类型的列相关的变量传入“12”,它不会失败,但数据库中的值变为 0。

我想要的是该类返回 false,表明 UPDATEINSERT 无法完成。

为此

,您建议采用什么方法? 我现在正在尝试构建一个列名称数组,其中包含具有数字数据类型(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘东忘西忘不掉你 2025-01-04 23:20:16

你问的就是 ORM。 PHP 中有一些很好的 ORM 库。

  1. 教义
  2. 推进
  3. 出口 ORM
  4. Leap ORM for Kohana(我使用它)
  5. PHP Data Mapper

顺便说一句,我回答你的问题。

如果设置了任何无效数据,您可以抛出异常。

查看示例。

class MyTable extends DbTable{
    function __set($var, $val){
        switch($var){
            case 'age': // age should be numeric type
                if(!is_numeric($val)){
                    throw new InvalidDataTypeException("$val of $var is not numeric");
                }
             /// do other operation here

             ... 
             ... // more cases
        }
    }
}

What are you are asking is called ORM. There are some good ORM libraries in PHP.

  1. Doctrine
  2. Propel
  3. Outlet ORM
  4. Leap ORM for Kohana ( I use it )
  5. PHP Data Mapper

BTW, I answer your question.

If any invalid data is set you can throw an Exception.

See a sample.

class MyTable extends DbTable{
    function __set($var, $val){
        switch($var){
            case 'age': // age should be numeric type
                if(!is_numeric($val)){
                    throw new InvalidDataTypeException("$val of $var is not numeric");
                }
             /// do other operation here

             ... 
             ... // more cases
        }
    }
}
请持续率性 2025-01-04 23:20:16

我的方法是为每个字段不仅为值创建一个变量,还为验证函数创建一个变量。

现在有一些静态验证函数,例如 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 an INT field would have MyClass::ValidateInt, call this while generating SQL for updating or inserting

失退 2025-01-04 23:20:16

如果您想验证输入,也许您可​​以将 jQuery Validate 插件集成到创建数据库实例的内核类中。

例如,您有一个 INT 列,使用 SHOW COLUMNS 您可以确定该列必须具有所需的十进制数,这样如果验证不正确,就没有人可以提交表单然后将其插入数据库。

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.

浪荡不羁 2025-01-04 23:20:16

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文