Check for duplicates
Check for duplicates
nskwortsow
Posts: 120Questions: 0Answers: 0
Hi Allan,
In your API, how would I check for the existence of a duplicate value (e.g. each patient should have a unique email address), and report an error back to editor?
Thanks,
Nathan
In your API, how would I check for the existence of a duplicate value (e.g. each patient should have a unique email address), and report an error back to editor?
Thanks,
Nathan
This discussion has been closed.
Replies
Allan
Interested in that too, can you give to us a basic example ?
Best and thanks in advance.
$editor = Editor::inst( $db, 'tablename', 'id' )
->fields(
Field::inst( 'idtodepuplicate' )->validator( function($val, $data, $opts) {
//how to query and do anything ?
return true;
} )
)
);
You'd use the `select` method for the DB instance: http://editor.datatables.net/docs/current/php/class-DataTables.Database.html#_select . Then based on the return from that, you'd make a decision on if it is valid or not - the `result` instance from the `select` has a `count` method which would be perfect to check if any rows were found or not: http://editor.datatables.net/docs/current/php/class-DataTables.Database.Result.html#_count
Regards,
Allan
I am currently on this page but I am unable to use it without examples.
I looked for examples such as:
http://datatables.net/forums/discussion/13149/filtering-query-where/p1#
and
http://www.datatables.net/forums/discussion/5788/server-side-processing-with-ms-sql-database/p1#
[code]
$editor = Editor::inst( $db, 'tablename', 'id' )
->fields(
Field::inst( 'idtodepuplicate' )->validator( function($val, $data, $opts) {
$count = $db
->query( 'select', 'tablename' )
->get( 'id' )
->where( 'idtodepuplicate',$val,'=' )
->exec()
->count();
}
if($count !== 0){
return "Duplicate";
}
return true;
} )
)
);
//right ?
[/code]
But I can not apply. I do not understand the usage syntax.
Best,
[code]
$rows = $db
->select( 'tablename', 'id', array(
'id' => $val
) )
->count();
[/code]
That will give you the number of rows in the `$rows` variable.
I see that your trial has actually expired now. How have you found it? Are there any other questions you have about Editor?
Regards,
Allan
I currently access here : http://editor.datatables.net/docs/current/php/class-DataTables.Database.Query.html
I wanted to take a license, but the use of the Editor lack of examples. Unlike DataTables, Editor tool is not explicit enough.
Thanks for this example i will test it, and thanks for all your answers in many posts, (help me a lot).
Perhaps you could explain what examples you think are missing from the list here: http://editor.datatables.net/examples/ and I'll take a look at adding them.
Regards,
Allan
[code]
Editor::inst( $db, 'xm_programmes', 'ID' )
->fields(
Field::inst( 'P' )->validator( function($val, $data, $db) {
if ( empty($val) ) {
return "This field is required.";
}
$checkduplicate = $db
->select( 'xm_programmes', 'ID', array(
'P' => $_POST['data']['P']
) )
->count();
if($checkduplicate !== 0){
return "Duplicate !";
}elseif(!is_numeric($val)){
return "This field must be numeric only.";
}
return true;
} ),
[/code]
I got Fatal error: Call to undefined method DataTables\Editor\Field::select()
What's wrong ?
Thanks
You need the `$db` variable, so just add `global $db;` in the closure.
Allan
Thanks for your help, it work now.
Maybe this code will help someone, how to check duplicate on the browser field :
[code]
<?php
/*
* Example PHP implementation used for the index.html example
* DataTables Editor basic example
*/
// DataTables PHP library
include( "lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'browsers' )
->fields(
Field::inst( 'engine' )->validator( 'Validate::required' ),
Field::inst( 'browser' )->validator( function($val, $data, $opts) {
global $db;
if ( empty($val) ) {
return "This field is required.";
}else{
$checkdup = $db
->select( 'browsers', 'id', array(
'browser' => mysql_real_escape_string($val)
) )
->count();
if($checkdup !== 0){
return "Duplicate browse.";
}
}
return true;
} ),
Field::inst( 'platform' ),
Field::inst( 'version' ),
Field::inst( 'grade' )->validator( 'Validate::required' )
)
->process( $_POST )
->json();
[/code]
Best
This code helps in solving duplication issue while posting new row (create) But while editing this code blocks any changes saying it is duplication. Kindly advice to get ride of counting same raw which is being modified by current post.
Thanking you.
Allan
But how to avoid duplication while editing/modifying data
I not possible, how to hide a particular field only while editing so that it can't be changed.
Thank u
$checkdup1 = $db
->select( 'browsers', 'id', array(
'browser' => $val
) )
->count();
if($checkdup1 =>2){
return "Duplicate browse.";}
if($checkdup1 ==1){
///here checking for duplication is caused by the same row which is being edited//
$checkdup2 = $db
->select( 'browsers', 'id', array(
'browser' => $val,'id'=>$id
) )
->count();
if($checkdup2 !== 1){
return "Duplicate browse.";
}
[/code]
This gives me Undefined variable id, Please help with this
As I said, check the submitted action before doing the duplicate check.
[code]
if ( $_POST['action'] === 'create' ) {
// Do duplicate check
}
[/code]
> This gives me Undefined variable id, Please help with this
What is $id meant to be? The ID sent from the client? Then you want to use `$_POST['id']` no?
Allan
Here in checkup1 if it is found one more duplication I have to make sure that it is not the same row I am editing with.
$id here is the id of the row being edited. I have the field name id in the data base, and want to make sure that it is not the one i counted as duplicate . I am getting Undefined index: id while using $_POST['id']
Allan
[code]
global $args;
if($args==""){$postcheck=$_POST['action'];}else{$postcheck=$args['action']; $postid=trim($args['id'],"row_");}
if($postcheck=='create' ){..............................}
if($postcheck=='edit' ){..............................}
[/code]
may be useful for some one.