Repository Style Example

Version: 0.3.0 +

Please Note: Repository style cannot use server-side processing

Id User Name First Name Last Name Email Roles

Code

Controller

<?php
namespace Brown298\DtTestBundle\Controller;

use Brown298\DtTestBundle\Model\UserRepositoryTable;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Class DtRepositoryStyleController
 *
 * @Route("/style/repository/")
 *
 * @package Brown298\DtTestBundle\Controller
 * @author  John Brown <brown.john@gmail.com>
 */
class DtRepositoryStyleController extends Controller
{
    /**
     * indexAction
     *
     * @Route("", name="repository_style")
     * @Template()
     *
     * @return array
     */
    public function indexAction()
    {
        $request    = $this->getRequest();
        $em         = $this->get('doctrine.orm.entity_manager');
        $repository = $em->getRepository('Brown298\DtTestBundle\Entity\User');

        $dataTable = new UserRepositoryTable();
        $dataTable->setContainer($this->container);
        $dataTable->setRepository($repository);
        if ($response = $dataTable->ProcessRequest($request)) {
            return $response;
        }

        return array(
            'columns' => $dataTable->getColumns(),
        );
    }
}

Model (UserRepositoryTable.php)

<?php
namespace Brown298\DtTestBundle\Model;

use Brown298\DataTablesBundle\Model\DataTable\RepositoryDataTableInterface;
use Brown298\DataTablesBundle\Test\DataTable\RepositoryDataTable;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class UserRepositoryTable
 *
 * @package Brown298\DtTestBundle\Model
 */
class UserRepositoryTable extends RepositoryDataTable implements RepositoryDataTableInterface
{
    /**
     * @var array
     */
    protected $columns = array(
        'user.id'         => 'Id',
        'user.username'   => 'User Name',
        'user.firstName'  => 'First Name',
        'user.lastName'   => 'Last Name',
        'user.email'      => 'Email',
        'user.roles.name' => 'Roles'
    );

    /**
     * @var bool hydrate results to doctrine objects
     */
    public $hydrateObjects = true;

    /**
     * getDataFormatter
     *
     * @return \Closure
     */
    public function getDataFormatter()
    {
        $renderer = $this->container->get('templating');
        return function($data) use ($renderer) {
            $count   = 0;
            $results = array();

            /** Brown298\DtTestBundle\Entity\User $row */
            foreach ($data as $row) {
                $results[$count][] = $row->getId();
                $results[$count][] = $renderer->render('Brown298DtTestBundle:DtRepositoryStyle:nameFormatter.html.twig', array('name' => $row->getUsername()));
                $results[$count][] = $row->getFirstName();
                $results[$count][] = $row->getLastName();
                $results[$count][] = $row->getEmail();
                $roles = array();
                foreach ($row->getRoles() as $role) {
                    $roles[] = $role->getName();
                }
                $results[$count][] = implode(', ', $roles);

                $count += 1;
            }

            return $results;
        };
    }
}

Twig Index (index.html.twig)

{% extends 'Brown298DtTestBundle::base.html.twig' %}

{% block body %}
    <h2>Repository Style Example</h2>

    {% include "Brown298DtTestBundle:DtRepositoryStyle:content.html.twig" %}

    <div class="table-bordered">
        {{ addDataTable(columns, {
        'bLengthChange': 1,
        'bInfo':         1,
        'bFilter':       1,
        'bServerSide':   0
        }) }}
    </div>


{% endblock %}

Twig Formatter (nameFormatter.html.twig)

<span class='strong'>{{ name }} </span>