{"id":159941,"date":"2012-11-23T16:38:03","date_gmt":"2012-11-23T12:38:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=159941"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=159941","title":{"rendered":"<span class=\"post_title\">ZF2 ActiveRecord Module<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>  \u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043d\u0430\u0447\u0430\u043b \u0438\u0437\u0443\u0447\u0430\u0442\u044c Zend Framework 2. \u0422\u0430\u043a \u043a\u0430\u043a \u0438\u043c\u0435\u044e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u043f\u044b\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439, \u0440\u0435\u0448\u0438\u043b \u0437\u0430\u043e\u0434\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b <a href=\"http:\/\/modules.zendframework.com\/\">modules.zendframework.com<\/a> \u0438 \u0440\u0435\u0448\u0438\u043b \u2014 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 Zf2ActiveRecord.<\/p>\n<p>  \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u044e\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0438 (\u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443), \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Zf2ActiveRecord. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0412\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 Git \u0438 Composer. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/ru.gitready.com\/\">\u044d\u0442\u043e<\/a> \u0438 <a href=\"http:\/\/getcomposer.org\/doc\/00-intro.md\">\u044d\u0442\u043e<\/a>. \u0411\u0443\u0434\u0435\u0442 \u043c\u0430\u043b\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0418\u0442\u0430\u043a, \u043f\u043e\u0435\u0445\u0430\u043b\u0438.<\/p>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<h5>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430<\/h5>\n<p>  \u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 \u043a\u0430\u0440\u043a\u0430\u0441 \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0443\u0436\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 <a href=\"https:\/\/github.com\/zendframework\/ZendSkeletonApplication\">ZendSkeletonApplication<\/a>.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0432 composer.json<\/p>\n<pre><code class=\"javascript\">&quot;require&quot;: {     &quot;alxsad\/zf2activerecord&quot;: &quot;dev-master&quot; } <\/code><\/pre>\n<p>  \u0418 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u044c \u0432 application.config.php<\/p>\n<pre><code class=\"php\">'modules' =&gt; array(     'Application',     'Zf2ActiveRecord', ), <\/code><\/pre>\n<h5>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0435\u0437 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f<\/h5>\n<p>  <\/p>\n<pre><code class=\"php\">'service_manager' =&gt; array(         'factories' =&gt; array(             'books-active-record' =&gt; function ($sm) {                 $adapter = $sm-&gt;get('zf2-active-record-adapter');                 $factory = new \\Zf2ActiveRecord\\ActiveRecord($adapter, array(                     'primaryKey' =&gt; 'id',                     'tableName'  =&gt; 'books',                 ));                 return $factory;             },         ),     ) <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"php\">namespace Application\\Controller; use Zend\\Mvc\\Controller\\AbstractActionController;  class IndexController extends AbstractActionController {     public function indexAction ()     {         \/* @var $books \\Zf2ActiveRecord\\ActiveRecord *\/         $books = $this-&gt;getServiceLocator()-&gt;get('books-active-record');         \/* @var $book \\Zf2ActiveRecord\\ActiveRecord *\/         $book = $books-&gt;create(array(             'title'  =&gt; 'test title',             'author' =&gt; 'test author',         ));         $saved = $book-&gt;save();     } } <\/code><\/pre>\n<h5>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435\u043c<\/h5>\n<p>  <\/p>\n<pre><code class=\"php\">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-&gt;id;     }      \/**      * @param int $id      * @return Book      *\/     public function setId ($id)     {         $this-&gt;id = (int) $id;         return $this;     }      \/**      * @return string      *\/     public function getAuthor ()     {         return $this-&gt;author;     }      \/**      * @param string $author      * @return Book      *\/     public function setAuthor ($author)     {         $this-&gt;author = $author;         return $this;     }      \/**      * @return string      *\/     public function getTitle ()     {         return $this-&gt;title;     }      \/**      * @param string $title      * @return Book      *\/     public function setTitle ($title)     {         $this-&gt;title = $title;         return $this;     }      \/**      * Exchange internal values from provided array      *      * @param  array $array      * @return void      *\/     public function exchangeArray (array $array)     {         foreach ($array as $key =&gt; $value) {             switch (strtolower($key)) {                 case 'id':                     $this-&gt;setId($value);                     continue;                 case 'author':                     $this-&gt;setAuthor($value);                     continue;                 case 'title':                     $this-&gt;setTitle($value);                     continue;                 default:                     break;             }         }     }      \/**      * Return an array representation of the object      *      * @return array      *\/     public function getArrayCopy ()     {         return array(             'id'     =&gt; $this-&gt;getId(),             'author' =&gt; $this-&gt;getAuthor(),             'title'  =&gt; $this-&gt;getTitle(),         );     } } <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"php\">'service_manager' =&gt; array(     'factories' =&gt; array(         'books-active-record' =&gt; function ($sm) {             $adapter = $sm-&gt;get('zf2-active-record-adapter');             $factory = new \\Application\\Entity\\Book();             $factory-&gt;setAdapter($adapter)                     -&gt;setPrimaryKey('id')                     -&gt;setTableName('books');             return $factory;         },     ), ) <\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"php\">namespace Application\\Controller; use Zend\\Mvc\\Controller\\AbstractActionController;  class IndexController extends AbstractActionController {     public function indexAction ()     {         \/* @var $books \\Application\\Entity\\Book *\/         $books = $this-&gt;getServiceLocator()-&gt;get('books-active-record');         \/* @var $book \\Application\\Entity\\Book *\/         $book = $books-&gt;findByPk(1);         $book-&gt;setTitle('Very Interested Book');         $saved = $book-&gt;save();     } } <\/code><\/pre>\n<h5>\u041f\u0440\u0438\u043c\u0435\u0440 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f<\/h5>\n<p>  <\/p>\n<pre><code class=\"php\">namespace Application\\Controller; use Zend\\Mvc\\Controller\\AbstractActionController;  class IndexController extends AbstractActionController {     public function indexAction ()     {         \/* @var $books \\Application\\Entity\\Book *\/         $books = $this-&gt;getServiceLocator()-&gt;get('books-active-record');         \/* @var $book \\Application\\Entity\\Book *\/         $book = $books-&gt;findByPk(1);         $deleted = $book-&gt;delete();     } } <\/code><\/pre>\n<h5>\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u0438\u0441\u043a\u0430<\/h5>\n<p>  <\/p>\n<pre><code class=\"php\">namespace Application\\Controller; use Zend\\Mvc\\Controller\\AbstractActionController;  class IndexController extends AbstractActionController {     public function indexAction ()     {         \/* @var $books \\Zf2ActiveRecord\\ActiveRecord *\/         $books = $this-&gt;getServiceLocator()-&gt;get('books-active-record');         return array(             'books' =&gt; $books-&gt;find(function(\\Zend\\Db\\Sql\\Select $select){                 $select-&gt;where(array('is_active' =&gt; 1));                 $select-&gt;limit(10);             }),         );     } } <\/code><\/pre>\n<h5>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u043c\u0438<\/h5>\n<p>  <\/p>\n<pre><code class=\"php\">namespace Application\\Controller; use Zend\\Mvc\\Controller\\AbstractActionController;  class IndexController extends AbstractActionController {     public function indexAction ()     {         $this-&gt;getEventManager()-&gt;getSharedManager()-&gt;attach(             'Application\\Entity\\Book', 'save.pre', function($e)         {             $book = $e-&gt;getTarget();             if ($book-&gt;isNew()) {                 $book-&gt;setTitle($book-&gt;getTitle() . ' - new');             }         });         \/* @var $books \\Application\\Entity\\Book *\/         $books = $this-&gt;getServiceLocator()-&gt;get('books-active-record');         \/* @var $book \\Zf2ActiveRecord\\ActiveRecord *\/         $book = $books-&gt;create(array(             'title'  =&gt; 'test title',             'author' =&gt; 'test author',         ));         $saved = $book-&gt;save();     } } <\/code><\/pre>\n<h5>\u0414\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f<\/h5>\n<p>  <\/p>\n<ol>\n<li>find.pre<\/li>\n<li>find.post<\/li>\n<li>save.pre<\/li>\n<li>save.post<\/li>\n<li>delete.pre<\/li>\n<li>delete.post<\/li>\n<\/ol>\n<h5>\u0417\u0430\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0432 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438<\/h5>\n<p>  <\/p>\n<ol>\n<li>\u0422\u0435\u0441\u0442\u044b PHPUnit<\/li>\n<li>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441\u043e \u0441\u0432\u044f\u0437\u044f\u043c\u0438 (one-to-one, one-to-many, many-to-many)<\/li>\n<li>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0432\u0448\u0438\u0445\u0441\u044f \u043e\u0448\u0438\u0431\u043e\u043a<\/li>\n<\/ol>\n<h5>\u0421\u0441\u044b\u043b\u043a\u0438<\/h5>\n<p>  <a href=\"https:\/\/github.com\/alxsad\/zf2activerecord\">https:\/\/github.com\/alxsad\/zf2activerecord<\/a><br \/>  <a href=\"https:\/\/packagist.org\/packages\/alxsad\/zf2activerecord\">https:\/\/packagist.org\/packages\/alxsad\/zf2activerecord<\/a><\/p>\n<p>  <i>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/i>    \t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/159941\/\"> http:\/\/habrahabr.ru\/post\/159941\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>  \u0421\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043d\u0430\u0447\u0430\u043b \u0438\u0437\u0443\u0447\u0430\u0442\u044c Zend Framework 2. \u0422\u0430\u043a \u043a\u0430\u043a \u0438\u043c\u0435\u044e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u043f\u044b\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439, \u0440\u0435\u0448\u0438\u043b \u0437\u0430\u043e\u0434\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b <a href=\"http:\/\/modules.zendframework.com\/\">modules.zendframework.com<\/a> \u0438 \u0440\u0435\u0448\u0438\u043b \u2014 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 Zf2ActiveRecord.<\/p>\n<p>  \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u044e\u0442\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0438 (\u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443), \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Zf2ActiveRecord. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0412\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 Git \u0438 Composer. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/ru.gitready.com\/\">\u044d\u0442\u043e<\/a> \u0438 <a href=\"http:\/\/getcomposer.org\/doc\/00-intro.md\">\u044d\u0442\u043e<\/a>. \u0411\u0443\u0434\u0435\u0442 \u043c\u0430\u043b\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0418\u0442\u0430\u043a, \u043f\u043e\u0435\u0445\u0430\u043b\u0438.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-159941","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/159941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=159941"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/159941\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}