0

I have apply manual query in cakephp and not get last generate id

My query is this

$areas = $this->Area->query("insert into  areas (Area_ID,Parent_Area_ID,Area) values ('','".$Parent_Area_ID."','".$Area_name."')");

How i get last id ?

Jaskaran Zap
  • 277
  • 1
  • 2
  • 9
  • 1
    I'm smelling SQL injection vulnerabilities... ps, please always mention your exact CakePHP version! – ndm Aug 04 '14 at 13:10
  • 1
    Why are you not using a model as wrapper for this? This would make it so much cleaner and easier to work with... – mark Aug 04 '14 at 14:06
  • 1
    It seems highly likely that `Area_ID` is the primary key - in which case you're using query for no benefit and at the cost of exposing yourself to SQL injection and losing Cake's default ORM functions - why aren't you calling save on a model? If you're using CakePHP, _use_ CakePHP. – AD7six Aug 04 '14 at 14:57

2 Answers2

0

Try this

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

If this doesn't work check out this answer from a similar question

Community
  • 1
  • 1
Pooshonk
  • 1,284
  • 2
  • 22
  • 49
0

Try this code:

$this->Area->getInsertID();

More detail here:
http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html#model-getinsertid

joe
  • 8,344
  • 9
  • 54
  • 80
Indrajeet Singh
  • 2,958
  • 25
  • 25