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

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

Open utterly-calm opened 8 years ago

utterly-calm commented 8 years ago
$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.