1:1 Beziehung speichern

Anleitungen und Quellcode-Auszüge die den Start vereinfachen sollen.

1:1 Beziehung speichern

Beitragvon getit am Sa 28. Feb 2009, 15:49

Nachdem ich jetzt lange gebraucht habe das hinzubekommen, dachte ich mir ich schreibe mal nen kleines Tutorial.

Dabei nochmal vielen Dank an euromark :!:


Vorerst das Datenmodell:
uml.gif
uml.gif (50.05 KiB) 142-mal betrachtet

Es geht in diesem Tutorial nur um die Beziehung zwischen 'clients' und 'progresses' (und die Tabellen die da noch dahinter hängen).

Die 1:1-Beziehung zwischen 'clients' und 'progresses'
Die Models:
Client: hasOne = array('Progress');
Progress: belongsTo = array('Client');

Die Action (ClientsController/add):
Code: Alles auswählen

function add() {
    if (!empty($this->data)) {
        $this->Client->create();
        if ($this->Client->save($this->data)) {
               
            $this->data['Progress']['client_id'] = $this->Client->id;
            $this->data['Progress']['employee_id'] = $this->data['Progress']['Employee']['name'];
            $this->data['Progress']['kind_id'] = $this->data['Progress']['Kind']['kind'];
            $this->Client->Progress->save($this->data);
               
            $this->Session->setFlash('Der Klient wurde angelegt.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Der Klient konnte nicht angelegt werden.');
        }
    }
       
    ...
    $this->set('progresses', $this->Client->Progress->find('list', array('fields' => 'date')));
    $this->set('employees', $this->Client->Progress->Employee->find('list'));
    $this->set('kinds', $this->Client->Progress->Kind->find('list', array('fields' => 'kind')));
}
 

Hier habe ich einige Aufrufe von set() weggelassen für die ganzen anderen Beziehungen.
Aber wie gesagt, geht es hier nur um die 1:1-Beziehungen.

View (add.ctp) - nur Auszug:
Code: Alles auswählen

...
echo $html->tag('div', null, array('id' => 'tabs'));
$fields = $processes->fields();
$i = 0;
foreach ($fields as $field) {  
    $field['options']['id'] = "$i";
    echo $form->input($field['field_name'], $field['options']);
    $i++;
}
echo $html->tag('/div') . "\n";
...
 


Und dann noch der Helper (processes.php):
Code: Alles auswählen

<?php
class ProcessesHelper extends AppHelper {
       
    function fields() {

        $fields = array(
       
            $employees = array(
                'field_name'    =>  'Progress.Employee.name',
                'options'       =>  array(
                    'label'         =>  'Mitarbeiter'
                )
            ),
       
            $previous_employees = array(
                'field_name'    =>  'Progress.previous_employees',
                'options'       =>  array(
                    'label'         =>  'bearbeitet von'
                )
            ),
           
            $date_of_meeting = array(
                'field_name'    =>  'Progress.date',
                'options'       =>  array(
                    'label'         =>  'Datum',
                    'dateFormat'    =>  'DMY'
                )
            ),
           
            $kind = array(
                'field_name'    =>  'Progress.Kind.kind',
                'options'       =>  array(
                    'label'         =>  'Art des Gesprächs'
                )
            ),
           
            $previous_kinds = array(
                'field_name'    =>  'Progress.previous_kinds',
                'options'       =>  array(
                    'label'         =>  'vorherige Arten'
                )
            ),
           
            $location = array(
                'field_name'    =>  'Progress.location',
                'options'       =>  array(
                    'label'         =>  'Ort'
                )
            ),
           
            $previous_locations = array(
                'field_name'    =>  'Progress.previous_locations',
                'options'       =>  array(
                    'label'         =>  'vorherige Orte'
                )
            ),
           
            $duration = array(
                'field_name'    =>  'Progress.duration',
                'options'       =>  array(
                    'label'         =>  'Dauer'
                )
            ),
           
            $total = array(
                'field_name'    =>  'Progress.total_duration',
                'options'       =>  array(
                    'label'         =>  'gesamte Dauer'
                )
            ),
           
            $topic = array(
                'field_name'    =>  'Progress.topic',
                'options'       =>  array(
                    'label'         =>  'Thema'
                )
            ),
           
            $further_informations = array(
                'field_name'    =>  'Progress.further_information',
                'options'       =>  array(
                    'label'         =>  'weitere Informationen'
                )
            ),
           
            $action = array(
                'field_name'    =>  'Progress.action',
                'options'       =>  array(
                    'label'         =>  'Maßnahme'
                )
            ),
           
            $previous_actions = array(
                'field_name'    =>  'Progress.previous_actions',
                'options'       =>  array(
                    'label'         =>  'bereits getroffene Maßnahmen'
                )
            )
        );
        return $fields;
    }
}
?>
 


So damit sollte es dann funktionieren.
:D Bei mir gehts.
getit
 
Beiträge: 105
Registriert: Mo 22. Dez 2008, 13:06
CakePHP-Version: 1.2.1.8004
OS: Vista

Zurück zu Tutorials und Snippets

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 0 Gäste

cron