Multiple Example

Table A

Id Name

Table B

Id User Name First Name Last Name Email

Code

Controller

<?php
namespace Brown298\DtTestBundle\Controller;

use Brown298\DtTestBundle\Model\UserTableB;
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 MultipleController
 * @Route("/style/multiple/")
 *
 * @package Brown298\DtTestBundle\Controller
 */
class MultipleController extends Controller
{
    /**
     * indexAction
     *
     * @Route("/{tableName}", defaults={"tableName" = null}, name="multiple_style")
     * @Template()
     *
     * @param $tableName
     * @return array
     */
    public function indexAction($tableName=null)
    {
        $request   = $this->getRequest();
        $em        = $this->get('doctrine.orm.entity_manager');

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

        $dataTableB = new UserTableB();
        $dataTableB->setEm($em);
        $dataTableB->setContainer($this->container);
        if ($tableName == 'dataTableB' &&$response = $dataTableB->ProcessRequest($request)) {
            return $response;
        }

        // display html
        return array(
            'columnsA' => $dataTableA->getColumns(),
            'columnsB' => $dataTableB->getColumns(),
        );
    }
}

Model A (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;
    }
}

Model B (UserTableB.php)

<?php

namespace Brown298\DtTestBundle\Model;

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

    /**
     * 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:DtRepositoryStyle:nameFormatter.html.twig', array('name' => $row['username']));
                $results[$count][] = $row['firstName'];
                $results[$count][] = $row['lastName'];
                $results[$count][] = $row['email'];
                $count += 1;
            }

            return $results;
        };
    }
}

Twig Index (index.html.twig)

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

{% block body %}
    <h2>Multiple Example</h2>
    <h4>Table A</h4>
    <div class="table-bordered">
        {{ addDataTable(columnsA, {
        'path':          path('multiple_style',{'tableName':'dataTableA'}),
        'id':            'dataTableA',
        'bLengthChange': 1,
        'bInfo':         1,
        'bFilter':       1
        }) }}
    </div>
    <div class="clear"></div>
    <h4>Table B</h4>
    <div class="table-bordered">
        {{ addDataTable(columnsB, {
        'path':          path('multiple_style',{'tableName':'dataTableB'}),
        'id':            'dataTableB',
        'bLengthChange': 1,
        'bInfo':         1,
        'bFilter':       1
        }) }}
    </div>
{% endblock %}

Twig Formatter (nameFormatter.html.twig)

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