how to insert data record using insert() method?

hi all,


						  'ID' => $id,

						  'NAME' => $this->NAME



i try to insert data with insert() method but not work, got this error?



The active record cannot be inserted to database because it is not new.

note : my current model is t_post. and i not crud t_contact table, only create model in commnd line(DOS).


is there any other solution ?

1 search entire forum to see snipet about insert() method, but it seems no one use this method.

is’t me in the wrong way :)

thank before.

insert() is a low level method that’s used internally by AR, e.g. when calling save() on a new record. With insert you can define, which of these attributes should be added to your INSERT statement. You can use it, but it still requires you to create a new record object first and set its attributes.

So what you should do is:

$contact = new t_contact;




Or use DAO instead, if you have to insert a lot of records.

thank you mike.

acctually i have to insert a lot of records. :)

could you please give me example using DAO or link related this DAO snipet? ::)

$contact = new t_contact;




that’s code works fine.

thanks again.

See here:

Where $connection = Yii::app()->db.

hi mike… m a newbie… try to help me!! i wanna see a live example of using DAO.didnt understand how to use it. i have gone through the documentation

it’s really complicated…

$sql = "insert into table (some_field) values (:some_value)";

$parameters = array(":some_value"=>$some_value);


I think the best way is to use multiple insert command, by seperating records using commo, I would something like this

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

for more information take a look at this:

Try this:

$contact = new t_contact;


for($nxt_id=1;$nxt_id<10;$nxt_id++) {



	$contact->ID = $nxt_id;

	$contact->NAME = $something . " : " . $nxt_id;



I would use a command.

$command = Yii::app()->db->createCommand();

foreach ($records as $rec) {







             // ...




Not sure what the performance would be like, probably somewhere between the "$contact=new contact;" and the bulk insert approaches.

hi guys…i m a newbie in yii framework…any one help me to insert values into database by yii framework…Thanks in advance…


$dbhost = ‘localhost:3036’;

$dbuser = ‘root’;

$dbpass = ‘rootpassword’;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )


die('Could not connect: ’ . mysql_error());


$sql = 'INSERT INTO employee '.

   '(emp_name,emp_address, emp_salary, join_date) '.

   'VALUES ( &quot;guest&quot;, &quot;XYZ&quot;, 2000, NOW() )';


$retval = mysql_query( $sql, $conn );

if(! $retval )


die('Could not enter data: ’ . mysql_error());


echo "Entered data successfully\n";



hai will someone tell me how to enter the data to table when the values are stored in variable not constant

in case you want insert new row.


       $logobj = new Log();

        $logobj->userid = $userid;

        $logobj->date = new CDbExpression('NOW()');


in case you want to update the existing row load that row in model object ,assign values same way to insert and use


Works Fine !

$builder = $model->getCommandBuilder();

$table = $model->getMetaData()->tableSchema;

$builder->createInsertCommand($table, $model->getAttributes())->execute();

It does not help you? For me helped.

Caring for also what “happens” after saving, for example, the “audit module” logging changes, as only logs after finishing the application, and all that is in memory. I’m talking about bulk inserts.

Should also pay attention to something that needs to change before saving, should be done manually before calling the insert.