Problem with afterInsert() function

A place for users and developers of the Xataface to discuss and receive support.

Problem with afterInsert() function

Postby muzafar » Wed Jan 25, 2012 1:46 am

Greetings,

I am facing problem with the afterInsert(). I am creating function like this.
function afterInsert(&$record)
{
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$app =& Dataface_Application::getInstance();
$query =& $app->getQuery();
//$record =& $app->getRecord();
$record = $record->val('id');

if ($record)
{
$res = mysql_query("SELECT * FROM requsition WHERE id = '".mysql_real_escape_string($record)."'",df_db());
if ( !$res ) throw new Exception(mysql_error(df_db()));
$row = mysql_fetch_assoc($res);

echo gettype ($row['requisitions']);
echo "<pre>";
print_r($row['requisitions']);
foreach ( $row['requisitions'] as $vals)
{
$res = mysql_query ("INSERT INTO requsition_bak (
req_id,
requisition_no,
item_name,
pricePerUnit,
NoofUnits,
) VALUES
('".$row['id']."',
'".mysql_real_escape_string($row['requisition_no'])."',
'".mysql_real_escape_string($vals['items'])."',
'".mysql_real_escape_string($vals['pricePerUnit'])."',
'".$vals['unitCount']."')", df_db());

if ( !$res ) throw new Exception(mysql_error(df_db()));
}
}
}

what I am doing is that I have one main table and one backup table. main table is. requsition and backup table is requisition_bak. I am inserting record to the main table first. in first table i have one field, name requisitions. for which i am using widget:type table, which insert records successfully in xml form. Now what i am doing here is that in afterInsert() function, i am trying to get the last inserted id, and then through this id i am retrieving records from requistion table to get requistions data of widget:type table and then parse it and insert it into the backup table, so that its easy for me while filtering. but this s throwing the following error.

Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\cvp\var\www\grants\tables\requsition\requsition.php on line 185

Catchable fatal error: Argument 1 passed to Dataface_IO::saveTransients() must be an instance of Dataface_Record, integer given, called in D:\xampp\htdocs\cvp\var\www\xata\Dataface\IO.php on line 687 and defined in D:\xampp\htdocs\cvp\var\www\xata\Dataface\IO.php on line 362

Also one more thing is that in afterInsert(), when i print_r the $record variable, it gives me nothing.

Kindly lead me.

Your help on the above will be greatly appreciated.

Thanks
muzafar
 
Posts: 44
Joined: Mon Nov 28, 2011 9:25 pm

Re: Problem with afterInsert() function

Postby shannah » Wed Jan 25, 2012 11:32 am

2 problems:

1. In the line:
Code: Select all
$record = $record->val('id');

you're assigning an integer to the $record variable... but the $record variable was passed by reference to afterInsert() so you're actually changing the record object to an integer... it affects the rest of the system.... If you want to assign the id to a variable don't use the same variable as the record.

2. In the line:
Code: Select all
foreach ( $row['requisitions'] as $vals)

$row['requisitions'] is not an array. It is a scalar value (it is just the contents of a single column of the first row of your SQL query.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Problem with afterInsert() function

Postby muzafar » Thu Jan 26, 2012 3:45 am

yes thanks for reply.. resolved..

so kind of you.
muzafar
 
Posts: 44
Joined: Mon Nov 28, 2011 9:25 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 21 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved