Welcome, Guest
Username: Password: Remember me

TOPIC: How to save multiple Items at once?

How to save multiple Items at once? 14 Nov 2012 14:41 #5352

  • goslingcools
  • goslingcools's Avatar
  • Offline
  • Senior Member
  • Posts: 64
  • Thank you received: 7
  • Karma: 1
Hi,

Simply said: How to save multiple Items at once?

Here is my question:

I'm looking for a way to do this with Cook.

Any suggestions?

Regards,

GC
The administrator has disabled public write access.

Re: How to save multiple Items at once? 14 Nov 2012 14:53 #5353

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @goslingcools,

Simply put, this is not a simple task. How familiar are you with ajax?

The best way would be to have 2 forms: 1 - CV, 2 - Training (Single items);

Using ajax, load the training form into the CV form with multiple instances (as many as you want with an 'add more training' button maybe. Adapt the save routine to capture the training instances of the form in an array and loop through them passing them to the save function of training with the CV id (whatever the name of the foreign key you've implemented is) upon successful save of the CV.

Naturally, the prepareQuery() will need to get the related training info too when launching an edit/update on a CV.

Check out this tutorial that @admin provided about implementing AJAX in joomla/cook: www.j-cook.pro/forum/7-design-your-appli...sing-the-modelactrlr

hope it helps,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: How to save multiple Items at once? 14 Nov 2012 15:06 #5354

  • goslingcools
  • goslingcools's Avatar
  • Offline
  • Senior Member
  • Posts: 64
  • Thank you received: 7
  • Karma: 1
I was thinking of using AJAX too :-)

But I would need 3 tables right?

1) CV's
2) Trainings
3) Table that links multiple Training ID's to a single CV ID

Right?
The administrator has disabled public write access.

Re: How to save multiple Items at once? 14 Nov 2012 15:08 #5355

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Not necessarily,

Just a cv_id in the trainings ;)

G
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: How to save multiple Items at once? 14 Nov 2012 15:09 #5356

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Of course, you'll have this id available from the save routine of the CV item, ready to pass as FKey on success, right?
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: How to save multiple Items at once? 14 Nov 2012 15:12 #5357

  • goslingcools
  • goslingcools's Avatar
  • Offline
  • Senior Member
  • Posts: 64
  • Thank you received: 7
  • Karma: 1
Ok, that's much better. And less work, thanks!

I was thinking way to complex. :-)
The administrator has disabled public write access.

Re: How to save multiple Items at once? 14 Nov 2012 15:14 #5358

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
It really depends how complex the entity is - i.e. how many fields it has but, it seems trainings are small objects. It's only worth breaking them out into more tables if a lot of the values in fields will be re-used over and over to reduce redundancy/duplication.

Glad it helped!

G
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.
The following user(s) said Thank You: goslingcools

Re: How to save multiple Items at once? 14 Nov 2012 15:19 #5359

  • goslingcools
  • goslingcools's Avatar
  • Offline
  • Senior Member
  • Posts: 64
  • Thank you received: 7
  • Karma: 1
This CV thing was just an example. Did not know how to explain it otherwise... :-)

But yes, my subitems will be quite simple.

Thanks again!
The administrator has disabled public write access.

Re: How to save multiple Items at once? 14 Nov 2012 16:10 #5360

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
2 tables fine for that then...

Best of luck!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: How to save multiple Items at once? 21 Nov 2012 08:56 #5543

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
All you have to do is not necessary with AJAX. In facts, it is not the best.

Ajax is good when you want to add some entries in your 'training' list.
So, I think you can implement AJAX for that, but to save the whole at one time, it is not good.

Fistly, for Ajax you need first that your main item (CV) is already initialized (id value)
Then, when you submit the form, if the id is not yet initialized, you will need anyway to send your complete form (main + N:1)


TODO :
1. Send the complete form.
2. Decrypt all this in the save() method of your controller. The controller have to work with the 2 models.

Controller :
- Save or update the main item, then get the ID of this saved object.
$modelCv->getState('cv.id');
- If save() was successfull, you can now add the individual 'training' entries, using a second model for that.

Very important : In the model in wich you will write a little bit, you must check carefully if you are dealing with new or existing items.
Read them all associated to this CV, then merge with the current posted form training entries.
(add, update, delete) You must do all this manually.

Not sure to be clear.
Can you understand the concept in main lines ?
Coding is now a piece of cake
The administrator has disabled public write access.

Re: How to save multiple Items at once? 21 Nov 2012 09:02 #5545

  • goslingcools
  • goslingcools's Avatar
  • Offline
  • Senior Member
  • Posts: 64
  • Thank you received: 7
  • Karma: 1
Thanks for your answer!

I solved it with some custom code. No ajax used.
But I think it's much to customized now to paste it here. :-) For example I did not use the model for saving etc of the trainings etc.
The administrator has disabled public write access.
Time to create page: 0.079 seconds

Get Started