This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.2 » mysql_insert_id replacement in symfony
mysql_insert_id replacement in symfony [message #102008] Mon, 05 July 2010 13:23 Go to next message
xare  is currently offline xare
Messages: 14
Registered: June 2010
Junior Member
Hello I am developing a site in symfony 1.2.9 and after have read all the ways to obtain the mysql_insert_id in this forum and on mailing lists I fail to obtain a correct answer.

I am faced by the following problem. For some reason the previous programmer of my project programmed two consecutive inserts of the same data in two different tables, my goal is to optimise the database and the model programming by just putting an index of the first table at the second one.

So far the model part for the insert has been programmed by hand in order to bear in mind some particularities.

What I want is to
1st. Perform one insert into one table
2nd. To use the last_id generated by the INSERT query in order use it into the insert of the table.

I have browsed all the documentation of symfony and the forums where I have found a couple of references but they do not seem to respond quite well to my purpose.

I basically need to obtain this

$needed_id=mysql_insert_id([ resource $link_identifier ] );

I would like to know if there is a genuine symfony way to obtain this and if not which is the way to obtain $link_identifier.

Thanks


Re: mysql_insert_id replacement in symfony [message #102023 is a reply to message #102008 ] Mon, 05 July 2010 18:41 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
Are you using Propel or Doctrine?


Remember Palestine
Re: mysql_insert_id replacement in symfony [message #102024 is a reply to message #102023 ] Mon, 05 July 2010 18:45 Go to previous messageGo to next message
xare  is currently offline xare
Messages: 14
Registered: June 2010
Junior Member
I am using propel



Re: mysql_insert_id replacement in symfony [message #102025 is a reply to message #102008 ] Mon, 05 July 2010 18:50 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
This is how I do it. I am not sure if there is a neater way, but this seems OK to me:

$object1->setXxx( ... );
$object1->save();

$object2->setObjectId($object1->getPrimaryKey());
$object2->setXxx( ... );
$object2->save();

The "setObjectId" method depends on the FK column name. If you have relationships defined, setObject() will be available too, and you can just set the related object instead.


Remember Palestine
Re: mysql_insert_id replacement in symfony [message #102047 is a reply to message #102025 ] Tue, 06 July 2010 09:00 Go to previous messageGo to next message
xare  is currently offline xare
Messages: 14
Registered: June 2010
Junior Member
Thanks for your answer.
As I have a code already developped by a previous programmer I do not see clear how to insert those lines in my code.

Here is a simplified snippet of what I haveƑ
$con = Propel::getConnection(ModulePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);

$con->beginTransaction();

try {
$sql1="INSERT INTO ....";
$st1 = $con->prepare($sql1);

$sql2 ="INSERT INTO table2(foreign_id) VALUES (:foreign_id)";
$st2 = $con->prepare($sql2);
$st2->bindParam(':foreign_id',XXXXXXXX,PDO::PARAM_INT);

$st1->execute();
$st2->execute();

$con->commit();
}


So my questions are as follow here?

I want XXXXXX to be something like mysql_insert_id([resource $link_identifier])

Normally if no resource $link_identifier is provided I guess that what we use is the id from the first insert sql query run but. When is it really run, what corresponds to mysql_query. $st1->execute(); or $con->commit? What happens with these commands? Do they return true or false or do they provide with a link identifier resource?

Thanks so much.

Re: mysql_insert_id replacement in symfony [message #102054 is a reply to message #102008 ] Tue, 06 July 2010 10:38 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
Oh dear, shoot the previous coder Wink

try
{
  $object1 = new Table1();
  $object1->setXxx( ... );
  $object1->save($con);

  $object2 = new Table2();
  $object2->setObjectId($object1->getPrimaryKey());
  $object2->setXxx( ... );
  $object2->save($con);

  $con->commit();
}

Make sure you use Eclipse or NetBeans, and all the methods relating to your columns will be made clear to you via auto-complete.

Forget about MySQL-specific discussions (mysql_insert_id) - after you do save the value will appear in the column, as per my example already provided.

[Updated on: Tue, 06 July 2010 10:38]


Remember Palestine
Re: mysql_insert_id replacement in symfony [message #102080 is a reply to message #102054 ] Tue, 06 July 2010 16:31 Go to previous messageGo to next message
xare  is currently offline xare
Messages: 14
Registered: June 2010
Junior Member
Solved my problem. My inserts perform now well they way I like.

Thanks so, so, much.

Re: mysql_insert_id replacement in symfony [message #102082 is a reply to message #102008 ] Tue, 06 July 2010 17:11 Go to previous message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
No probs. Have fun with Propel - see how it is much easier than worrying about inserts and updates? Smile


Remember Palestine
Previous Topic:Calling a module 'module' ?
Next Topic:what does automatic_cleaning_factor do?
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software