Fork me on GitHub

Mesour Sources

Packagist Travis Dependency Status

  • None

Installation

Suggests

Usage

$data = [[
        'id' => 1,
        'params' => '$name',
    ],[
        'id' => 2,
        'params' => '$header',
    ],[
        'id' => 3,
        'params' => '$callback',
]];

$source = new Mesour\DataGrid\Sources\ArrayGridSource($data);

//! return first row from result
$source->fetch();

//! return result in array($key => $value)
$source->fetchPairs('method', 'params');

//! return result
$source->fetchAll();

//! return raw rows from result, if use doctrine source, here will be Entities
$source->fetchLastRawRows();

For doctrine/orm

Use: Mesour\DataGrid\Sources\DoctrineGridSource

/** @var Doctrine\ORM\EntityManager $entityManager */
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
        ->from('Mesour\Sources\Tests\Entity\User', 'u');

//! second parameter is array of column mapping
$source = new \Mesour\Sources\DoctrineSource('users', 'userId, $qb, [
    'userId' => 'u.userId',
    'groupName' => 'gr.name',
]);

//! last parameter is and/or. "and" = FALSE, "or" = TRUE. FALSE is default
$source->where('u.email= :email', ['email' => 'john.doe@test.xx'], FALSE);

For nette/database

Use: Mesour\DataGrid\Sources\NetteDbGridSource

/** @var Nette\Database\Context $context */
$userSelection = $context->table('user');

$source = new Mesour\DataGrid\Sources\NetteDbGridSource('users', 'user_id', $userSelection);


//! Example where condition
$source->where('name = ?', 'John');

For array (mesour/array-manager)

Use: Mesour\DataGrid\Sources\ArrayGridSource

$data = [
    ['id' => 1, 'name' => 'John', 'amount' => 1546],
    ['id' => 2, 'name' => 'Peter', 'amount' => 351],
    ['id' => 3, 'name' => 'Peter', 'amount' => 351],
];
$source = new Mesour\DataGrid\Sources\ArrayGridSource('users', 'id', $data);

//! Example where condition
$source->where('name', 'John', Mesour\ArrayManage\Searcher\Condition::EQUAL, 'or')

Data Structure

One to one relationship

$columnMapping = [
    'id' => 'u.id',
    'group_id' => 'u.group',
    'last_login' => 'u.lastLogin',
    'group_name' => 'g.name',
    'group_type' => 'g.type'
];  

/** @var Doctrine\ORM\EntityManager $entityManager */
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
        ->from(Mesour\Sources\Tests\Entity\User::class, 'u');

$queryBuilder->join(Sources\Tests\Entity\Group::class, 'g', Join::WITH, 'u.group = g.id');

$source = new DoctrineFilterSource(Mesour\Sources\Tests\Entity\User::class, 'id', $queryBuilder, $columnMapping);

$dataStructure = $source->getDataStructure();

//! set one to one relationships
$dataStructure->addOneToOne('group_name', Mesour\Sources\Tests\Entity\Group::class, 'name');
$dataStructure->addOneToOne('group_type', Mesour\Sources\Tests\Entity\Group::class, 'type');

//! result must be (by your toArray method on entity!)
$source->fetch() === [
    'name' => 'John',
    'surname' => 'Doe',
    'group_name' => 'First group',
    'group_type' => 'admin',
];

One to many relationship

Warning All relasionship must be set on your entity
/** @var Doctrine\ORM\EntityManager $entityManager */
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
        ->from(Mesour\Sources\Tests\Entity\User::class, 'u');


$source = new DoctrineFilterSource(Mesour\Sources\Tests\Entity\User::class, 'id', $queryBuilder, ['id' => 'u.id']);

$dataStructure = $source->getDataStructure();

$dataStructure->addOneToMany(
    'addresses',
    Mesour\Sources\Tests\Entity\UserAddress::class,
    'user_id', //! real column name
);

//! result must be (by your toArray method on entity!)
$source->fetch() === [
    'name' => 'John',
    'surname' => 'Doe',
    'addresses' => [
        [
            'street' => 'test',
            'city' => 'Test city',
            'country' => 'Test country'
        ],
        // ... more addresses
    ],
];

Many to many relationship

Warning All relasionship must be set on your entity
Info For DoctrineSource must add referenced table for now. In this example companies. For NetteDbTableSource must add relational table for now. In this example user_companies. See: Some table missing on bottom of this page.
/** @var Doctrine\ORM\EntityManager $entityManager */
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
        ->from(Mesour\Sources\Tests\Entity\User::class, 'u');


$source = new DoctrineFilterSource(Mesour\Sources\Tests\Entity\User::class, 'id', $queryBuilder, ['id' => 'u.id']);

$dataStructure = $source->getDataStructure();

$dataStructure->addManyToMany(
    'companies',
    Mesour\Sources\Tests\Entity\Company::class,
    'company_id', //! real column name
    'user_companies', //! real relational table name
    'user_id', //! real column name
);

//! result must be (by your toArray method on entity!)
$source->fetch() === [
    'name' => 'John',
    'surname' => 'Doe',
    'companies' => [
        [
            'name' => 'IBM s.r.o.',
        ],
        // ... more user's companies
    ],
];
Info Can add some table manually
$source->addTableToStructure('companies', 'id');