how to insert data record using insert() method?

hi all,




t_contact::model()->insert(array('attributes'=>array(

						  'ID' => $id,

						  'NAME' => $this->NAME

						

						)));

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


CDbException

Description


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).

s,

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;

$contact->ID=$id;

$contact->NAME=$something;

$contact->save();

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;

$contact->ID=$id;

$contact->NAME=$something;

$contact->save();

that’s code works fine.

thanks again.

See here: http://www.yiiframework.com/doc/guide/database.dao#binding-parameters

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);


Yii::app()->db->createCommand($sql)->execute($parameters);

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:

http://dev.mysql.com/doc/refman/5.5/en/insert.html

Try this:




$contact = new t_contact;

$contact->unsetAttributes();


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

	

	$contact->setIsNewRecord(true);

	$contact->ID = $nxt_id;

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

	$contact->save();

}



I would use a command.

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

foreach ($records as $rec) {

  &#036;command-&gt;reset();


            


  &#036;result=&#036;command-&gt;insert('table_name', 


         array(


              'id'=&gt;&#036;rec-&gt;id,


              'column2'=&gt;&#036;rec-&gt;column2,


             // ...


              'created_dt'=&gt;'now()'


                    ));

}

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…

<?php

$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() )';

mysql_select_db(‘test_db’);

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

if(! $retval )

{

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

}

echo "Entered data successfully\n";

mysql_close($conn);

?>

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.




       $userid=15;

       $logobj = new Log();

        $logobj->userid = $userid;

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

        $logobj->insert()



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




$logobj->save();



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.

Thanks.