Hi:
I'm trying to get the last inserted id in schedule table from EditSchedule with my own method in ScheduleFactory:
----------------------------------------------------------
if ( $fail_transaction == FALSE ) {
//$sf->FailTransaction();
$sf->CommitTransaction();
var_dump($sf->getId());//returns false
//-----------> My reference to the class's method that gets the last inserted schedule id and insert the new rows into another table
$sf-> setScheduleJobProducts( $data['products_ids']);
Redirect::Page( URLBuilder::getURL( array('refresh' => TRUE ), '../CloseWindow.php') );
break;
} else {
$sf->FailTransaction();
}
-------------------------------------------------------
My omethod fails to retrieve $this->isnew() either $this->getId() within the same object (class)
function setScheduleJobProducts( $ids ){
if (is_array($ids) and count($ids) > 0) {
// -------------> Here !$this->isNew() returns false always as a consequence old rows never get deleted
if ( !$this->isNew() ) {
$sjpclf = new ScheduleJobProductsControlListFactory();
$sjpclf->getByScheduleControlId( $this->getId() );
$tmp_ids = array();
foreach ($sjpclf as $obj) {
$id = $obj->getProduct();
Debug::text('Recurring Schedule ID: '. $obj->getRecurringScheduleControl() .' ID: '. $id, __FILE__, __LINE__, __METHOD__, 10);
//Delete products that are not selected.
if ( !in_array($id, $ids) ) {
Debug::text('Deleting: '. $id, __FILE__, __LINE__, __METHOD__, 10);
$obj->Delete();
} else {
//Save ID's that need to be updated.
Debug::text('NOT Deleting : '. $id, __FILE__, __LINE__, __METHOD__, 10);
$tmp_ids[] = $id;
}
}
unset($id, $obj);
}
//Insert new mappings.
$plf = new ProductListFactory();
if( $this->getId() > 0 ){ //--------> $this->getId() returns false therefore the condition
foreach ($ids as $id) {
//-------------> if the object isNew variable $tmp_ids is not set yielding in warnigns, but it works nevertheless
if ( isset($ids) AND !in_array($id, $tmp_ids) ) {
$sjpcf = new ScheduleJobProductsControlFactory();
//This methods returns false as $this->getId returns false
$sjpcf->setScheduleControl( $this->getId() );
$sjpcf->setProduct( $id );
$obj = $plf->getById( $id )->getCurrent();
if ($this->Validator->isTrue( $this->user_validator_label,
$sjpcf->Validator->isValid(),
$this->user_validator_msg.' ('. $obj->getName() .')' )) {
$sjpcf->save();
}
}
}
return TRUE;
}
}
Debug::text('No Products IDs to set.', __FILE__, __LINE__, __METHOD__, 10);
return FALSE;
}
My question is, why $this->getId returns false, because of the transaction commit. I have tried in several places in my code to no avail. I could choose to bypass the Isnew condition but I still need the $this->getId() to insert the correct value corresponding to $this->getId() value in the other table.
Any help apreciated!
Best Regards and thanks for your support to the open sourcy community!
Help how to properly $this->IsNew() / $this->getId() ?
Help how to properly $this->IsNew() / $this->getId() ?
Our man from Havana
When creating a new object in TimeTrex, an ID is only assigned to it when Save() is called.
If the object is in fact new Save() will return the inserted ID of the object, then it will delete all the data it has in memory so any subsequent calls to $obj->getId() (or pretty much any other method) will return false, because all the data has been cleared from memory.
If the object is not new, Save() returns true if the data was successfully updated, or false if an error has occurred.
Here are some example usages:
So as you can see, if you pass FALSE to Save() that instructs it to not delete the data from memory so you can continue to access methods after the save has occurred.
If you take a look at:
interface/pay_stub_amendment/EditRecurringPayStubAmendment.php
in the submit case, you will see how Save(FALSE) is used to get the data into the database and have an ID assigned to it, so it can then go on to save the users assigned to the object immediately after.
The reason you don't want to use Save(FALSE) all the time, is because for instance in loops some data may get carried over between objects and cause a real mess. So as a safety measure TimeTrex defaults to deleting the in-memory data for an object when Save() is called.
If the object is in fact new Save() will return the inserted ID of the object, then it will delete all the data it has in memory so any subsequent calls to $obj->getId() (or pretty much any other method) will return false, because all the data has been cleared from memory.
If the object is not new, Save() returns true if the data was successfully updated, or false if an error has occurred.
Here are some example usages:
Code: Select all
if ($obj->isValid() ) {
$inserted_id = $obj->Save();
$obj->getId(); //returns false because all data from memory has been cleared for $obj. But we still have $inserted_id to work with.
}
Code: Select all
if ($obj->isValid() ) {
$inserted_id = $obj->Save(FALSE); //Don't delete data from memory.
$obj->getId(); //returns inserted_id.
//All other calls on $obj continue to work, as data is in still in memory.
}
If you take a look at:
interface/pay_stub_amendment/EditRecurringPayStubAmendment.php
in the submit case, you will see how Save(FALSE) is used to get the data into the database and have an ID assigned to it, so it can then go on to save the users assigned to the object immediately after.
The reason you don't want to use Save(FALSE) all the time, is because for instance in loops some data may get carried over between objects and cause a real mess. So as a safety measure TimeTrex defaults to deleting the in-memory data for an object when Save() is called.
TimeTrex Community Edition is developed and supported by volunteers.
Help motivate us to continue by showing your appreciation!
Help motivate us to continue by showing your appreciation!
Wonderful
Hi mikeb:
Wonderful explanation. I can see clearly now!
I couldn't understand before the relation between Save(FALSE) and the $obj->getId();
I followed your advice and manage to get the last inserted id
Thanks a lot for your support to the OSC!
Best Regards
Wonderful explanation. I can see clearly now!
I couldn't understand before the relation between Save(FALSE) and the $obj->getId();
I followed your advice and manage to get the last inserted id
Thanks a lot for your support to the OSC!
Best Regards
Our man from Havana