Netbeans 向导生成 php 类

发布于 2024-09-25 17:29:35 字数 176 浏览 5 评论 0原文

我想要一个向导,我可以在其中插入数据库连接,并通过一些设置生成 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.

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

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

发布评论

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

评论(2

遥远的她 2024-10-02 17:29:35

NetBeans 太棒了!

NetBeans 具有强大的 MySql 支持,但是,没有本地工具可以从表生成类。

有一个名为 db2php 的插件,在此处查看。它将允许您生成类,基本上是 ORM。如果您不使用框架,那就太好了。如果您使用 Zend 框架,请在 NetBeans 站点上搜索 Zend Framework 支持或右键单击项目并转到 Zend -> 运行 Zend 命令

此外,为了轻松连接和代码生成,使用 ALT + Insert 可以节省大量时间。上下文会根据文档中定义的内容而变化。


编辑于 2010 年 10 月 1 日

目前还没有软件具有用于创建数据库表的自定义模板系统。人们试图转换为更标准化和无sql的方式,换句话说ORM。教义是其中之一,但有学习曲线。

解决方案 #1

您可能想要研究标准 NetBeans 模板系统。如果您转到 [工具 -> 模板 -> 在 PHP 部分下],您将能够添加模板,然后通过 [右键单击 -> 新建 -> 模板从模板创建新类。其他->您的模板]。扩展 PHP 类可能适合您。这是Sun 的博客,展示了如何使用模板,也在 Google 上进行了一些搜索。

解决方案 #2

继承可能是您的答案。创建具有连接和公共方法的 BaseTable,然后创建从 BaseTable 继承(扩展)的 ChildTable。

您可以创建数据访问层 (DAL) 来创建通用数据访问方法或创建表对象。

下面是 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);

您可以重写父类并使其成为通用表,其中包含所有字段以及保存方法、删除方法等...然后子类将扩展它并使其特定于表。
复制代码、到处使用模板并不是一个好习惯。最好延长课程,如果您决定进行更改,则必须更改 1 个地方。但是,如果您有数十个表并且使用了模板,则可能需要进行数十项更改。

只是遇到一个有趣的主题,可能会帮助您 哪些数据库模式(ORM、DAO、Active Record 等)可用于中小型项目?

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?

不语却知心 2024-10-02 17:29:35

如果您在 PHP 中使用 ORM,例如 Doctrine,它们有非常详细的命令行工具,可以帮助您自动生成类存根。

查看他们的参考指南

If you are using an ORM in PHP such as Doctrine they have very well documented command line tools which enable you to automatically generate class stubs.

Take a look at their reference guides.

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