|
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 ColumnFiltering extends AbstractTable |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
protected $config = array( |
|
17
|
|
|
'name' => 'Filtering by column', |
|
18
|
|
|
'showPagination' => true, |
|
19
|
|
|
'showQuickSearch' => false, |
|
20
|
|
|
'showItemPerPage' => true, |
|
21
|
|
|
'itemCountPerPage' => 10, |
|
22
|
|
|
'showColumnFilters' => true, |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array Definition of headers |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $headers = array( |
|
30
|
|
|
'idcustomer' => array('title' => 'Id', 'width' => '50') , |
|
31
|
|
|
'name' => array('title' => 'Name' , 'filters' => 'text'), |
|
32
|
|
|
'surname' => array('title' => 'Surname' , 'filters' => 'text' ), |
|
33
|
|
|
'street' => array('title' => 'Street' , 'filters' => 'text'), |
|
34
|
|
|
'city' => array('title' => 'City'), |
|
35
|
|
|
'active' => array('title' => 'Active' , 'width' => 100 , |
|
36
|
|
|
'filters' => array( null => 'All' , 1 => 'Active' , 0 => 'Inactive')), |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
public function init() |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function initFilters($query) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($value = $this->getParamAdapter()->getValueOfFilter('name')) { |
|
|
|
|
|
|
47
|
|
|
$query->where("name like '%" . $value . "%' "); |
|
48
|
|
|
} |
|
49
|
|
|
if ($value = $this->getParamAdapter()->getValueOfFilter('surname')) { |
|
|
|
|
|
|
50
|
|
|
$query->where("surname like '%" . $value . "%' "); |
|
51
|
|
|
} |
|
52
|
|
|
if ($value = $this->getParamAdapter()->getValueOfFilter('street')) { |
|
|
|
|
|
|
53
|
|
|
$query->where("street like '%" . $value . "%' "); |
|
54
|
|
|
} |
|
55
|
|
|
$value = $this->getParamAdapter()->getValueOfFilter('active'); |
|
|
|
|
|
|
56
|
|
|
if ($value != null) { |
|
57
|
|
|
$query->where("active = '" . $value . "' "); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
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: