Google

Kategorie

Reprezentacja Wiedzy

Kalendarz

September 2007
M T W T F S S
« Aug   Dec »
 12
3456789
10111213141516
17181920212223
24252627282930

Zapisywanie wielu rekordów w CakePHP

September 18th, 2007 by prond

Był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 |

15 Responses

  1. Frank Says:

    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.

  2. breezee1205 Says:

    Thanks for the post,

  3. darkgriever Says:

    Thanks a lot for this post

  4. jthsmits Says:

    Hmm. Good.

  5. mstod00 Says:

    Thanks for the post, I have been having the same problems.

  6. wft3000 Says:

    Thanks for the post,

  7. lrech Says:

    Hmm. Good.

  8. tibi2008 Says:

    Thanks for the great tips.

  9. Vince4u2 Says:

    Good post.

  10. fadthemisss Says:

    Thanks a lot for this post

  11. heocung Says:

    Hmm. Good.

  12. deven Says:

    Nice blog btw

  13. sparky88 Says:

    Hmmm, I am tempted to try this.

  14. PSD Says:

    Thanks for this - great idea.

  15. mehmetaksu Says:

    I came across. Thank you.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.