1

I use this code but its not working in cakephp and the code is:

$inserted = $this->get_live->query("INSERT INTO myaccounts (fname) values('test');

After this im using:

$lead_id = $this->get_live->query("SELECT LAST_INSERT_ID()");

It's working, but only one time.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
zakmail007
  • 165
  • 1
  • 1
  • 4
  • What exactly is it that you are trying to achieve? And is this code in your model/controller/elsewhere? – Joep Jan 25 '12 at 13:54
  • Even after your edit I still don't understand the question. Are you executing these calls from a loop? If so, please post the loop. And once more: in which file did you place this code? – Joep Jan 25 '12 at 13:58
  • 1
    Is there any reason why you're using the query() function, rather then Cake's save() function? The query function is rarely ever needed. – Oldskool Jan 25 '12 at 14:01
  • With the activemodel record, when you save, $this->Model->id becomes populated anyway. It **really** looks like you need to back to the Cake manual. – Dunhamzzz Jan 25 '12 at 14:21
  • this might be helpful for you http://stackoverflow.com/a/11020439/1239506 – Moyed Ansari Jun 13 '12 at 17:44

3 Answers3

6

Try this. Lots less typing. In your controller, saving data to your database is as simple as:

public function add() {
    $data = "test";
    $this->Myaccount->save($data);
    // $this->set sends controller variables to the view
    $this->set("last", $this->Myaccount->getLastInsertId());
}

You could loop through an array of data to save with foreach, returning the insertId after each, or you could use Cake's saveAll() method.

Myaccount is the Model object associated with your controller. Cake's naming convention requires a table called "myaccounts" to have a model class called "Myaccount" and a controller called "Myaccounts_Controller". The view files will live in /app/views/myaccounts/... and will be named after your controller methods. So, if you have a function add()... method in your controller, your view would be /app/Views/Myaccounts/add.ctp.

The save() method generates the INSERT statement. If the data you want to save is located in $this->data, you can skip passing an argument in; it will save $this->data by default. save() even automagically detects whether to generate an UPDATE or an INSERT statement based on the presence of an id in your data.

As a rule of thumb, if you're using raw sql queries at any point in Cake, you're probably doing it wrong. I've yet to run into a query so monstrously complex that Cake's ORM couldn't model it.

http://book.cakephp.org/2.0/en/models/saving-your-data.html

http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html?highlight=getlastinsertid

HTH :)

OpenSorceress
  • 2,014
  • 16
  • 21
  • Thanks for the reply but my problem as it is actually i have two DB tables both name are 'myaccounts' but one at on live server and second is on local server i want to copy yesterday's records from live server to local server.so i use given code in next comment. – zakmail007 Jan 27 '12 at 13:06
  • **`$yesterday=date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); $opt1['conditions']=array('date(created_on)'=>$yesterday); $opt1['fields']=array('fname','lname','address','remark','loacladdress','permaddress'); $liveData=$this->get_live->find('all',$opt1);//get_live is model name for($i=0;$i < count($liveData); $i++) { if($this->Myaccount->save($liveData[$i])) { $lid=$this->Myaccount->getLastInsertId(); }else{ echo "error
    "; } }`**
    – zakmail007 Jan 27 '12 at 13:09
  • its not inserting data in Local DB but it comes in if condition and local model name is 'Myaccount'. Both DB has been connected now. can you tell me what and where is actual problem. – zakmail007 Jan 27 '12 at 13:10
2

You can get last inserted record id by (works for cakePHP 1.3.x and cakePHP 2.x)

echo $this->ModelName->getLastInsertID();

Alternately, you can use:

echo $this->ModelName->getInsertID();

CakePHP 1.3.x found in cake/libs/model/model.php on line 2775

CakePHP 2.x found in lib/Cake/Model/Model.php on line 3167

Note: This function doesn't work if you run the insert query manually

Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104
0
pr($this->Model->save($data));
id => '1'

id is a last inserted value

snkhan120
  • 9
  • 3