In your District model, add this line:
class District extends AppModel {
var $displayField = 'district';
// ...
}
Now use find('list') and it will work fine.
To avoid unnecessary code, you can just do this:
[controller]
$this->set('districts', $this->District->find('list'));
[view]
echo $form->input('district_id');
>From this the `districts` variable will be used automatically to get
populate the dropdown. It's all in the naming!
hth
grigri
On Jan 12, 12:10 pm, forrestgump <[email protected]> wrote:
> Hello,
> Been a while since i posted here....i ran into a problem and needed
> some help...I have two tables as follows:
>
> table1:
> name:districts
> fields:id,district
>
> table2:
> name:mandals
> fields:id,mandal,district_id
>
> as you might have guessed i have associated the two tables with the
> help of foreign key "district_id". I manually entered the values and
> with the help of the values into the tables as:
>
> table1:
> id:1
> district:Nizamabad
>
> table2:
> id:1
> mandal:Bhongir
> district_id:1
>
> with the help of the belongsTo relationship as defined below
>
> var $belongsTo = array('District' =>
> array('className' => 'District',
> 'conditions' => '',
> 'order' => '',
> 'foreignKey' => 'district_id'
> )
> );
> i was able to properly display the name of the district in a simple
> table in a view by using the code below
> <table>
> <tr>
> <th>Sno.</th>
> <th>District</th>
> <th>Mandal</th>
> <th>Action</th>
> </tr>
>
> <!-- Here is where we loop through our $mandals array, printing out
> mandal info -->
>
> <?php $i=1; ?>
> <?php foreach ($mandals as $mandal): ?>
> <tr>
> <td><?php echo $i++; ?></td>
> <td><?php echo $mandal['District']['district']; ?></td>
> <td><?php echo $mandal['Mandal']['mandal']; ?></td>
> <td><?php echo $html->link('Edit', "/mandals/edit/".$mandal['Mandal']
> ['id']); ?></td>
> </tr>
> <?php endforeach; ?>
> </table>
>
> I wish to now create a form which contains the following fields and
> save the data into table2(mandals):
> Mandal Name:
> Districts:
>
> I want the Districts field to be a dropdown list which is populated
> with the name of the districts, but i want the corresponding id of the
> district to be stored in table2 district_id field when the save button
> is clicked.
>
> Will appreciate any help that can be offered ive been trying to figure
> something out since 3 days...i tried using:
> $this->set('districtslist', $this->District->find('list'));
> along with echo $form->select('district_id',$districtslist,null,array
> (),'select one');
>
> but i end up populating the select list with the id's of the
> districts. by using
> $this->set('districtslist', $this->District->find('all'));
> the select list gets populated with the data of all the fields from
> both the tables.
>
> Please Help!!!!!!
>
> Thanks in advance,
> forrestgump
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" 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
-~----------~----~----~----~------~----~------~--~---