Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

fActiveRecord Insert ID

posted by anonymous 8 years ago

Hi there In the class Node extends fActiveRecord I want to get the last insert id. How can I do that? Thanks!

	function save($node_id=null){
                    
			//insert the new node
			$result = fRecordSet::buildFromSQL(
					 __CLASS__,
					"INSERT INTO nodes
							SET parent  =  $node_id"
			);
			$result->insert_id;
		}
		return $result->insert_id;

Well, fRecordSet only allows for selecting records so I doubt you will be able to use an INSERT statement and have it work.

If you use fActiveRecord to create a new record and save it, the auto-incrementing ID will automatically appear in the object.

$node = new Node();
$node->setParent($parent_id);
$node->store();
$node_id = $node->getId();

The ->getId() method is an example assuming your column is named id. fActiveRecord does not care what the name of the column is, as long as the column is an auto-incrementing primary key, the generated value will appear in the record after a successful ->store() operation.

posted by wbond 8 years ago