IsNew

Discussion for TimeTrex open source community developers.
Locked
jorge
Posts: 23
Joined: Thu Feb 22, 2007 2:56 am

IsNew

Post 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 !
Our man from Havana
mikeb
Posts: 709
Joined: Thu Jul 27, 2006 11:58 am

Post 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.
TimeTrex Community Edition is developed and supported by volunteers.
Help motivate us to continue by showing your appreciation!
jorge
Posts: 23
Joined: Thu Feb 22, 2007 2:56 am

Post 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.
Our man from Havana
mikeb
Posts: 709
Joined: Thu Jul 27, 2006 11:58 am

Post 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.
TimeTrex Community Edition is developed and supported by volunteers.
Help motivate us to continue by showing your appreciation!
jorge
Posts: 23
Joined: Thu Feb 22, 2007 2:56 am

Thanks

Post 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
Our man from Havana
mikeb
Posts: 709
Joined: Thu Jul 27, 2006 11:58 am

Post 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?
TimeTrex Community Edition is developed and supported by volunteers.
Help motivate us to continue by showing your appreciation!
jorge
Posts: 23
Joined: Thu Feb 22, 2007 2:56 am

Hi Mike

Post 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
Our man from Havana
Locked