dqyy38265 2010-09-30 14:45
浏览 68
已采纳

Netbeans向导生成php类

I want a wizard where I insert the database connection, and with some settings it generates the PHP classes. The wizard can use (internally) a template already created.

Is that posible? Does that already exists? If not, any ideas of how to do it?

Thanks!

Edit

I'm looking for something wich let me make my own class template or set it up.

  • 写回答

2条回答 默认 最新

  • doushi5752 2010-09-30 14:55
    关注

    NetBeans rocks!

    NetBeans has strong MySql support, however, there are no native tools to generate classes from tables.

    There is a plug-in called db2php, check it out here. It will allow you to generate classes, basically ORM. Great if you are not using frameworks. If you are using Zend framework, search Zend Framework support on NetBeans site or right click project and go to Zend -> Run Zend Command.

    Also, for easy connections and code generation use ALT + Insert, saves a lot of time. The context changes depending on what is defined in you document.


    Edit on Oct 1, 2010

    There is no software out there that has custom templating system for database table creation. People try to convert to more standardized and sql free way, in other words ORM. Doctrine is one of them, but there is learning curve.

    Solution # 1

    You might want to look into standard NetBeans templating system. If you go to [Tools->Templates->Under PHP Section] you will be able to add templates and then create new classes from your the template via [Right Click->New -> Other -> Your Template]. Extending PHP Class might do it for you. Here is Sun's blog showing how to use templates, also do a little searching on Google.

    Solution # 2

    Inheritance might be the answer for you. Create BaseTable that has the connection and common methods, then create ChildTable that enherits (extends) from BaseTable.

    You can create Data Access Layer (DAL) to create common data access methods or create Table objects.

    Below is an example of DAL

    abstract class DALBase {
    
        /**
         * Database connection
         * @var <type>
         */
        protected $_connection = null;
        /**
         * Name of table
         * @var string
         */
        protected $_tbl = null;
        /**
         * Name of primary key column
         * @var string
         */
        protected $_key = null;
    
        /**
         * Default Init
         */
        public function __construct() {
            $this->_tbl = 'table_name';
            $this->_key = 'primary_key';
        }
    
        /**
         * Gets the connection
         * 
         * @return <type>
         */
        private function _getConnection() {
            if (!$this->_connection) {
                $this->_connection = mysqli_connect('localhost', 'zend101', '', 'zend101', '3306');
                if (!$this->_connection) {
                    throw new Exception('Could not connect to MySQL: ' . mysqli_connect_error());
                }
            }
    
            //
            return $this->_connection;
        }
    
        /**
         * Executes the query
         */
        public function executeQuery($query) {
            $conn = $this->_getConnection();
            return mysqli_query($conn, $query);
        }
    
        /**
         * Loads item by primary key
         * 
         * @param mixed $id
         * @return object
         */
        public function getByID($id) {
            //
            $query = "SELECT * FROM $this->_tbl WHERE $this->_key = $id";
    
            //
            $conn = $this->_getConnection();
            $result = $this->executeQuery($query);
    
            //
            $item = mysql_fetch_object($query);
    
            //
            return $item;
        }
    
        /**
         * Loads a list
         *
         * @return array
         */
        public function loadList() {
    
            //
            $data = array();
    
            //
            $result = $this->executeQuery("SELECT * FROM $this->_tbl");
            if ($result) {
                //  Scan through the resource
                while ($row = mysql_fetch_object($result)) {
                    //  put row object into the array
                    $data[] = $row;
                }
            }
    
            //
            return $data;
        }
    
    }
    
    /**
     * Car table
     */
    class DALCar extends DALBase {
    
        /**
         *
         */
        public function __construct() {
            $this->_tbl = 'car';
            $this->_key = 'vin';
        }
    
    }
    
    /**
     * Client table
     */
    class DALClient extends DALBase {
    
        /**
         *
         */
        public function __construct() {
            $this->_tbl = 'client';
            $this->_key = 'id';
        }
    
    }
    
    //  Usage
    $dal = new DALClient();
    $clients = $dal->loadList();
    $client1 = $dal->getByID(1);
    $client5 = $dal->getByID(5);
    

    You can rewrite the parent class and make it table generic where you have all your fields and save method, delete method, etc... and then child classes will extend it and make it table specific. It is not a good practice to copy your code, use template everywhere. It is better to extend classes, if you decide to make a change you will have to change it in 1 place. But if you have dozens of tables and you used template, you might need to do dozens of changes.

    Just run into an interesting topic, might help you Which database patterns (ORM, DAO, Active Record, etc.) to use for small/medium projects?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真