Sunday, May 25, 2014

Re: adding a new record with foreign key option

Try looking at find->list )http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list). It’ll return a key => value array where key is the value you want to store (teacher_id or student_id) and value is the name of the teacher or student that will be displayed in the drop down. Check out the displayField model property (http://book.cakephp.org/2.0/en/models/model-attributes.html#displayfield).

On 26 May 2014, at 04:40, jagguy <andrew@itfutures.edu.au> wrote:

Hi,

I am wanting to add a new record  where I have a form input and the user types in the details of the new record in a form using cakePHP ( I cant find anything in the cakePHP manual or google as yet).

I can do this already but I get too much information in the drop down list in the form entry .

 2 of the fields are foreign keys so the user needs to select the field instead of typing them in.

I have  simple case of student, teacher and tutor sessions where I am entering in data on tutor session only. The user selects the existing teacher and student name instead of typing them .

Now I dont get an error but  the drop down list for student and teacher has too much information like the name, id and the tablename.

I just want the name of student and teacher (only) to appear in the drop down list of the add new form entry.
I do get the right information just too much.

yes i checked the manual on cake php

model  class Tutorsession extends AppModel  {             public $belongsTo = array(          'Teacher' => array(              'className' => 'Teacher',              'foreignKey' => 'teacher_id'          ),           'Student' => array(              'className' => 'Student',              'foreignKey' => 'student_id'          ),      );  controller////////////    public function add() {            $this->set( 'st',$this->Tutorsession->find('all', array('contain' => array('Student'),              'fields' => array('DISTINCT Student.firstname') )));          $this->set( 'te',$this->Tutorsession->find('all', array('contain' => array('Teacher'),              'fields' => array('DISTINCT Teacher.firstname') )));    view  <?php  echo $this->Form->create('Tutorsession');   echo$this->Form->input('teacher_id', array('options' => $te));   echo$this->Form->input('student_id', array('options' => $st));











Connect with us: 

    

Please consider the environment before printing this email.
 
This e-mail and any attachments to it (the "Communication") is, unless otherwise stated, confidential,  may contain copyright material and is for the use only of the intended recipient. If you receive the Communication in error, please notify the sender immediately by return e-mail, delete the Communication and the return e-mail, and do not read, copy, retransmit or otherwise deal with it. Any views expressed in the Communication are those of the individual sender only, unless expressly stated to be those of National Training and Solutions Provider Pty Ltd ABN 34 123 831 023, or any of its related entities. NTSP does not accept liability in connection with the integrity of or errors in the Communication, computer virus, data corruption, interference or delay arising from or in respect of the Communication.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: