📜 ⬆️ ⬇️

Doctrine2 and ZendFramework Integration

Time does not stand still, and we must run after time.
Now and ZF2 on the way. First milestone released. Zend Framework 2.0.0dev1 Release . Doctrine2 BETA3 - was on Friday.
A good programmer never stands still - there is no time. Will lag behind and forward - make the table - "Code for the Food".

It was evening, there was nothing…

Downloading the source code of the libraries began to play. The goal is to be able to create models while maintaining the ZF structure and work through the Doctrine2 connection.
First we need to put in the include_path classes that are part of Doctrine2.
')
library/Doctrine
libray/Symfony


Symfony will need to use the tool out of the box for Doctrine2 to work (two Console and Yaml components).
In configs / application.ini we register data for the auto loader.
autoloadernamespaces.doctrine = "Doctrine"
autoloadernamespaces.symfony = "Symfony"


Continued under the cut. What options need to register in the application.ini. How to connect Doctrine2, the source code of a resource plugin.


For my application, I chose
appnamespace = "Ecom"

At the moment I have not found a more "beautiful" way to prescribe the path to the resource plugins.
pluginpaths.Ecom\Application\Resources = APPLICATION_PATH "/resources"


Doctrine2 configuration parameters, for me this is just a connection to the database configuration (as long as I don’t need anything else).
resources.doctrine2.connection.driver = "pdo_mysql"
resources.doctrine2.connection.host = "localhost"
resources.doctrine2.connection.user = "root"
resources.doctrine2.connection.password = ""
resources.doctrine2.connection.dbname = "doctrinedb"


In the resource plugin there must be a namespace - Ecom \ Application \ Resources; And the class name must match the name in application.ini.

<? php

namespace Ecom \ Application \ Resources ;

use Doctrine \ Common \ Util ;

use Ecom \ Document ;

use Doctrine \ ORM ;
use Doctrine \ DBAL ;
use Doctrine \ Common ;
use Doctrine \ Common \ Cache ;
use Zend \ Application \ Resource ;

class Doctrine2 extends Resource \ AbstractResource {

public function init ( ) {

$ front = $ this -> getBootstrap ( ) -> getResource ( 'frontcontroller' ) ;
$ modules = $ front -> getControllerDirectory ( ) ;
$ entityModels = array ( ) ;
foreach ( $ modules as $ module => $ moduleDirectory ) {
$ dir = dirname ( $ moduleDirectory ) . "/ models" ;
if ( is_dir ( $ dir ) ) {
$ entityModels [ ] = $ dir ;
}
}

$ options = $ this -> getOptions ( ) ;

$ config = new ORM \ Configuration ( ) ;

$ config -> setProxyDir ( APPLICATION_PATH . '/ proxies' ) ;
$ config -> setProxyNamespace ( 'Ecom \ Proxy' ) ;
$ config -> setAutoGenerateProxyClasses ( $ this -> getBootstrap ( ) -> getEnvironment ( ) == "development" ) ;

$ driverImpl = $ config -> newDefaultAnnotationDriver ( $ entityModels ) ;
$ config -> setMetadataDriverImpl ( $ driverImpl ) ;

if ( $ this -> getBootstrap ( ) -> getEnvironment ( ) == "development" ) {
$ cache = new Cache \ ArrayCache ( ) ;
} else {
$ cache = new Cache \ ApcCache ( ) ;
}

$ config -> setMetadataCacheImpl ( $ cache ) ;
$ config -> setQueryCacheImpl ( $ cache ) ;

$ evm = new Common \ EventManager ( ) ;
$ entityManager = ORM \ EntityManager :: create ( $ options [ 'connection' ] , $ config , $ evm ) ;

$ eventDocument = new EventDocument ( $ entityManager -> getEventManager ( ) ) ;

return $ entityManager ;
}
}


class EventDocument {

private $ evm ;

public $ preFooInvoked = false ;
public $ postFooInvoked = false ;

public function __construct ( $ evm )
{
$ this -> evm = $ evm ;
// $ evm-> addEventListener (array (ORM \ Events :: preRemove), $ this);
// $ evm-> addEventListener (array (ORM \ Events :: postRemove), $ this);
// $ evm-> addEventListener (array (ORM \ Events :: prePersist), $ this);
$ evm -> addEventListener ( array ( ORM \ Events :: postPersist , ORM \ Events :: postUpdate ) , $ this ) ;
// $ evm-> addEventListener (array (ORM \ Events :: preUpdate), $ this);
// $ evm-> addEventListener (array (ORM \ Events :: postUpdate), $ this);
// $ evm-> addEventListener (array (ORM \ Events :: postLoad), $ this);
// $ evm-> addEventListener (array (ORM \ Events :: loadClassMetadata), $ this);
// $ evm-> addEventListener (array (ORM \ Events :: onFlush), $ this);
}

public function postPersist ( Common \ EventArgs $ e )
{
\ Doctrine \ Common \ Util \ Debug :: dump ( $ e -> getEntity ( ) ) ;
echo __METHOD__ ;
}

public function postUpdate ( Common \ EventArgs $ e )
{
echo __METHOD__ ;
}


}

$ entityModels contains a list of existing modules in the system; the same mechanism is used by the Frontcontroller plugin resource for loading modules.
$ options ['connection'] - database connection options for Doctrine2.
When generating a schema, Doctrine2 will search for data in the ModuleName / Model folder, use the schema for the database based on the php-doc model class.

Creating a controller and an example of creating a new model object.

  1. <? php
  2. namespace Catalog ;
  3. use Zend \ Controller ;
  4. use Core \ Model \ Entity ;
  5. use Catalog \ Model ;
  6. class IndexController extends Controller \ Action
  7. {
  8. public function init ( )
  9. {
  10. / * Initialize action controller here * /
  11. }
  12. public function indexAction ( ) {
  13. $ product = new Model \ Product ( ) ;
  14. $ product -> setName ( 'test' ) ;
  15. $ product -> setSku ( 'test' . mktime ( ) ) ;
  16. / * @var $ em Doctrine \ ORM \ EntityManager * /
  17. $ em = $ this -> getInvokeArg ( 'bootstrap' ) -> getResource ( 'doctrine2' ) ;
  18. $ em -> persist ( $ product ) ;
  19. $ em -> flush ( ) ;
  20. $ this -> _helper -> layout -> setLayout ( 'layout' ) ;
  21. }
  22. }


Everything should be simple and clear. It is worth paying attention only to the fact that it is necessary to use the namespace Catalog; - module name.

The class of the product model itself

  1. <? php
  2. namespace Catalog \ Model ;
  3. use Core \ Model \ Entity ;
  4. / **
  5. * @Entity
  6. * @HasLifecycleCallbacks
  7. * @Table (name = "catalog_product")
  8. * /
  9. class Product extends Model \ Entity {
  10. / **
  11. * @var integer
  12. * @Id @Column (type = "integer")
  13. * @GeneratedValue (strategy = "AUTO")
  14. * /
  15. private $ id ;
  16. / **
  17. * @var string
  18. * @Column (type = "string", length = 255)
  19. * /
  20. private $ name ;
  21. / **
  22. * @var string
  23. * @Column (type = "string", length = 255, unique = true, nullable = false)
  24. * /
  25. private $ sku ;
  26. / **
  27. * @var string
  28. * @Column (type = "datetime", nullable = true)
  29. * /
  30. private $ created ;
  31. / **
  32. * @var string
  33. * @Column (type = "datetime", nullable = true)
  34. * /
  35. private $ updated ;
  36. / **
  37. * @return integer the $ id
  38. * /
  39. public function getId ( ) {
  40. return $ this -> id ;
  41. }
  42. / **
  43. * @return string the $ name
  44. * /
  45. public function getName ( ) {
  46. return $ this -> getProperty ( 'name' ) ;
  47. }
  48. / **
  49. * @param string $ name the $ name to set
  50. * /
  51. public function setName ( $ name ) {
  52. $ this -> name = $ name ;
  53. return $ this ;
  54. }
  55. / **
  56. * @return string the $ created
  57. * /
  58. / **
  59. * @return the $ sku
  60. * /
  61. public function getSku ( ) {
  62. return $ this -> getProperty ( 'sku' ) ;
  63. }
  64. / **
  65. * @param $ sku the $ sku to set
  66. * /
  67. public function setSku ( $ sku ) {
  68. $ this -> sku = $ sku ;
  69. return $ this ;
  70. }
  71. public function getCreated ( ) {
  72. return $ this -> getProperty ( 'created' ) ;
  73. }
  74. / **
  75. * @param $ created the $ created to set
  76. * /
  77. public function setCreated ( $ created ) {
  78. $ this -> created = $ created ;
  79. return $ this ;
  80. }
  81. / **
  82. * @return the $ updated
  83. * /
  84. public function getUpdated ( ) {
  85. return $ this -> getProperty ( 'updated' ) ;
  86. }
  87. / **
  88. * @param $ updated the $ updated to set
  89. * /
  90. public function setUpdated ( $ updated ) {
  91. $ this -> updated = $ updated ;
  92. return $ this ;
  93. }
  94. }


Again, it is worth noting - namespace Catalog \ Model; Mandatory instruction for model class.

My thoughts - I like it all. PHP has risen one step higher as a language for the corporate sector.

Source: https://habr.com/ru/post/99061/


All Articles