ZF2 Module ActiveRecord

Hello.

Relatively recently started to learn Zend Framework 2. As they have a lot of experience with the first version, at the same time decided to write something useful. Looked modules.zendframework.com and decided it would be Zf2ActiveRecord.

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



Installation

Write your frame or use a ready ZendSkeletonApplication.

Add dependency in composer.json

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

And include the module in application.config.php

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


Simple example of usage without inheritance

the
'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;
},
),
)

the
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();
}
}


Example usage with inheritance

the
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 the  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(),
);
}
}

the
'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;
},
),
)

the
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();
}
}


delete Example

the
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();
}
}


Example search

the
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
public function indexAction ()
{
/* @var $books \Zf2ActiveRecord\ActiveRecord */

return array(
'books' => $books->find(function(\Zend\Db\Sql\Select $select){
$select- > where(array('is_active' = > 1));
$select- > limit(10);
}),
);
}
}


Example events

the
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

    the
  1. find.pre
  2. the
  3. find.post
  4. the
  5. save.pre
  6. the
  7. save.post
  8. the
  9. delete.pre
  10. the
  11. delete.post


Planned in the new version

    PHPUnit Tests the

  1. Work with relationships (one-to-one, one-to-many, many-to-many)
  2. the
  3. Fix discovered errors


Links

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

Thank you
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

The release of the new version of the module modLivestreet 0.3.0-rc

mSearch: search + filter for MODX Revolution

Emulator data from GNSS receiver NMEA