Skip to content

Doctrine 2, joining with other table and issue with sort-able field. #89

@utterly-calm

Description

@utterly-calm
$qb->select("s","u")
      ->add('from', 'Application\Entity\Schools s')
      ->leftJoin('s.user', 'u', 'WITH', 'u.id = s.user');

protected $headers = array(
    'id' =>     array('tableAlias' => 's', 'title' => 'Id', 'width' => '50', 'sortable' => false) ,
        'name' =>   array('tableAlias' => 's', 'title' => 'Name' , 'filters' => 'text') ,
        'user' =>   array('tableAlias' => 'u', 'title' => 'Entered By' , 'filters' => 'text') ,
        'createdAt' =>   array('tableAlias' => 's', 'title' => 'Created At' , 'filters' => 'text') ,
        'modifiedAt' =>   array('tableAlias' => 's', 'title' => 'Modified At' , 'filters' => 'text') ,
        'status' =>   array('tableAlias' => 's', 'title' => 'Status','filters' => array( null => 'All' , 1 => 'Active' , 0 => 'Inactive')) ,
);
public function init()
{
       $this->getHeader('user')->getCell()->addDecorator('callable', array(
            'callable' => function($context, $record){
                if(is_object($record->getUser())){
                    return $record->getUser()->getFirstname().' '.$record->getUser()->getLastname();
                }else{
                    return '-';
                }
            }
        ));
}

Users Entity:
/**
* @ORM\OneToMany(targetEntity="Application\Entity\Schools", mappedBy="user")
*/
private $school;

Schools Entity:
/**
 * @var \Application\Entity\Users
 *
 * @ORM\ManyToOne(targetEntity="Application\Entity\Users", inversedBy="school")
 * @ORM\JoinColumns({
    *   @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */
 private $user;

Everythings working fine including proper listing, individual search on each column, pagination and so on. But when sorted by "Entered By" (user) column, it displays following error:

[Semantical Error] line 0, col 95 near 'user asc': Error: Class Application\Entity\Users has no field or association named user

I understand that why this error is happening but how to resolve it. How to customize sort-able columns. I think there must be way to resolve it using doctrine 2 query builder or the other way could be to modify it inside zftable methods. I'm not sure how to resolve it.

Thanks for any help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions