Yii Framework Forum: how to insert data record using insert() method? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

how to insert data record using insert() method? Rate Topic: -----

#1 User is offline   fastcrash 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 10-March 10
  • Location:South Jakarta

Posted 26 April 2010 - 02:36 AM

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

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 26 April 2010 - 02:57 AM

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

#3 User is offline   fastcrash 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 10-March 10
  • Location:South Jakarta

Posted 26 April 2010 - 03:14 AM

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

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 26 April 2010 - 03:24 AM

See here: http://www.yiiframew...ding-parameters

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

#5 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 25 April 2011 - 01:33 AM

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
0

#6 User is offline   el chief 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 24-November 10

Posted 25 April 2011 - 09:53 PM

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

http://learnyii.blogspot.com/

Quick snippets to get you up to speed with the Yii PHP framework
1

#7 User is offline   shahdoost 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 30-March 12

Posted 18 April 2012 - 03:23 AM

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.../en/insert.html
0

#8 User is offline   VINAY Kr. SHARMA 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 13-September 11
  • Location:Gurgaon

Posted 18 April 2012 - 05:20 AM

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

VINAY Kr. SHARMA
@vinaykrsharma
Facebook ID: vinaykrsharma
Blog

Co-operation comes from friendship, friendship comes from trust, and trust comes from kind-heartedness.
0

#9 User is offline   Chris Backhouse 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 04-August 11
  • Location:France

Posted 02 July 2012 - 08:37 AM

I would use a command.
$command = Yii::app()->db->createCommand();

foreach ($records as $rec) {

$command->reset();

$result=$command->insert('table_name',
array(
'id'=>$rec->id,
'column2'=>$rec->column2,
// ...
'created_dt'=>'now()'
));
}

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

#10 User is offline   txsuriya 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 14-October 12

Posted 14 October 2012 - 11:18 PM

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

Attached File(s)


0

#11 User is offline   parkerfrank 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 02-October 12

Posted 15 October 2012 - 02:02 AM

<?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 ( "guest", "XYZ", 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);
?>
0

#12 User is offline   shamshad 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-October 12

Posted 22 October 2012 - 12:28 AM

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

#13 User is offline   hemc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 09-July 12
  • Location:New Delhi

Posted 22 October 2012 - 04:15 AM

 shamshad, on 22 October 2012 - 12:28 AM, said:

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

Please don't add a reply to say thanks, There is already a +1 button to do same.
0

#14 User is offline   Mohammad Fareed 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 08-March 14
  • Location:Hyderabad

Posted 17 June 2014 - 06:22 AM

Works Fine !

 el chief, on 25 April 2011 - 09:53 PM, said:

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


-
With best Regards.
Mohammad Fareed.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users