Ryan Gantt Sun Jan 23 17:38:29 -0500 2011

Subject: Troubles with after_save callback

Hey team,

I'm having trouble running an after_save callback in my model. Here is the model code in question:

class Statement extends ActiveRecord\Model {
static $belongs_to = array(
array( 'problem' )
);

static $after_save = array('revision');
public function revision()
{
$r = new Revision( array(
'foreign_id' => $this->id,
'type' => 'statement'
));
$r->save();
$sr = new StatementRevision( array(
'global_revision' => $r->id,
'statement' => $this->id,
'body' => $this->body,
'date' => time(),
'author_id' => '0'
));
}
}

class StatementRevision extends ActiveRecord\Model {
}
?>

And here is the relevant part of the schema:

CREATE TABLE IF NOT EXISTS `revisions` (
`revision_id` int(11) NOT NULL AUTO_INCREMENT,
`type` enum('statement','setup','solution') DEFAULT NULL,
`foreign_id` int(11) NOT NULL,
PRIMARY KEY (`revision_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `statements` (
`id` int(5) not null auto_increment,
`problem` int(5) not null default 0,
`body` TEXT default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `statementrevisions` (
`id` int(5) not null auto_increment,
`global_revision` int(5) not null default 0,
`statement` int(5) not null default 0,
`body` TEXT default null,
`date` int(11) not null,
`author` int(5) not null,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

I can't figure out how to get the pretty php formatting turned on, so this is a nightmare. No search feature and no formatting documentation makes this forum fun to use!

When I run save() on a new instance of Statement above, this is the error I get from PHP-AR:

Warning: array_unshift() expects parameter 1 to be array, string given in C:\htdocs\...\lib\CallBack.php on line 221

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\htdocs\...\lib\CallBack.php on line 175

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\htdocs\...\lib\CallBack.php on line 175

Any ideas why this is happening? The model works just fine, save the revision function. The revision function never even gets run. Thanks!


Ryan Gantt Sun Jan 23 18:22:05 -0500 2011

Also to note is that wrapping the name of the callback in array() doesn't help either, e.g.

static $after_save = array( array( 'revision' ) );

this removes the error messages, but doesn't call the appropriate method.

Ryan Gantt Wed Jan 26 21:34:47 -0500 2011

Are callbacks working just fine for everyone else???

Jacques Fuentes Sat Feb 05 16:58:11 -0500 2011

This works just fine for me when I test with your schema and models. Are you using 1.0 stable version? You may need to try upgrading to nightly. A brief look at the current code shows to me that doing a multi-d array for declaring a callback wouldn't work at all.

This will definitely error on latest: static $after_save = array(array('string'))

This works: static $after_save = array('string')

(1-3/3)