Zend Model Creator 2
Version 0.0.1 Created by Henrik Hussfelt, (*1)
Introduction
Zend Model Creator is a generator for quick prototyping of your Zend Project.
If you are not keen on using pure ORM's like Doctrine, this might be something for you., (*2)
Out of the box, Zend Model Creator works with Zend\Db and generates Entities, Mappers
and event aware Services based on your database structure., (*3)
This project is manly used to avoid the time spent on creating models for
your Zend Framework projects., (*4)
Requirements
Features / Goals
- Generate Entities [COMPLETE]
- Generate Mappers [COMPLETE]
- Generate Event Aware Services [COMPLETE]
- Generate EventServices [COMPLETE]
- Generate Module.php [COMPLETE]
- Generate autoloader files [COMPLETE]
- Generate Add Forms [INCOMPLETE]
- Generate Edit Forms [INCOMPLETE]
- Generate FormFilters [INCOMPLETE]
Installation
Install via git, (*5)
Clone this repo
git clone git@github.com:hussfelt/Zend-Model-Creator-2.git
, (*6)
cd into your directory
cd Zend-Model-Creator-2
, (*7)
Install via Composer, (*8)
Add this to your composer.json under "require":
"hussfelt/zend-model-creator-2": "dev-master"
, (*9)
Run command:
php composer.phar update
, (*10)
Usage
Cd into the directory (installed with composer):
cd vendor/hussfelt/zend-model-creator-2
, (*11)
Run with php:
php zmc.php --host=[DB_HOST] --db=[DATABASE_NAME] --user=[USERNAME] --password=[PASSWORD]
, (*12)
- Move your [NAMESPACE] directory into the /vendor/ directory
- Add [NAMESPACE] to your application.config.php:
return array(
'modules' => array(
'ZfcBase',
'[NAMESPACE_HERE]',
'Application',
),
);
Options
--without-entity=1
Will not generate entities, (*13)
--without-mapper=1
Will not generate mapper, (*14)
--without-service=1
Will not generate the services, (*15)
--without-module=1
Will not generate the Module.php file, (*16)
--without-autoloaders=1
Will not generate the autoloader files, (*17)
--without-config=1
Will not generate the config file, (*18)
--without-options=1
Will not generate the options file, (*19)
--namespace=[NAMESPACE]
Will generate your data into a given Namespace and not use ZMC2, (*20)
Example usage in Controller
This will replace the IndexController.php in the Zend Framework Skeleton application.
Given you have a database-table "album" in your database, this will output all records "Name" from that table., (*21)
module/Application/src/Application/Controller/IndexController.php:, (*22)
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
protected $albumService;
protected $options;
public function indexAction()
{
// Get all albums from service
$albums = $this->getAlbumService()->findAll();
foreach ($albums as $album) {
echo $album->getName() . "<br />";
}
exit;
}
public function getAlbumService()
{
if (!isset($this->albumService)) {
$this->albumService = $this->getServiceLocator()->get('ZmcBase\Service\Album');
}
return $this->albumService;
}
}
Also, you need to fix your global.php and local.php config files if you have not done this yet:, (*23)
config/autoload/global.php:, (*24)
<?php
/**
* Global Configuration Override
*
* You can use this file for overridding configuration values from modules, etc.
* You would place values in here that are agnostic to the environment and not
* sensitive to security.
*
* @NOTE: In practice, this file will typically be INCLUDED in your source
* control, so do not include passwords or other sensitive information in this
* file.
*/
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=[YOUR_DATABASE_NAME];host=[YOUR_DB_HOST]',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
config/autoload/local.php:, (*25)
<?php
/**
* Local Configuration Override
*
* This configuration override file is for overriding environment-specific and
* security-sensitive configuration information. Copy this file without the
* .dist extension at the end and populate values as needed.
*
* @NOTE: This file is ignored from Git by default with the .gitignore included
* in ZendSkeletonApplication. This is a good practice, as it prevents sensitive
* credentials from accidentally being comitted into version control.
*/
return array(
'db' => array(
'username' => '[DB_USERNAME]',
'password' => '[DB_PASSWORD]',
),
);