Ah.. yes. in 0.7.1 it still had a bit of an ugly error message for this. This is fixed in 1.0
Best way is probably to do this is with a validate method.
e.g. validate__userid(&$record, $value, &$params){
$res = mysql_query("select userid from foo where userid='".addslashes($value)."' limit 1", df_db());
if ( mysql_num_rows($res) > 0 ){
$params['message'] = 'This user already has an entry...';
return false;
}
return true;
}
In 0.7.1 there may also be a bit of a bug you need to fix as well for this to work. Check
http://xataface.com/forum/viewtopic.php ... c&start=15
for info on how to fix this.
Ref:
http://xataface.com/documentation/how-t ... validation
An alternative approach is to do this in the beforeInsert() method. See the handling errors section in:
http://xataface.com/documentation/tutor ... x/triggers
Now if you want the "new record" button to just not appear altogether there are two ways to do this.
One way is in the getPermissions() method, the other is to override the 'new' action with a different condition. And for this to work you need to know the value that the user would input into the new record form before he gets there (otherwise we don't know if the record is a duplicate).
Now suppose you have a function alreadyPostedRecord() which checks to see if this user has already posted a record. We might implement the getPermissions() method as follows:
- Code: Select all
function getPermissions(&$record){
$perms = Dataface_PermissionsTool::ALL();
if ( alreadyPostedRecord() ) unset($perms['new']);
return $perms;
}
!!!!Important. The getPermissions() method may be called dozens (or even hundreds of times per request, so it is important that your alreadyPostedRecord() function doesn't do a lot of work. You should cache results. E.g. make sure it doesn't perform any database or filesystem calls or any other expensive function, unless it only performs this the first time it is called, and returns the cached value the rest of the time.
One way to do this is with a static variable to cache the value.
e.g.
- Code: Select all
function alreadyPostedRecord(){
static $ret = -1;
if ( $ret == -1 ){
$res = mysql_query("select userid from foo where userid='".getUserID()."' limit 1", df_db());
if ( mysql_num_rows($res) > 0 ) $ret = 1;
else $res = 0;
}
return $ret;
}
That way it only executes the code inside the if statement once....
Finally you could override the new action in your actions.ini file to use a different condition as follows:
- Code: Select all
[new > new]
condition="!alreadyPostedRecord()"
Essentially this says to display the 'new' action if the alreadyPostedRecord() function returns false (i.e. the record is not already posted.
But the best solution is probably to start working with 1.0