1

I have the following table that handles reserverations:

CREATE TABLE `Plots` (
  `Plot_ID` int(11) NOT NULL AUTO_INCREMENT,
  `Grid_ID` int(11) NOT NULL DEFAULT 0,
  `Instance_ID` int(11) NOT NULL DEFAULT 0,
  `Session_ID` char(32) DEFAULT NULL,
  `User_ID` int(11) DEFAULT NULL,
  `Transaction_ID` int(11) DEFAULT NULL,
  `CreateDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `UpdateDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `Plot` varchar(10) NOT NULL DEFAULT 0,
  `ReserveDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `ReservationTimeoutDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `PurchaseDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`Plot_ID`),
  CONSTRAINT `Plots_ibfk_1` FOREIGN KEY (`Grid_ID`) REFERENCES `Grids` (`Grid_ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `Plots_ibfk_2` FOREIGN KEY (`Instance_ID`) REFERENCES `Instances` (`Instance_ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `Plots_ibfk_3` FOREIGN KEY (`Session_ID`) REFERENCES `Sessions` (`Session_ID`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `Plots_ibfk_4` FOREIGN KEY (`User_ID`) REFERENCES `Users` (`User_ID`) ON DELETE NO ACTION ON UPDATE CASCADE,
  CONSTRAINT `Plots_ibfk_5` FOREIGN KEY (`Transaction_ID`) REFERENCES `Transactions` (`Transaction_ID`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;

As you can see, "Session_ID" is a foreign key to my Sessions table, which is being managed by Zend_Session and Zend_Session_SaveHandler_DbTable.

I'm having problems when I create the session, and within the same script, try to use it's Session_ID to insert a record into "Plots" (shown above), because the record does not exist in the Sessions table.

Can I force Zend_Session_SaveHandler_DbTable to save it's data into the database earlier? If so, how?

Travis
  • 599
  • 2
  • 6
  • 16

1 Answers1

1

See PHP's session_write_close() / session_commit(). This will write the session data and end session.

Alternatively why does working on Plots fail? FK constraints or something? You know the session_id, its just not in the database yet. But you know what it will be when inserted (assuming session shutdown as expected). Not ideal I suppose but workable.

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • 1
    In ZF, you can call `Zend_Session::writeClose()` which will ultimately call `session_write_close()`. If the op was brave, he could look into manually calling `Zend_Session_SaveHandler_DbTable::write($id, $data)` but I think the serialization of the `$data` could have some unforeseen implications, so it may be best to use standard `session_write_close`. You can always restart the session right after calling close if you still need to write data to it. – drew010 Mar 30 '12 at 17:58
  • drew, your comment above is the answer I was looking for, but I can't accept it as the answer. If you want the credit, move your comment into an answer and I'll mark it as accepted, otherwise, I'll just accept ficuscr's answer. – Travis Apr 02 '12 at 18:00