dudapiotr / ZfTable

*Not supported*ZfTable 3.1. Awesome ZF2 table / grid (and much much more) generator with huge possibilities of decorating and conditioning. Integrated with DataTables, Doctrine 2, Bootstrap 2.0 and 3.0.
http://dudapiotrek.laohost.net/
MIT License
75 stars 59 forks source link

Removed extra dot from '.columnName' if tableAlias was not present & doctrine's nested results #37

Open alihammad-gist opened 9 years ago

alihammad-gist commented 9 years ago

Also added the ability to reference nested "aliased" results generated by queries like below. I have not changed the behavior which was present already. Sorry for un-necessary conflicts because of my auto formatting tool .

Select r as rank, COUNT(m) as members_count FROM Staff\Entity\Rank r LEFT JOIN r.members as m GROUP BY r.id

Sample result set.

array(2) {
  [0]=>
  array(2) {
    ["rank"]=>
    string(17) "Staff\Entity\Rank"
    ["members_count"]=>
    string(1) "4"
  }
  [1]=>
  array(2) {
    ["rank"]=>
    string(17) "Staff\Entity\Rank"
    ["members_count"]=>
    string(1) "4"
  }
}

As you can see the immediate child of top-level array is another array not the entity itself. Below is a sample DataTable for queries like this.

<?php
//... irrelevant stuff

class Rank extends AbstractTable
{

    protected $urlPlugin;

    protected $config = array(
        'showPagination'    => true,
        'showQuickSearch'   => false,
        'showItemPerPage'   => false,
        'showColumnFilters' => false,
    );

    // Definition of headers
    protected $headers = array(
        'id' => array(
            'tableAlias' => 'rank',
            'title'      => 'Id',
        ),
        'title' => array(
            'tableAlias' => 'rank',
            'title'      => 'Title',
        ),
        'members_count' => array(
            'title' => 'Number of Assignees',
        ),
        'actions' => array(
            'tableAlias' => 'rank',
            'title'      => 'Actions',
            'width'      => '170',
            'sortable'   => false,
        ),
    );

    // table classes
    protected $class = array('table', 'table-condensed', 'table-hover', 'table-striped', 'dataTable');

    public function init()
    {

        /**
         *
         * @var UrlPlugin
         */
        $urlPlugin = $this->getUrlPlugin();

        // Actions Delete / Edit
        $this->getHeader('actions')->getCell()->addDecorator('callable', array(
            'callable' => function ($context, $record) use ($urlPlugin) {
                $record = $record['rank'];// <-- using alias
                // rank MD is unedit/delete-able
                if ($record->getTitle() == 'MD') {
                    return '"Default Rank"';
                }

                $editUrl = $urlPlugin('zfcadmin/staff/rank/edit', array(
                    'rankId' => $record->getId()
                ));
                $deleteUrl = $urlPlugin('zfcadmin/staff/rank/delete', array(
                    'rankId' => $record->getId()
                ));
                $rankTitle = $record->getTitle();
                return '
                <button class="btn btn-primary btn-xs" onclick="window.location = \'' . $editUrl . '\'"><span class="glyphicon glyphicon-edit"></span> Edit</button>
                <button class="btn btn-danger btn-xs" onclick="if (window.confirm(\'Do you really want to Remove ' . $rankTitle . '\')) window.location = \'' . $deleteUrl . '\'">
                    <span class="glyphicon glyphicon-remove"></span> Remove
                </button>
                ';
            }
        ));

        // // Ranks
        // $this->getHeader('members_count')->getCell()->addDecorator('callable', array(
        //     'callable' => function ($context, $record) {
        //         $collection = $record->getMembers();
        //         return count($collection);
        //     }
        // ));
    }

    public function setUrlPlugin(UrlPlugin $urlPlugin)
    {
        $this->urlPlugin = $urlPlugin;
    }

    protected function getUrlPlugin()
    {
        return $this->urlPlugin;
    }
}
alihammad-gist commented 9 years ago

Don't merge this, This alias is being used in Order By clause too, which doesn't work.

Select r as rank, COUNT(m) as members_count FROM Staff\Entity\Rank r LEFT JOIN r.members as m GROUP BY r.id ORDER BY rank.id asc