Problem/Motivation
The DBTNG Example needs to show some additional things. Please chime in here. We need at least:
- How to do an update or delete matching a "like" query.
- A transaction #2986423: Add a transaction example
- A delete with more than one condition
- An update with more than one condition
- A condition based on subquery (from @tanoshimi)
- A join
- A static query
- range(): successor of db_query_range()
- ordering #2792251: DBTNG Example needs ordered list example
- random ordering
- grouping
- Tagging and hooks in queries #2390731: Add DBTNG example which uses addTag().
Proposed resolution
Want to take on one of these things? Great! Add a child issue for one of the items above.
Remaining tasks
User interface changes
API changes
Data model changes
Comments
| Status | File | Size |
|---|---|---|
| new | 718672_range_at_least.patch | 878 bytes |
A start ;)
I probably won't do many more but needed to learn to do this and found it missing in example.module
A good start :-)
Committed #3, thanks.
What I'd like to see in that module is a whole "recipe" section with "all" the things that are missing. Lots of practical things people need.
It wouldn't bother me if there were no UI at all, but if we can get a testable, full-quality set of recipes in here I'd be really happy.
Good comments
Real running queries
Explanations of what the related SQL would be
Tests for each
I'd actually love to see simpler examples to copy and paste. :-)
OK, so it just needs lots more examples :-)
Mentioning this one #1028168: Create a page to show results from database including a pager. in case someone gets excited. :-)
Comment #13
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | filter_form_for_list_page-718672-13.patch | 3.49 KB |
I have patched filter form for the listing page; a new easy to learn stuff in this module.
Kindly review.
Thanks!
Comment #14
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-14.patch | 2.55 KB |
Also review the new patch containing "Ordered List" example.
Thanks!
The last submitted patch, 14: ordered_list_example-718672-14.patch, failed testing.
Comment #16
aburrows commented-
+++ b/dbtng_example/dbtng_example.module @@ -1,4 +1,5 @@ +Remove this linebreak
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +585,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { +/*Doxygen should be /**
+++ b/dbtng_example/dbtng_example.module
@@ -574,6 +585,43 @@ function dbtng_example_form_update_submit($form, &$form_state) {
+ // Make a table for them.
+ $header = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age'));
+ $output .= theme('table', array('header' => $header, 'rows' => $rows));
This is not an ordered list, but a table.
Comment #18
minakshiPh commented+ $select = db_select('dbtng_example', 'ex');
+ $select->fields('ex', array('pid', 'uid', 'name', 'surname', 'age'));
+ // 'ex.name' is the field on which to order.
+ // Legal values for sorting are "ASC" and "DESC". Any other value will be converted to "ASC".
+ $select->orderBy('ex.name', 'ASC');
+ // generate the result in object format.
+ $result = $select->execute()->fetchAll();This query will create an ordered list.
Comment #19
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-19.patch | 2.44 KB |
corrected the patch as mentioned in #16 and attached the same.
Kindly review.
Thanks!
I think the example would be better if it is called "Ordering results" or something similar as that is what the example is about.
-
+++ b/dbtng_example/dbtng_example.module @@ -344,6 +344,10 @@ function dbtng_example_help($path) { + $output = t('Generate an ordered list of all the records in the database. '); + $output .= t('The records will be displayed in ascending order for column "Name".');Extraneous whitespace after
database..It seems to me that this string should be on one line in one t() call.
-
+++ b/dbtng_example/dbtng_example.module @@ -388,6 +392,12 @@ function dbtng_example_menu() { + 'access callback' => TRUE,I know the internet is littered with examples using TRUE as an access callback. Using TRUE as an access callback is however dangerous in production code and has been known to cause security issues. It should therefore be prevented in any code you write to ensure you never accidentally leave access to menu items unchecked. Try to always use a genuine access callback (even if it's just checking if a user has access to the admin pages or permission to view content for example).
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +584,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { + * This function queries the database and retrives all the records in an ascending order.This comment exceeds 80 characters.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +584,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { + * SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname, ex.age AS ageThis comment exceeds 80 characters.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +584,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { + $output = '';This should be declared closer to where it is used. Besides that, this menu callback should actually return a render array instead of a string.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +584,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { + $select = db_select('dbtng_example', 'ex'); + $select->fields('ex', array('pid', 'uid', 'name', 'surname', 'age')); + // 'ex.name' is the field on which to order. + // Legal values for sorting are "ASC" and "DESC". Any other value will be converted to "ASC". + $select->orderBy('ex.name', 'ASC'); + // generate the result in object format. + $result = $select->execute()->fetchAll();If you extract this section into it's own method named something like dbtng_example_execute_ordered_select_query, then the dev reading the example would immediately know where to look for the actual Database API example.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +584,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { + $select->fields('ex', array('pid', 'uid', 'name', 'surname', 'age'));I'm not sure about the schema of the dbtng_example table, but it seems to me like you are selecting all fields in the table.
If that is the intention, you can just do the following:
$select->fields('ex');Or even better
$select = db_select('dbtng_example', 'ex')->fields('ex');That way you can direct the reader's focus on the
$select->orderBy('ex.name', 'ASC')call. -
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +584,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { + // Legal values for sorting are "ASC" and "DESC". Any other value will be converted to "ASC".This comment exceeds 80 characters.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +584,43 @@ function dbtng_example_form_update_submit($form, &$form_state) { + if ($result) { + $rows = array(); + foreach ($result as $row) { + // Sanitize the data before handing it off to the theme layer. + $rows[] = array_map('check_plain', (array) $row); + } + // Make a table for them. + $header = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age')); + $output .= theme('table', array('header' => $header, 'rows' => $rows)); + } + else { + drupal_set_message(t('No records found.')); + } + return $output;You could also extract this into a function dbtng_example_render_resultset_as_table to make sure that the reader does not have to filter out information that is not relevant to the database API.
Comment #22
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-22.patch | 3.28 KB |
| new | interdiff-718672-19-22.txt | 3.74 KB |
@legolasbo: The mentioned modifications have been made in the new patch and also attached the interdiff.
Kindly review.
Thanks!
-
+++ b/dbtng_example/dbtng_example.module @@ -346,8 +346,7 @@ + $output = t('Generate a list of database records in Ordering format. The records will be displayed in ascending order for column "Name".');'Ordering' should be 'ordering'
-
+++ b/dbtng_example/dbtng_example.module @@ -395,7 +394,7 @@ + 'access arguments' => array('access dbtng example ordered list'),This is too specific. 'access content' or 'access administration pages' will do.
-
+++ b/dbtng_example/dbtng_example.module @@ -403,6 +402,22 @@ + * Implements hook_permission(). + * + * This hook can supply permissions that the module defines, so that they can + * be selected on the user permissions page and used to grant or restrict + * access to actions the module performs. + */ +function dbtng_example_permission() { + return array( + 'access dbtng example ordered list' => array( + 'title' => t('Permission to view an Ordered List'), + 'description' => t('Give permission to access an Ordered List.'), + ), + ); +} + +/**This can be removed if you use one of the permissions mentioned above
-
+++ b/dbtng_example/dbtng_example.module @@ -588,39 +603,46 @@ + * This function queries the database and retrives all the records in an + * ascending order./s/retrives/retrieves
/s/in an ascending/in ascending -
+++ b/dbtng_example/dbtng_example.module @@ -588,39 +603,46 @@ + * SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname,Maybe explain that the code below will result in the following query
-
+++ b/dbtng_example/dbtng_example.module @@ -588,39 +603,46 @@ *Excess blank comment line
-
+++ b/dbtng_example/dbtng_example.module @@ -588,39 +603,46 @@ - $output = ''; - - $select = db_select('dbtng_example', 'ex'); - $select->fields('ex', array('pid', 'uid', 'name', 'surname', 'age')); + $dbtng_example_execute_ordered_select_query = db_select('dbtng_example', 'ex')->fields('ex'); // 'ex.name' is the field on which to order. - // Legal values for sorting are "ASC" and "DESC". Any other value will be converted to "ASC". - $select->orderBy('ex.name', 'ASC'); + // Legal values for sorting are "ASC" and "DESC". Any other value will be + // converted to "ASC". + $dbtng_example_execute_ordered_select_query->orderBy('ex.name', 'ASC'); // generate the result in object format. - $result = $select->execute()->fetchAll(); + $dbtng_example_result = $dbtng_example_execute_ordered_select_query->execute()->fetchAll(); - if ($result) {I think you misunderstood me here. I meant something as follows:
$result = dbtng_example_execute_ordered_select_query(); foreach ($result as $row) { .. Generate the table .. } -
+++ b/dbtng_example/dbtng_example.module @@ -588,39 +603,46 @@ + foreach ($dbtng_example_result as $row) { // Sanitize the data before handing it off to the theme layer. $rows[] = array_map('check_plain', (array) $row); } - // Make a table for them. - $header = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age')); - $output .= theme('table', array('header' => $header, 'rows' => $rows)); + $output .= dbtng_example_render_resultset_as_table($rows);The foreach should actually be inside of the dbtng_example_render_resultset_as_table() function
Comment #25
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-25.patch | 2.92 KB |
| new | interdiff-718672-22-25.txt | 3.79 KB |
@legolasbo: Modified the code as mentioned and also attached the interdiff.
Kindly review.
Thanks!
Review of the interdiff:
-
+++ b/dbtng_example/dbtng_example.module @@ -602,17 +586,30 @@ + $output = ''; + if ($result) { + $output .= dbtng_example_render_resultset_as_table($result); + } + else { + drupal_set_message(t('No records found.')); + } + return $output;Checking wether or not there is something to render should be the task of dbtng_example_render_resultset_as_table. The entire method could be as simple as:
dbtng_example_ordered_list() { $result = dbtng_example_execute_ordered_select_query(); dbtng_example_render_resultset_as_table($result); }Pretty self-descriptive no? You could even change the function's docblock to "Menu callback."
-
+++ b/dbtng_example/dbtng_example.module @@ -620,27 +617,22 @@ + // return the result + return $dbtng_example_result;This comment is redundant.
return $dbtng_example_result;almost literally states 'return the result'
Review of the entire patch (without previous comments from the interdiff)
+++ b/dbtng_example/dbtng_example.module
@@ -574,6 +583,58 @@ function dbtng_example_form_update_submit($form, &$form_state) {
+ $output = theme('table', array('header' => $header, 'rows' => $rows));
+ return $output;
If you render the to html here, the output can't be changed anymore. If you however return a render array it can still be altered by the theme layer if required.
i.e.:
return [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
];
The method can then be renamed to something like 'dbtng_example_convert_resultset_to_table_render_array()' which would make it self-describing.
Comment #27
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-27.patch | 3.04 KB |
| new | interdiff-718672-25-27.txt | 1.86 KB |
Corrections are made in the updated patch with interdiff.
Kindly review.
Thanks!
-
+++ b/dbtng_example/dbtng_example.module @@ -344,6 +344,9 @@ function dbtng_example_help($path) { + $output = t('Generate a list of database records in ordering format. The records will be displayed in ascending order for column "Name".');"Generate a list of database records ordered by the 'name' column in ascending order." reads better I think.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,62 @@ function dbtng_example_form_update_submit($form, &$form_state) { + * Render an ordered list of entries in the database.Menu callback demonstrating how to retrieve ordered results from the database.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,62 @@ function dbtng_example_form_update_submit($form, &$form_state) { + return dbtng_example_render_resultset_as_table($result);return dbtng_example_convert_resultset_to_table_render_array($result); -
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,62 @@ function dbtng_example_form_update_submit($form, &$form_state) { + $rows = array(); + if($result) { + foreach ($result as $row) { + // Sanitize the data before handing it off to the theme layer. + $rows[] = array_map('check_plain', (array) $row); + } + }This should live in a method called
dbtng_example_convert_resultset_to_table_rows()and be called fromdbtng_example_convert_resultset_to_table_render_array()
This is also missing tests that prove that everything works as expected.
Comment #30
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-30.patch | 3.05 KB |
| new | interdiff-718672-27-30.txt | 1.81 KB |
Corrections are made as mentioned in #28
Kindly review.
Thanks!
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { +/** + * This function demonstrates retrieving of ordered results from the database + */This docblock is missing an @return annotation.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { +/** + * This function queries the database and retrieves all the records in + * ascending order. + * + * The code below will result in the following query + * SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname, + * ex.age AS age + * FROM {dbtng_example} ex + * ORDER BY ex.name ASC + */this docblock is missing a @return annotation.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { + $dbtng_example_execute_ordered_select_query = db_select('dbtng_example', 'ex')->fields('ex');Maybe you should add a comment explaining the 'ex'. Or even better replace all 'ex' with $tableAlias.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { + // 'ex.name' is the field on which to order.Maybe this comment should also explain what 'ex' and 'name' are exactly.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { + // generate the result in object format.I'm not sure what you mean this comment.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { + return $dbtng_example_result;An empty line above the return statement more clearly separates the 2 logical steps querying and returning data.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { +/** + * This function renders a resultset to table rows + */ +function dbtng_example_render_resultset_to_table_rows($result) {This function does not render anything, it just converts the resultset into table rows.
'dbtng_example_convert_resultset_into_table_rows'
Besides that, the docblock is missing @param and @return annotations.
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { + $rows[] = array_map('check_plain', (array) $row);Doesn't this result in a pass by reference warning?
-
+++ b/dbtng_example/dbtng_example.module @@ -574,6 +583,63 @@ function dbtng_example_form_update_submit($form, &$form_state) { + $rows = dbtng_example_render_resultset_to_table_rows($result);You can inline this variable.
Comment #32
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-32.patch | 3.43 KB |
| new | interdiff-718672-30-32.txt | 2.35 KB |
Added annotations as mentioned in #31
Kindly review.
Thanks!
-
+++ b/dbtng_example/dbtng_example.module @@ -601,22 +602,31 @@ - $dbtng_example_execute_ordered_select_query = db_select('dbtng_example', 'ex')->fields('ex'); - // 'ex.name' is the field on which to order. + $dbtng_example_execute_ordered_select_query = db_select('dbtng_example', 'dbtng_example')->fields('dbtng_example'); + // 'dbtng_example.name' is the field on which to order where 'dbtng_example' + // is a table alias and 'name' is a column namethis doesn't improve the example. Something like the pseudocode below explains what is going on without the need for comments.
$tableName = 'dbtng_example'; $tableAlias = 'ex'; .. = $db_select($tableName, $tableAlias)->fields($tableAlias) -
+++ b/dbtng_example/dbtng_example.module @@ -601,22 +602,31 @@ +function dbtng_example_converts_resultset_to_table_rows($result) {/s/converts/convert
-
+++ b/dbtng_example/dbtng_example.module @@ -629,9 +639,15 @@ * This function renders array for table 'dbtng_example'This comment is incorrect. the function converts a resultset into a render array.
Comment #34
minakshiPh commented| Status | File | Size |
|---|---|---|
| new | ordered_list_example-718672-34.patch | 3.37 KB |
| new | interdiff-718672-32-34.txt | 1.89 KB |
corrections are made as mentioned in #33.
Kindly review.
Thanks!
The last submitted patch, 27: ordered_list_example-718672-27.patch, failed testing.
The last submitted patch, 30: ordered_list_example-718672-30.patch, failed testing.
The last submitted patch, 32: ordered_list_example-718672-32.patch, failed testing.
The last submitted patch, 34: ordered_list_example-718672-34.patch, failed testing.
Comment #39
manojbisht_drupal commented| Status | File | Size |
|---|---|---|
| new | dbtng_example.patch | 948 bytes |
Hi,
Attached is the patch for delete based on multiple condition.
The last submitted patch, 34: ordered_list_example-718672-34.patch, failed testing.
The last submitted patch, 34: ordered_list_example-718672-34.patch, failed testing.
Comment #42
manojbisht_drupal commentedThe last submitted patch, 39: dbtng_example.patch, failed testing.
@manojbisht_drupal - Your patch is not in proper git format, so doesn't apply. Full instructions are at https://www.drupal.org/node/707484
The last submitted patch, 34: ordered_list_example-718672-34.patch, failed testing.
Comment #46
manojbisht_drupal commented| Status | File | Size |
|---|---|---|
| new | 718672-multiple-delete-entry-condition.patch | 1.02 KB |
Hi rFay,
Please find the revised patch attached with the instructions on the link.
Thanks,
Manoj Bisht
Rescoping.
This is all good stuff. Moving to 8.x-1.x because that's current.
File an issue per item, and when it's in, we can backport to 7.x-1.x as needed in those child issues.
Moving to new examples meta
Let's move this to 4.0.x, which is for ^9.4 || ^10
Comment #1
rfayIt should also have:
A transaction
A delete with more than one condition
An update with more than one condition
A condition based on subquery (from @tanoshimi)
A join
A static query
range(): successor of db_query_range()
ordering
random ordering
grouping