Zapisywanie wielu rekordów w CakePHP
September 18th, 2007 by prondByłem ostatnio zmuszony do zbudowania w CakePHP aplikacji,
która miałaby pozwalać na masową edycję stron, produktów, etc.
Szperając na blogach i forach nie udało mi się jednak znaleźć rozwiązania,
które pozwalałoby jednocześnie korzystać ze wszystkich udogodnień CakePHP.
Po paru eksperymentach udało mi się zneleźć dość satysfakcjonujące rozwiązanie.
Przynajmniej nadal mogę korzystać z takich ‘automagicznych’ elementów jak
walidacja i helpera do formularzy.
Kontroler : controllers/pages_controller.php
function addMany($count = 2) { for ($i=1;$i<=$count;$i++) { $modelName = "Page{$i}"; $this->{$modelName} = & new Page; $this->{$modelName}->useTable = "page"; $this->{$modelName}->name = $modelName; ClassRegistry::addObject("Page{$i}",$this->{$modelName}); } if (!empty($this->data)) { for ($i=1;$i<=$count;$i++) { $modelName = "Page{$i}"; $this->{$modelName}->create($this->data); $this->{$modelName}->save(); } } $languages = $this->Page->Language->generateList(); $this->set('count',$count); $this->set(compact('languages')); }
Widok : views/pages/add_many.php
<div class="page">
<form action="" method="post">
<?php for ($i=1;$i<=$count;$i++) : ?>
<fieldset>
<legend><?php __('Add');?> <?php __('Page');?></legend>
<?php
echo $form->input("Page{$i}.language_id");
echo $form->input("Page{$i}.name");
echo $form->input("Page{$i}.urn");
echo $form->input("Page{$i}.description");
echo $form->input("Page{$i}.keywords");
echo $form->input("Page{$i}.published_from");
echo $form->input("Page{$i}.published_to");
echo $form->input("Page{$i}.published");
echo $form->input("Page{$i}.contents");
?>
</fieldset>
<?php endfor; ?>
<?php echo $form->end('Submit');?>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('List', true).' '.__('Pages', true), array('action'=>'index'));?></li>
<li><?php echo $html->link(__('List', true).' '.__('Languages', true), array('controller'=> 'languages', 'action'=>'index')); ?> </li>
<li><?php echo $html->link(__('New', true).' '.__('Language', true), array('controller'=> 'languages', 'action'=>'add')); ?> </li>
</ul>
</div>Posted in CakePHP |
July 30th, 2008 at 5:28 pm
Hi,
Really like how neat your solution is - don’t fully understand it though.
Are objects added to the registry available in your views automagically?
How would you implement an edit() version of what you are doing?
Thanks for the neat approach though.
Frank.
November 1st, 2008 at 12:47 pm
Thanks for the post,
November 3rd, 2008 at 1:39 am
Thanks a lot for this post
November 5th, 2008 at 6:52 pm
Hmm. Good.
November 11th, 2008 at 12:28 pm
Thanks for the post, I have been having the same problems.
November 12th, 2008 at 3:44 am
Thanks for the post,
November 13th, 2008 at 2:02 am
Hmm. Good.
November 15th, 2008 at 2:29 pm
Thanks for the great tips.
November 17th, 2008 at 1:24 pm
Good post.
November 18th, 2008 at 1:25 am
Thanks a lot for this post
November 18th, 2008 at 7:51 am
Hmm. Good.
November 18th, 2008 at 5:41 pm
Nice blog btw
November 23rd, 2008 at 9:10 pm
Hmmm, I am tempted to try this.
November 24th, 2008 at 10:21 pm
Thanks for this - great idea.
November 26th, 2008 at 2:43 am
I came across. Thank you.