1
votes

i am new to cake and using version 2.

i have models

hgcylinder.php

class HgCylinder extends AppModel  {
    //put your code here
    var $name= "HgCylinder";

    var $belongsTo = array('HgKeyGase');
}

hgkeygase.php
class HgKeyGase extends AppModel  {
    //put your code here
    var $name= "HgKeyGase";


    public $belongsTo = array(
        'HgKeyColor' => array(
            'className' => 'HgKeyColor',
            'foreignKey' => 'hg_key_color_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
        );

}

Controller HgCylindersController.php

<?php

class HgCylindersController extends AppController
{
    var $name = "HgCylinders";
    // var $scaffold;
    function index()
    {
        $this->set('hey',$this->HgCylinder->find('all'));
    }


    public function edit($id = null) {
        $this->HgCylinder->id = $id;
        if ($this->request->is('post')) {
        var_dump($this->request->data);
        exit;
            if ($this->HgCylinder->save($this->request->data)) {
                $this->Session->setFlash(__('Cylinder has been updated successfull'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The cylinder could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->HgCylinder->read(null, $id);
        }
        $hg_key_gase_id = $this->HgCylinder->HgKeyGase->find('list');

        $this->set(compact('hg_key_gase_id'));
    }

} 

?>

View : edit.ctp
<?php 

echo $this->form->create('HgCylinder',array('action'=>'edit'));
echo $this->form->input('hg_key_gase_id',);
echo $this->form->input("capacityM3");
echo $this->form->input("weightEmpty");
echo $this->form->input("weightFilled");
echo $this->form->input("isFilled");
echo $this->form->end('Add');
?>

my problem is hg_key_gase_id is become select list with no options. if i changed the name to "hgKeyGas" in view and controller it shows the options from the hg_key_gases table. but on saving does not saving the value of hg_key_gase_id field in hg_cylinders table instead it stores null in this field.

second i want to know that it is necessary to have variable name passing to view from controller exactly same for as field in table.

1

1 Answers

0
votes

try to stick to conventions.

so its

$hgKeyGases = $this->HgCylinder->HgKeyGase->find('list');

$this->set(compact('hgKeyGases'));

the pluralized form will be able to populate your select box (as documented in the cook book)

also use $this->Form->input() (note the capital F). $name is not necessary. dont use read(), use find(first) instead. dont set the action (the form will post to itself by default).

and most importantly. ALWAYS respect the casing of files in your filesystem. especially if you plan on deploying on NIX systems (which are case sensitive). so it would be HgCylinder.php and HgKeyGase.php as Model class files.

last tip: use baking (cake bake via shell console) to bake your crud files. this way you learn how its done the right way. it would have also answered your question itself by the way.

the documentation can be found here: http://book.cakephp.org/2.0/en/index.html maybe you found an old outdated version, the 2.x one is the one you should have used.