Skip to content

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

Open
alihammad-gist wants to merge 1 commit intodudapiotr:masterfrom
alihammad-gist:master
Open

Removed extra dot from '.columnName' if tableAlias was not present & doctrine's nested results#37
alihammad-gist wants to merge 1 commit intodudapiotr:masterfrom
alihammad-gist:master

Conversation

@alihammad-gist
Copy link

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;
    }
}

…d added the ability to reference nested object from doctrine resultset
@alihammad-gist alihammad-gist changed the title Removed extra dot from '.columnName' if tableAlias was not present Removed extra dot from '.columnName' if tableAlias was not present & doctrine's nested results Oct 17, 2014
@alihammad-gist
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant