PHP code example of kristoreed / laminas-dictionary

1. Go to this page and download the library: Download kristoreed/laminas-dictionary 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/ */

    

kristoreed / laminas-dictionary example snippets


use Kristoreed\Laminas\DbManager\Query\Executor\Executor as QueryExecutor;

$dbAdapter = new Adapter([
    'driver'    => 'pdo',
    'dsn'       => 'mysql:dbname=test;host=localhost',
    'username'  => 'admin',
    'password'  => 'admin',
]);

$queryExecutor = new QueryExecutor($dbAdapter);
$userRepository = new Dictionary($queryExecutor);
$userRepository->setTableName('users');
$user = $userRepository->getElementById(404);


class User extends Dictionary implements UserInterface
{
    /**
     * @var string
     */
    protected $tableName = 'users';

    /**
     * @var QueryExecutorInterface
     */
    protected $queryExecutor;

    /**
     * User constructor
     *
     * @param QueryExecutorInterface $queryExecutor
     */
    public function __construct(QueryExecutorInterface $queryExecutor)
    {
        parent::__construct($queryExecutor);
        $this->queryExecutor = $queryExecutor;
    }

    /**
     * @param string $elementEmail
     *
     * @return array
     *
     * @throws ExecutorException
     */
    public function getElementByEmail(string $elementEmail): array
    {
        $select = new Select();
        $select
            ->from(['u' => $this->tableName])
            ->columns(['*'])
            ->where([
                'email' => $elementEmail
            ]);

        return $this->queryExecutor->getRow($select);
    }
}

$dbAdapter = new Adapter([
    'driver'    => 'pdo',
    'dsn'       => 'mysql:dbname=test;host=localhost',
    'username'  => 'admin',
    'password'  => 'admin',
]);

$queryExecutor = new QueryExecutor($dbAdapter);
$userRepository = new User($queryExecutor);
$user = $userRepository->getElementByEmail("[email protected]");