Step-1 : install zf2 developer tool
php composer.pharrequire zendframework/zend-developer-tools:dev-master
Step-2: add in application config/application.config.php
'modules' => array(
'ZendDeveloperTools',
'DoctrineModule',
'DoctrineORMModule',
'Application',
),
Step-3: Create your entity module/Application/src/Application/Entity/User
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */
class User {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string") */
protected $fullName;
// getters/setters
}
Step-4: do mapping in module/Application/config/module.config.php
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities'
)
))),
Step-5: configure connection config/autoload/doctrine.local.php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity */
class User {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string") */
protected $fullName;
// getters/setters
}
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities'
)
))),
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
)))));
Step-6 , check you database create table or run doctrine command
php composer.pharrequire zendframework/zend-developer-tools:dev-master
php composer.pharrequire zendframework/zend-developer-tools:dev-master
Step-7 : test it .
module/Application/src/Application/Controller/IndexController.php
module/Application/src/Application/Controller/IndexController.php
public function indexAction() {
$objectManager = $this
->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$user = new \Application\Entity\User();
$user->setFullName('Marco Pivetta');
$objectManager->persist($user);
$objectManager->flush();
die(var_dump($user->getId())); // yes, I'm lazy
}
public function indexAction() {
$objectManager = $this
->getServiceLocator()
->get('Doctrine\ORM\EntityManager');
$user = new \Application\Entity\User();
$user->setFullName('Marco Pivetta');
$objectManager->persist($user);
$objectManager->flush();
die(var_dump($user->getId())); // yes, I'm lazy
}
Comments
Post a Comment