Page 1 of 1

IsNew

Posted: Mon Apr 23, 2007 3:52 pm
by jorge
Hi:
I'm custom devolping an application and i had an issue, when calling the method isNew() it always returns false even though is inserting a new database record.

Can you help on this one?
Best regards and thanks in advance !

Posted: Mon Apr 23, 2007 9:21 pm
by mikeb
IsNew() returns TRUE only if the object does not have an ID (
['data']['id'] ) set.


I can provide more information if you can offer code snippets.

Posted: Tue Apr 24, 2007 12:02 am
by jorge
Thanks for your answer.

I tried to achive was someting like this:

if ( $jf->isValid() ) {
$jf->Save(FALSE);

if ( isset($data['user_ids']) ) {
$jf->setUser( $data['user_ids'] );
}

if ( $jf->isValid() ) {
$jf->Save();
#Redirect::Page( URLBuilder::getURL( array('id' => $data['id'], 'data_saved' => TRUE), 'EditCase.php') );
break;
}
}

in the above case jf->setUser calls the parent class isNew to insert or update multiple rows in a database mapped to one id ($this->getId). Like in RecurringScheduleControl if I can remember. $this->IsNew() always return false.


Best regards
Thanks in advance.

Posted: Tue Apr 24, 2007 8:25 am
by mikeb
The reason why isNew() is always returning FALSE is because you call it after Save(). As soon as Save() is called the object is stored in the database and assigned an ID at that time. Therefore the object is no longer "new". Thats basically how we define "new" in TimeTrex, the object has not been saved to the database yet.

If you look at setUser() in RecurringScheduleControl, you will see that this is actually the proper procedure. You can't assign users to a object that isn't saved in the database and subsequently has an ID to be mapped to. It also doesn't use isNew() to determine insert/update, it uses isNew() to determine if it needs to delete already existing mapped entries that are not to be assigned any more.

Thanks

Posted: Tue Apr 24, 2007 10:43 am
by jorge
Hi mikeb:
That's the way how I understood worked isNew(). However the explanation about isNew cheking just to delete old mapped entries clarifies more the whole scenerio. Thanks a lot I'll give a second though to my script.

Good work,
Best regards

Posted: Tue Apr 24, 2007 11:21 am
by mikeb
We are always interested to hear from community members that are developing custom solutions, or add-ons to TimeTrex, do you care to elaborate on what you are working on by chance?

Hi Mike

Posted: Wed Apr 25, 2007 11:29 pm
by jorge
I'm developing an application that will permit fiedl's workers to collect values from househods and save it to a database. It's very much wap oriented.

Best Regards,
Jorge