Netbeans wizard to generate php classes
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?
Th开发者_运维知识库anks!
Edit
I'm looking for something wich let me make my own class template or set it up.
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?
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.
精彩评论