Model Style Example

Version: 0.2.0 +

The Model style is preferred over the controller method as it abstracts the processing of the table to it's own class. This creates a more testable and flexible interface. The benefit is that the controller becomes much lighter by handeling the response in the same action. Rendering the data rows is also moved to the model and implemented as a closure for a nice clean implementation.

Example Table

Id Name

Code

Controller

<?php
namespace Brown298\DtTestBundle\Controller;

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

/**
 * Class DtModelStyleController
 *
 * @Route("/style/model/")
 *
 * @package Brown298\DtTestBundle\Controller
 */
class DtModelStyleController extends Controller
{
    /**
     * indexAction
     *
     * @Route("", name="model_style")
     * @Template()
     *
     * @return array
     */
    public function indexAction()
    {
        $request   = $this->getRequest();
        $em        = $this->get('doctrine.orm.entity_manager');

        // process the data table
        $dataTable = new UserTable();
        $dataTable->setEm($em);
        $dataTable->setContainer($this->container);
        if ($response = $dataTable->ProcessRequest($request)) {
            return $response;
        }

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

Model (UserTable.php)

<?php
namespace Brown298\DtTestBundle\Model;


use Brown298\DataTablesBundle\Model\DataTable\AbstractQueryBuilderDataTable;
use Brown298\DataTablesBundle\Model\DataTable\QueryBuilderDataTableInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class UserTable
 *
 * @package Brown298\DtTestBundle\Model
 */
class UserTable extends AbstractQueryBuilderDataTable implements QueryBuilderDataTableInterface
{
    /**
     * @var array
     */
    protected $columns = array(
        'user.id'   => 'Id',
        'user.username' => 'Name',
    );

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

            foreach ($data as $row) {
                $results[$count][] = $row['id'];
                $results[$count][] = $renderer->render('Brown298DtTestBundle:DtModelStyle:nameFormatter.html.twig', array('name' => $row['username']));
                $count += 1;
            }

            return $results;
        };
    }

    /**
     * getQueryBuilder
     *
     * @param Request $request
     *
     * @return null
     */
    public function getQueryBuilder(Request $request = null)
    {
        $userRepository = $this->em->getRepository('Brown298\DtTestBundle\Entity\User');
        $qb = $userRepository->createQueryBuilder('user')
            ->andWhere('user.deleted = false');

        return $qb;
    }
}

Twig Index (index.html.twig)

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

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

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

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

{% endblock %}

Twig Formatter (nameFormatter.html.twig)

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