Skip to content

Commit 67ea5e3

Browse files
committed
Database updated with real data; some minor bug fixes.
1 parent c6d890d commit 67ea5e3

File tree

5 files changed

+75
-19
lines changed

5 files changed

+75
-19
lines changed

Scaffolding.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,26 +1826,23 @@ private function prepareList($select)
18261826
}
18271827

18281828
foreach ($this->fields as $columnName => $columnDetails) {
1829-
1830-
if (!empty($columnDetails['hide']) && ($columnDetails['hide'] === true
1831-
|| $columnDetails['hide'] == 'list')) {
1832-
continue;
1833-
}
1834-
18351829
// Table fields have fully-qualified SQL name.
18361830
if (strpos($columnDetails['sqlName'], '.')) {
1837-
list($table, $column) = explode('.', $columnDetails['sqlName']);
1838-
// If alias exist or column not found by its SQL primary name, let's try alias.
1839-
if (empty($item[$column]) && !empty($item[$columnName])) {
1831+
// If alias exist let's try alias.
1832+
// @todo: or column not found by its SQL primary name,
1833+
if (!empty($item[$columnName])) {
18401834
$column = $columnName;
1835+
} else {
1836+
list($table, $column) = explode('.', $columnDetails['sqlName']);
18411837
}
18421838
}
18431839
// Computed fields have alias only.
18441840
else {
18451841
$column = $columnName;
18461842
}
18471843

1848-
$value = $item[$column];
1844+
// null values may be returned.
1845+
$value = !empty($item[$column]) ? $item[$column] : null;
18491846

18501847
// Call list view modifier for specific column if set
18511848
if (isset($columnDetails['listModifier'])) {
@@ -1858,7 +1855,6 @@ private function prepareList($select)
18581855

18591856
$row[$columnName] = $value;
18601857
}
1861-
18621858
// Fetch PK(s).
18631859
if (!is_null($info)) {
18641860
$keys = array();

tests/application/controllers/ReadersController.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,71 @@
33
class ReadersController extends Zend_Controller_Scaffolding
44
{
55

6-
public function init()
7-
{
6+
public function init() {
7+
parent::init();
8+
89
$fields = array(
910
'name' => array(
10-
'title' => 'First & last name',
11+
// The title of the column
12+
'title' => 'Name',
13+
// We want to be able to search by the reader's name
1114
'searchable'=> true,
15+
// We want to be able to sort by name
16+
'sortable'=> true,
17+
// The field is first in the list.
18+
'listOrder' => 1
1219
),
1320
'category' => array(
21+
// We don't want to show the ID in the list.
1422
'hide' => 'list',
23+
// Instead we want to show it as relevant
24+
// text field from a related table, defined as key of the next field.
25+
// Note: 'Category' is the name of reference rule defined in
26+
// current model class (Application_Model_Readers) inside _referenceMap variable.
1527
'displayField' => 'Category.name'
1628
),
29+
// The field from related table, that will replace meaningless ID.
1730
'Category.name' => array(
1831
'title' => 'Category',
32+
'listOrder' => 2,
33+
'sortable' => true
34+
),
35+
// A field from dependent table that we also want to show.
36+
// Note: 'Reader' is the name of the reference rule in the dependent
37+
// table's model, that points to the current model class,
38+
// 'balance' is the field we want to show, that belongs to dependent
39+
// table, NOT current table (i.e. not 'readers').
40+
// Important: this field is not intended for changes from the application
41+
// and is just for demo purposes.
42+
'Reader.balance' => array(
43+
'title' => 'Balance',
44+
'listOrder' => 3
1945
),
2046
'created' => array(
2147
'searchable'=> true,
22-
'fieldType' => 'jsPicker',
23-
'hide' => 'edit'
48+
// This field will be transmitted with other
49+
// fields of type 'jsPicker' to loadDatePicker method.
50+
// This allows to create a JS widget (calendar for date,
51+
// but any other if needed).
52+
'fieldType' => 'jsPicker',
53+
// We don't want to allow edit of this field
54+
'hide' => 'edit',
55+
'listOrder' => 5
2456
),
2557
'updated' => array(
2658
'hide' => 'edit',
27-
'listOrder' => 2
59+
'listOrder' => 4
2860
),
61+
// This field definition makes sense only for edit form
62+
// and allows to show list of assigned books through a
63+
// many-to-many table readers_books.
64+
// Note: 'Books' stands for the reference rule defined in the dependent table
65+
// Application_Model_ReadersBooks that points to books table, while
66+
// 'title' is the textual name of the related entity (book) that
67+
// substitutes a meaningless ID.
2968
'Books.title' => array(
3069
'title' => 'Assigned books',
31-
)
70+
),
3271
);
3372

3473
$this->scaffold(new Application_Model_Readers(), $fields, array('csrfProtected' => false, 'entityTitle' => 'reader', 'disabledActions' => array(self::ACTION_DELETE)));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
class Application_Model_ReaderAccounts extends Zend_Db_Table {
3+
protected $_name = 'reader_accounts';
4+
5+
// References the main table 'readers'
6+
protected $_referenceMap = array(
7+
'Reader' => array(
8+
'columns' => 'reader_id', // foreign key column
9+
'refTableClass' => 'Application_Model_Readers',
10+
'refColumns' => 'id'
11+
),
12+
);
13+
}
14+
?>

tests/application/models/Readers.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
class Application_Model_Readers extends Zend_Db_Table {
33
protected $_name = 'readers';
44

5+
// These MUST be defined if you want to use data from
6+
// related tables (especially for one-to-many relationships)
57
protected $_referenceMap = array(
68
'Category' => array(
79
'columns' => 'category', // foreign key column
@@ -10,6 +12,11 @@ class Application_Model_Readers extends Zend_Db_Table {
1012
),
1113
);
1214

13-
protected $_dependentTables = array('Application_Model_ReadersBooks');
15+
protected $_dependentTables = array(
16+
// This is a many-to-many table.
17+
'Application_Model_ReadersBooks',
18+
// And this is 1-1 table that fully depends on reader.
19+
'Application_Model_ReaderAccounts'
20+
);
1421
}
1522
?>
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)