⬆️ ⬇️

ZF2 ActiveRecord Module

Hello.



Relatively recently, I began to study Zend Framework 2. Since I have a lot of experience with the first version, I decided to write something useful at the same time. I looked at modules.zendframework.com and decided it would be Zf2ActiveRecord.



In the article I will not describe how the modules are made (there are many articles on this topic), but just give some examples of working with Zf2ActiveRecord. It is assumed that you already know how to work with Git and Composer. If not, then you can read this and that . There will be little text and a lot of code. So let's go.

')





Installation


We write our skeleton or use the ready ZendSkeletonApplication .



Add a dependency to composer.json



"require": { "alxsad/zf2activerecord": "dev-master" } 


And we include the module in application.config.php



 'modules' => array( 'Application', 'Zf2ActiveRecord', ), 




Simple example of use without inheritance


 'service_manager' => array( 'factories' => array( 'books-active-record' => function ($sm) { $adapter = $sm->get('zf2-active-record-adapter'); $factory = new \Zf2ActiveRecord\ActiveRecord($adapter, array( 'primaryKey' => 'id', 'tableName' => 'books', )); return $factory; }, ), ) 


 namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; class IndexController extends AbstractActionController { public function indexAction () { /* @var $books \Zf2ActiveRecord\ActiveRecord */ $books = $this->getServiceLocator()->get('books-active-record'); /* @var $book \Zf2ActiveRecord\ActiveRecord */ $book = $books->create(array( 'title' => 'test title', 'author' => 'test author', )); $saved = $book->save(); } } 




Use case with inheritance


 namespace Application\Entity; use Zf2ActiveRecord\AbstractActiveRecord; class Book extends AbstractActiveRecord { /** * @var int */ protected $id = null; /** * @var string */ protected $author = null; /** * @var string */ protected $title = null; /** * @return int */ public function getId () { return $this->id; } /** * @param int $id * @return Book */ public function setId ($id) { $this->id = (int) $id; return $this; } /** * @return string */ public function getAuthor () { return $this->author; } /** * @param string $author * @return Book */ public function setAuthor ($author) { $this->author = $author; return $this; } /** * @return string */ public function getTitle () { return $this->title; } /** * @param string $title * @return Book */ public function setTitle ($title) { $this->title = $title; return $this; } /** * Exchange internal values from provided array * * @param array $array * @return void */ public function exchangeArray (array $array) { foreach ($array as $key => $value) { switch (strtolower($key)) { case 'id': $this->setId($value); continue; case 'author': $this->setAuthor($value); continue; case 'title': $this->setTitle($value); continue; default: break; } } } /** * Return an array representation of the object * * @return array */ public function getArrayCopy () { return array( 'id' => $this->getId(), 'author' => $this->getAuthor(), 'title' => $this->getTitle(), ); } } 


 'service_manager' => array( 'factories' => array( 'books-active-record' => function ($sm) { $adapter = $sm->get('zf2-active-record-adapter'); $factory = new \Application\Entity\Book(); $factory->setAdapter($adapter) ->setPrimaryKey('id') ->setTableName('books'); return $factory; }, ), ) 


 namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; class IndexController extends AbstractActionController { public function indexAction () { /* @var $books \Application\Entity\Book */ $books = $this->getServiceLocator()->get('books-active-record'); /* @var $book \Application\Entity\Book */ $book = $books->findByPk(1); $book->setTitle('Very Interested Book'); $saved = $book->save(); } } 




Deletion example


 namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; class IndexController extends AbstractActionController { public function indexAction () { /* @var $books \Application\Entity\Book */ $books = $this->getServiceLocator()->get('books-active-record'); /* @var $book \Application\Entity\Book */ $book = $books->findByPk(1); $deleted = $book->delete(); } } 




Sample search


 namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; class IndexController extends AbstractActionController { public function indexAction () { /* @var $books \Zf2ActiveRecord\ActiveRecord */ $books = $this->getServiceLocator()->get('books-active-record'); return array( 'books' => $books->find(function(\Zend\Db\Sql\Select $select){ $select->where(array('is_active' => 1)); $select->limit(10); }), ); } } 




An example of working with events


 namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; class IndexController extends AbstractActionController { public function indexAction () { $this->getEventManager()->getSharedManager()->attach( 'Application\Entity\Book', 'save.pre', function($e) { $book = $e->getTarget(); if ($book->isNew()) { $book->setTitle($book->getTitle() . ' - new'); } }); /* @var $books \Application\Entity\Book */ $books = $this->getServiceLocator()->get('books-active-record'); /* @var $book \Zf2ActiveRecord\ActiveRecord */ $book = $books->create(array( 'title' => 'test title', 'author' => 'test author', )); $saved = $book->save(); } } 




Available events


  1. find.pre
  2. find.post
  3. save.pre
  4. save.post
  5. delete.pre
  6. delete.post




Scheduled in the new version


  1. PHPUnit tests
  2. Work with relationships (one-to-one, one-to-many, many-to-many)
  3. Correction of detected errors




Links


https://github.com/alxsad/zf2activerecord

https://packagist.org/packages/alxsad/zf2activerecord



Thanks for attention

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



All Articles