duanlu1793 2012-01-25 17:22
浏览 55
已采纳

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

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条回答 默认 最新

  • douying4203 2012-01-25 17:38
    关注

    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
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面