Save datas with multiple relationships with cakephp 1.2

32 Views Asked by At

I’ve been stuck for a couple of months on an old cakephp 1.2 I don’t understand the logic of this framework and I am totally lost ....

I have a form to add/remove from my table called contract (see below) As you can see, I want to be able to add 1 zone per form. I managed to create a table of performances with their own zone here.

 array(
    'files' => '',
    'Contract' => array(
        'name' => '',
        'beggin' => '',
        'end' => '',
        'content1' => '<p><br></p>',
        'content2' => '<p><br></p>',
        'content3' => '',
        'content4' => '',
        'content5' => '',
        'content7' => '',
        'content8' => ''
    ),
    'Zone' => array(
        (int) 0 => array(
            'zone_name' => 'Bat A',
            'id_prestation' => array(
                (int) 0 => '10',
                (int) 1 => '11'
            )
        ),
        (int) 1 => array(
            'zone_name' => 'Bat B',
            'id_prestation' => array(
                (int) 0 => '10',
                (int) 1 => '12',
                (int) 2 => '13',
                (int) 3 => '14'
            )
        )
    )
 )

so far, I have no difficulties. the relation between the tables Contract and Prestation is a $hasAndBelongsToMany here is my form :

form

My problem is that when the form is submitted, the data are not saved. Could someone please explain what is it that i’m doing wrong and explain what i should do plz ? Below is all my codes that you may need to get what i’m trying to explain:

<?php
 class Contract extends AppModel {
    public $hasMany = array('Document', 'Statu');
    public $hasAndBelongsToMany = array('Prestation');
    public $belongsTo = array('Client', 'Residence');   public function contracts() {
        return $this->find(
            'all',
            array(
                'conditions' => array(
                    'Contract.is_active' => false
                )
            )
        );
    }
 }

My function of adding a new contract:

    public function add() {       if ($this->request->is(array('post', 'put')))
            {
                // TODO: Rewrite or remove the two next conditions when client and residences will be re-implemented.
                // SUPPRESSION DU CLIENT
                if (!empty($this->request->data['Contract']['client_id'])) {
                    unset($this->request->data['Client']);
                } else {
                    unset($this->request->data['Contract']['client_id']);
                }           // SUPPRESSION DE LA RESIDENCE
                if (!empty($this->request->data['Contract']['residence_id'])) {
                    unset($this->request->data['Residence']);
                } else {
                    unset($this->request->data['Contract']['residence_id']);
                }           // AJOUT DU STATUS
// AJOUT DES PRESTATIONS
                if (!empty($this->request->data['Zone'])) {
                    foreach ($this->request->data['Zone'] as $key => $zone) {
                        $this->request->data['Zone'][$key] = $zone;
                    }
                }
                $this->request->data['Statu'] = array( 0 => array( 'status' => 'proposed' ) );           // AJOUT DU CONTRAT
                $this->Contract->create();           $this->request->data['Contract']['is_active'] = false;
                if ($this->Contract->saveAll($this->request->data))
                {
                    $this->Session->setFlash(__('Contrat enregistré.'));
                    return $this->redirect(array('controller' => 'contracts', 'action' => 'index'));
                }           $this->Session->setFlash(__("Impossible d'enregistrer le devis."));
            }
            else
            {
                $this->set( 'clients', $this->Client->find('list') );
                $this->set( 'residences', $this->Residence->find('list') );
                $this->set( 'prestations', $this->Prestation->find('list') );
            }
        }

Thank you for your help

0

There are 0 best solutions below