PHP code example of zfstarter / zfs-domain-model

1. Go to this page and download the library: Download zfstarter/zfs-domain-model library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zfstarter / zfs-domain-model example snippets


'service_manager' => array(
    'factories' => array(
        'ZFS\DomainModel\Service' => 'ZFS\DomainModel\Service\Factory'
    )
)


'service_manager' => array(
    'aliases' => array(
        // допустим, у нас есть подключение к БД как 'Db\Adapter'
        // чтоб ZFS\DomainModel могла его использовать по-умолчанию, делаем алиас:
        'ZFS\DomainModel\Adapter' => 'Db\Adapter'
    )
)


namespace Application\Model\Gateway;

use ZFS\DomainModel\Gateway\AbstractFactory as BaseAbstractFactory;

class AbstractFactory extends BaseAbstractFactory
{
    protected $provides = array(
        'UsersGateway' => array(
            'tableName' => 'users'
        )
    );
}

'service_manager' => array(
    'abstract_factories' => array(
        'Application\Model\Gateway\AbstractFactory'
    )
)

/** @var \ZFS\DomainModel\Gateway\TableGateway $gateway */
$gateway = $this->getServiceLocator()->get('UsersGateway');

const OPTION_TABLE_GATEWAY        = 'tableGateway';         // имя класса, создается через new
const OPTION_TABLE_NAME           = 'tableName';            // имя таблицы, единственное обязательное поле
const OPTION_ADAPTER              = 'adapter';              // имя, доступное через ServiceLocator
const OPTION_TABLE_FEATURES       = 'tableFeatures';        // массив из обьектов ***Feature
const OPTION_RESULT_SET_PROTOTYPE = 'resultSetPrototype';   // имя класса, создается через new
const OPTION_OBJECT_PROTOTYPE     = 'objectPrototype';      // имя класса, создается через new
const OPTION_SQL                  = 'sql';                  // имя класса, создается через new


namespace Application\Model\Object;

use ZFS\DomainModel\Object\Object;

class User extends Object
{
    protected $primaryColumns = array(
        'id'
    );

    public function getId()
    {
        return $this->data['id']; // напрямую из массива данных
    }

    public function getName()
    {
        return $this->get('name'); // использует настройку $fieldToColumnMap, а так же возвращает null если isset($this->data[$name]) == false 
    }
}


namespace Application\Model\Object;

use ZFS\DomainModel\Object\ObjectMagic;

/**
 * @property int    id
 * @property string name
 */
class User extends ObjectMagic
{
    protected $primaryColumns = array(
        'id'
    );
}


namespace Application\Model\Object;

use ZFS\DomainModel\Object\ObjectMagic;

/**
 * @property int    id
 * @property string name
 * @property string dateOfBirth
 */
class User extends ObjectMagic
{
    protected $primaryColumns = array(
        'id'
    );

    protected $fieldToColumnMap = array(
        'dateOfBirth' => 'date_of_birth'
    );
}


namespace Application\Model\Gateway;

use ZFS\DomainModel\Feature\FilterColumnsFeature;
use ZFS\DomainModel\Gateway\AbstractFactory as BaseAbstractFactory;
use ZFS\DomainModel\Service\Options;

class AbstractFactory extends BaseAbstractFactory
{
    public function __construct()
    {
        $this->provides['UsersGateway'] = array(
            Options::OPTION_TABLE_NAME       => 'users',
            Options::OPTION_TABLE_FEATURES   => array(new FilterColumnsFeature()),
            Options::OPTION_OBJECT_PROTOTYPE => 'Application\Model\Object\User'
        );
    }
}