dev-master
9999999-dev https://github.com/alxsad/zf2activerecordZF2 ActiveRecord
BSD-3-Clause
The Requires
- php >=5.3.3
- zendframework/zendframework 2.*
zf2 active record
Wallogit.com
2017 © Pedro Peláez
ZF2 ActiveRecord
step 1 - composer.json ``` json "require": { "alxsad/zf2activerecord": "dev-master" }, (*1)
step 2 - run command ``` bash php composer.phar self-update php composer.phar update
step 3 - include module in application.config.php ``` php 'modules' => array( 'Application', 'Zf2ActiveRecord', ),, (*2)
Provided Events ------------- * find.pre * find.post * save.pre * save.post * delete.pre * delete.post Example 1 ------------- module.config.php ``` php '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; }, ), )
``` php <?php, (*3)
namespace Application\Entity;, (*4)
use Zf2ActiveRecord\AbstractActiveRecord;, (*5)
class Book extends AbstractActiveRecord { /** * @var int */ protected $id = null;, (*6)
/**
* @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(),
);
}
}, (*7)
``` php
<?php
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();
return array();
}
}
module.config.php ``` php '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; }, ), ), (*8)
``` php <?php 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(); return array(); } }
``` php <?php, (*9)
namespace Application\Controller;, (*10)
use Zend\Mvc\Controller\AbstractActionController;, (*11)
class IndexController extends AbstractActionController { public function indexAction () { /* @var $books \Application\Entity\Book */ $books = $this->getServiceLocator()->get('books-active-record');, (*12)
/* @var $book \Application\Entity\Book */
$book = $books->findByPk(1);
$deleted = $book->delete();
return array();
}
}, (*13)
Example 4 (Find) ------------- ``` php <?php 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); }), ); } }
``` php <?php, (*14)
namespace Application\Controller;, (*15)
use Zend\Mvc\Controller\AbstractActionController;, (*16)
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'); } });, (*17)
/* @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();
return array();
}
} ```, (*18)
Alex Davidovich (alxsad@gmail.com), (*19)
ZF2 ActiveRecord
BSD-3-Clause
zf2 active record