Hello.
I'm not sure whether or not this will be a flourish related problem. I'm running in trouble with a prepared statement for inserting data which includes a select as well.
Within my groups management application I have defined both 'roles' and 'functions' tables, with the usual 'role_functions' joint table, as well. After the user creates a group, the roles table is filled with some default, predefined roles for that group, each one of these having a predefined ACL with its list of permitted functions, i.e. for 'member' role:
$default_ACLs['member'] => array(
'resources' => array('view', 'new', 'edit'),
'users' => array('view')
)
As far as I need the role id to fill the role_functions records, and also to search at the functions table for those actions (actually, 'resources_view', 'resources_new', and so on) to put those ids in the join table, I have split the process in two parts, defining the following two methods in the Role class:
/**
* Set default roles and functions for new group
*/
static public function addInitialRoles( $id_group )
{
$stmt = $GLOBALS['db']->prepare("INSERT INTO role_functions (id_role, id_function)
VALUES (%i, (SELECT id FROM functions WHERE name = '%s'))");
foreach( $GLOBALS['config']->default_roles as $role_name ) {
// Add the new role
$id_role = self::newRole( $role_name, $id_group );
// Add the role-functions join records
foreach ($GLOBALS['config']->default_ACLs[$role_name] as $module => $perms_array) {
foreach ( $perms_array as $action ) {
$function_name = $module . '_' . $action;
$GLOBALS['db']->query( $stmt, $id_role, $function_name);
}
}
}
}
/**
* Add a role name to a group
*/
static public function newRole( $role_name, $id_group )
{
try {
$role = new Role();
$role->setName( $role_name );
$role->setIdGroup( $id_group );
$role->store();
$id_role = $role->getId();
return $id_role;
} catch (fExpectedException $e) {
return false;
}
}
The newRole() method goes fine, and the new role id is successfully returned. However, when It comes to save the list of authorized functions, the prepared statement fails with the following errors:
Warning: vsprintf() [function.vsprintf]: Too few arguments in /usr/local/share/php/flourish/classes/fStatement.php on line 99
Warning: vsprintf() [function.vsprintf]: Too few arguments in /usr/local/share/php/flourish/classes/fStatement.php on line 139
Fatal error: Uncaught exception 'fSQLException' with message 'PostgreSQL error (ERROR: bind message supplies 2 parameters, but prepared statement "fstmt2" requires 0) in {false}' in /usr/local/share/php/flourish/classes/fDatabase.php:421 Stack trace:
#0 /usr/local/share/php/flourish/classes/fDatabase.php(2198): fDatabase->checkForError(Object(fResult), NULL)
#1 /usr/local/share/php/flourish/classes/fDatabase.php(2679): fDatabase->performQuery(Object(fStatement), Object(fResult), Array)
#2 /usr/local/share/php/flourish/classes/fDatabase.php(2580): fDatabase->run(Object(fStatement), 'fResult', Array)
#3 /usr/local/www/agoravirtual_v6/applications/vomanager/private/models/Role.php(45): fDatabase->query(Object(fStatement), '4', 'resources_view')
#4 /usr/local/www/applications/manager/private/controllers/groups_new.php(93): Role::addInitialRoles('5')
(Oddly enough, I get a 'Too few arguments' followed by a 'supplies 2 parameters, but "fstmt2" requires 0').
I have succesfully used the INSERT...(SELECT construct in other applications, (albeit "manually"), but it seems that for some reason it doesn't run within a prepared statement.
Am I missing something here? (Of course, if there is another, cleaner, flourish-way to do that, I'll be glad to hear of it.)
Thanks in advance, A.