2017 © Pedro Peláez
 

library zf2activerecord

ZF2 ActiveRecord

image

alxsad/zf2activerecord

ZF2 ActiveRecord

  • Friday, November 23, 2012
  • by alxsad
  • Repository
  • 3 Watchers
  • 4 Stars
  • 29 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Zf2ActiveRecord

Installation

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

Example 2 (Simple)

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

Example 3 (Delete)

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

Example 5 (Events)

``` 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)

The Versions

23/11 2012

dev-master

9999999-dev https://github.com/alxsad/zf2activerecord

ZF2 ActiveRecord

  Sources   Download

BSD-3-Clause

The Requires

 

zf2 active record