Insert a new record to database's table

hi there,

I have two questions about INSERT:

  1. Please how can i use DAO/AR to insert a new record to a table, then get the new record’s id?

  2. Can I include AR method in a transaction? i.e.




$transaction = $connection->beginTransaction();

try {

	$quiz->save(); // here is an AR method


	$quiz_id = <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' /> // here, i need some way to get the new $quiz's id in the database table

	

	//below is DAO method


	$sql="INSERT INTO {{quiz_question}} (quiz_id, question_id) VALUES(:quiz_id,:question_id)";

	$command=$connection->createCommand($sql);

	for($i=0;$i<3;$i++) {

		$command->bindParam(":quiz_id",$quiz_id,PDO::PARAM_INT);

		$command->bindParam(":question_id",$questionArr[$i]['question_id'],PDO::PARAM_INT);

		$command->execute();

	}

} catch (Exception $e) {        	

	$transaction->rollBack();

}



Thanks,

aslan

The ID of new record is available right after saving, so


$quiz->save();

$quiz_id = $quiz->id;

yep thanks, please what about the 2nd question? and how to get this new id if i use DAO method?

  1. Yes, you can.

2a) Try $connection->lastInsertID.

Thanks!!!