Ahhh... I think I now understand what you're saying. Initially I
thought you meant creating tables in the actual database.... and I'm
sure everyone else thought the same.

But re-reading your question I now think you mean *HTML* tables. Is
that correct?

OK... I would achieve the effect you want using ajax...

Here is the code you would put in your view for model A. You could
probably do the same with cakephp helpers but this is easy enough to
understand.


/views/modela/add.ctp

<script type="text/javascript" src="/js/prototype.js"></script>
<script type="text/javascript">
function addB(){
        new Ajax.Updater('mytable', '/ModelB/blank_row/',
{insertion:Insertion.Bottom});
}
</script>


<?php echo $form->create('ModelA') ?>

        <table>
                <tbody id="mytable">

                </tbody>
        </table>

        <input type="button" value="Add B" onclick="addB()" />

<?php echo $form->end() ?>





<?php

class ModelBController extends AppController {

        var $name = 'ModelB';
        var $components = array('RequestHandler');
        var $helpers = array('Form');

        function blank_row(){
                // might need to retrieve some data for select lists and things 
like
that here?
                // or set some default values if necessary
        }

}

?>



And then in your view you would output the new row like so....

/views/modelb/blank_row.ctp

<tr>
        <td><?php echo $form->create("ModelB.field1") ?></td>
        <td><?php echo $form->create("ModelB.field2") ?></td>
        <td><?php echo $form->create("ModelB.field3") ?></td>
</tr>


And that code should fit together nicely....

HOWEVER.... the only thing left to consider is making sure the field
names and ids are not duplicated and are in a format cakephp can
handle when saving.

(note: cakephp doesn't automatically save hasMany associations... so
you'll need the saveHasMany() function included at the bottom of this
email.)


The saveHasMany() function expects form input field names to be in a
format like this:

<input type="text" name="data[ModelB][0][field1]" value="value1" />
<input type="text" name="data[ModelB][0][field2]" value="value2" />
<input type="text" name="data[ModelB][0][field3]" value="value3" />

<input type="text" name="data[ModelB][1][field1]" value="value1" />
<input type="text" name="data[ModelB][1][field2]" value="value2" />
<input type="text" name="data[ModelB][1][field3]" value="value3" />


Unfortunately in the blank_row.ctp above the form helper will create
fields like this.. which will obviously not work...

<input type="text" id="ModelBField1" name="data[ModelB][field1]"
value="value1" />

The way to solve this is to pass an extra argument when requesting the
blank row, using javascript to keep track of the counter_var.. eg

<script type="text/javascript">
var counter_var = 0;
function addB(){
        new Ajax.Updater('mytable', '/modelb/blank_row/'+(counter_var++),
{insertion:Insertion.Bottom});
}
</script>

Then modify your controller to pass this through to the view to use.

function blank_row($index){
        $this->set('index', intval($index));
}

(another note: currently you can't overwrite the fieldname when using
the form helper, so my suggestion is to overwrite the $form->Html-
>tags['***'] array)

<?php
// note that I am adding the $index to each fieldname

$form->Html->tags['input'] = '<input name="data[%s]['.$index.'][%s]"
%s/>';
$form->Html->tags['hidden'] = '<input type="hidden" name="data[%s]['.
$index.'][%s]" %s/>';

// ... you get the idea... you can find the rest defined inside /cake/
libs/view/helpers/html.php

?>
<tr>
        <td><?php echo $form->input('field1', array('id' => 'ModelB'.
$index.'field1')); ?></td>
        <td><?php echo $form->input('field2', array('id' => 'ModelB'.
$index.'field2')); ?></td>
        <td><?php echo $form->input('field3', array('id' => 'ModelB'.
$index.'field3')); ?></td>
</tr>






And finally here is the saveHasMany() method that goes into your
app_model.php file

class AppModel extends Model {

        function saveHasMany($k) {
                $success = true;

                $v = $this->hasMany[$k];

                $myid = $this->data[$this->name][$this->primaryKey] ? $this-
>data[$this->name][$this->primaryKey] : $this->id;
                $deletequery = "DELETE FROM {$this->$k->useTable} WHERE
{$v['foreignKey']} = {$myid}";
                $db =& ConnectionManager::getDataSource($this->$k->useDbConfig);
                $db->query($deletequery);

                if (!isset($this->data[$k]) || empty($this->data[$k])) {
                        return $success;
                }

                // Save new data
                foreach ($this->data[$k] as $tc) {
                        $tc[$v['foreignKey']] = $myid;
                        $data[$v['className']] = $tc;
                        $this->{$v['className']}->create($data);
                        if (!$this->{$v['className']}->save()) {
                                $success = false;
                        }
                }

                return $success;
        }

}


and use it in your model like:

class ModelA extends AppModel {

        ... blah associations, etc ...

        function afterSave(){
                $this->saveHasMany('ModelB');
        }

}


On Dec 4, 9:03 pm, dandreta <[EMAIL PROTECTED]> wrote:
> Nobody knows any link or example how I can do it?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to