I searched high and low but couldn't really find a proper answer for 
doing this so with a bit of fiddling I managed to get something going.

What I wanted was a way to have the equivalent of 
$form->input('Modelb/Modelb',.....) but also with the extra column data 
in a table.

I didn't get much clue from what to do in the view from the info I could 
find (I did notice Nate mentioning the 'with' option for habtm in the model)

So after some fiddling I ended up setting an extra $modelb variable 
holding all the records from modelb and looping through that to show my 
'options':
In the controller:
        $modelb = $this->Modela->Modelb->findAll();
And in the view: (yes I know it's messy but it's late and I'm tired, 
suggestions for better way are more than welcome)
          <?php foreach($modules as $module):?>
          <?php
                // get to the right Modelb and get the data for the 
fields (need some better checking for nonexistant keys later)
                foreach($this->data['Modelb'] as $cModelb) {
                    if ($cModelb['id']==$modelb['Modelb']['id']) {
                        // We have the right module
                        $ModelaModelb=$cModule['Modelamodelb];
                    }
                }
          ?>
          <tr>
            <td><b><?=$modelb['Modelb']['title']?></b></td>
            <td><input 
name="data[Modelb][Modelb][<?=$modelb['Modelb']['id']?>][extrafielda]" 
type="checkbox"
                <?php if ($ModelaModelb['extrafielda']) echo 
'CHECKED="CHECKED"'; ?>
                value="1" />&nbsp;</td>
            <td><input 
name="data[Modelb][Modelb][<?=$modelb['Modelb']['id']?>][extrafieldb]" 
type="checkbox"
                <?php if ($ModelaModelb['extrafieldb']) echo 
'CHECKED="CHECKED"'; ?>
                value="1" />&nbsp;</td>
            <td><input 
name="data[Modelb][Modelb][<?=$modelb['Modelb']['id']?>][extrafieldc]" 
type="textbox"
                value="<?php echo $ModelaModelb['extrafieldc'];?>" 
/>&nbsp;</td>
          </tr>
    <?php endforeach; ?>

So far so good except that on saving I was getting errors from trying to 
convert arrays to text, and then insert errors (came from model.php : 
__saveMulti).
So I copied the function __saveMulti($joined, $id) into my model and 
modified the bit inside if (!empty($update)) {...}  it so it now looks 
like this:

                    if (!empty($update)) {
                        $values[]  = $db->value($id, 
$this->getColumnType($this->primaryKey));
                        if (is_array($update)) {
                            $values[]  = $db->value($key);
                            foreach ($update as $fld => $val) {
                                $keys[]='`'.$fld.'`';
                                $values[]=$db->value($val);
                            }
                            $aFields[$assoc][] = $fields[$assoc]. 
','.join(',', $keys);
                            unset($keys);
                        } else {
                            $values[]  = $db->value($update);
                            $aFields[$assoc][]=$fields[$assoc];
                        }
                        $values    = join(',', $values);
                        $newValues[] = "({$values})";
                        unset ($values);
                    }
And then  the $db->query for inserting is like so:
                 $db->query("INSERT INTO {$table} 
({$aFields[$loopAssoc][$x]}) VALUES {$newValue[$loopAssoc][$x]}");

I haven't tested my __saveMulti anywhere else but I can't see why it 
wouldn't work as normal everywhere else.
Okay so having found a solution, two/three questions,
1. Is there a better way I can do the view bit.
2. Is there a better way instead of messing with __saveMulti,
2.5    if not could I ask that someone more knowledgable than me get 
this submitted into model.php sometime in the future.

-- 
*/Jeremy Pointer/*
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

begin:vcard
fn:Jeremy Pointer
n:Pointer;Jeremy
adr:;;Box 499;Krugersdorp;Gauteng;1740;South Africa
email;internet:[EMAIL PROTECTED]
tel;work:+27 11 665-2445
tel;fax:+27 86 5013109
tel;cell:+27 82 3724769
x-mozilla-html:TRUE
version:2.1
end:vcard

Reply via email to