|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ZfTable ( Module for Zend Framework 2) |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2013 Piotr Duda [email protected] |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace ZfTable\Example\TableExample; |
|
10
|
|
|
|
|
11
|
|
|
use ZfTable\AbstractTable; |
|
12
|
|
|
|
|
13
|
|
|
class Doctrine extends AbstractTable |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
protected $config = array( |
|
17
|
|
|
'name' => 'Doctrine 2', |
|
18
|
|
|
'showPagination' => true, |
|
19
|
|
|
'showQuickSearch' => false, |
|
20
|
|
|
'showItemPerPage' => true, |
|
21
|
|
|
'showColumnFilters' => true, |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array Definition of headers |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $headers = array( |
|
28
|
|
|
'idcustomer' => array('tableAlias' => 'q', 'title' => 'Id', 'width' => '50') , |
|
29
|
|
|
'doctrine' => array( |
|
30
|
|
|
'tableAlias' => 'q', |
|
31
|
|
|
'title' => 'Doctrine closure' , |
|
32
|
|
|
'filters' => 'text', |
|
33
|
|
|
'sortable' => false |
|
34
|
|
|
), |
|
35
|
|
|
'product' => array('tableAlias' => 'p', 'title' => 'Product', 'filters' => 'text'), |
|
36
|
|
|
'name' => array('tableAlias' => 'q', 'title' => 'Name', 'filters' => 'text') , |
|
37
|
|
|
'surname' => array('tableAlias' => 'q', 'title' => 'Surname', 'filters' => 'text'), |
|
38
|
|
|
'street' => array('tableAlias' => 'q', 'title' => 'Street', 'filters' => 'text'), |
|
39
|
|
|
'city' => array('tableAlias' => 'q', 'title' => 'City', 'filters' => 'text'), |
|
40
|
|
|
'active' => array('tableAlias' => 'q', 'title' => 'Active', 'width' => 100 ), |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
public function init() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->getHeader('doctrine')->getCell()->addDecorator('callable', array( |
|
46
|
|
|
'callable' => function ($context, $record) { |
|
47
|
|
|
return $record->name . ' ' . $record->surname; |
|
48
|
|
|
} |
|
49
|
|
|
)); |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$this->getHeader('product')->getCell()->addDecorator('callable', array( |
|
53
|
|
|
'callable' => function ($context, $record) { |
|
54
|
|
|
|
|
55
|
|
|
if (is_object($record->product)) { |
|
56
|
|
|
return $record->product->product; |
|
57
|
|
|
} else { |
|
58
|
|
|
return ''; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
//The filters could also be done with a parametrised query |
|
65
|
|
|
protected function initFilters($query) |
|
66
|
|
|
{ |
|
67
|
|
View Code Duplication |
if ($value = $this->getParamAdapter()->getValueOfFilter('name')) { |
|
|
|
|
|
|
68
|
|
|
$query->where($query->expr()->like('q.name', '?1'))->setParameter('1', '%' . $value . '%'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
if ($value = $this->getParamAdapter()->getValueOfFilter('surname')) { |
|
|
|
|
|
|
72
|
|
|
$query->where($query->expr()->like('q.surname', '?1'))->setParameter('1', '%' . $value . '%'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($value = $this->getParamAdapter()->getValueOfFilter('doctrine')) { |
|
|
|
|
|
|
76
|
|
|
$query->where("q.name like '%".$value."%' OR q.surname like '%".$value."%' "); |
|
77
|
|
|
$expr = $query->expr(); |
|
78
|
|
|
$query->where($expr->orX($expr->like('q.name', '?1'), $expr->like('q.surname', '?2'))) |
|
79
|
|
|
->setParameter('1', '%' . $value . '%') |
|
80
|
|
|
->setParameter('2', '%' . $value . '%'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
if ($value = $this->getParamAdapter()->getValueOfFilter('street')) { |
|
|
|
|
|
|
84
|
|
|
$query->where($query->expr()->like('q.street', '?1'))->setParameter('1', '%' . $value . '%'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
if ($value = $this->getParamAdapter()->getValueOfFilter('city')) { |
|
|
|
|
|
|
88
|
|
|
$query->where($query->expr()->like('q.city', '?1'))->setParameter('1', '%' . $value . '%'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
if ($value = $this->getParamAdapter()->getValueOfFilter('product')) { |
|
|
|
|
|
|
92
|
|
|
$query->where($query->expr()->like('q.product', '?1'))->setParameter('1', '%' . $value . '%'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: