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