CakePHP 2.6.7 hasMany Data not saved

I am using CakePHP 2.6.7.

The user will be able to add several cars and several addresses to his profile. He will also be able to associate multiple addresses with one vehicle and associate one address with multiple vehicles.

Then I have a User model with:

class User extends AppModel { public $hasMany = array( 'Car', 'Address' ); } 

Car Model with:

 class Car extends AppModel { public $belongsTo = array( 'User' ); public $hasAndBelongsToMany = array( 'Address' => array( 'unique' => 'keepExisting' ) ); } 

Address Model:

 class Address extends AppModel { public $hasAndBelongsToMany = array( 'Car' => array( 'unique' => 'keepExisting', ), ); public $belongsTo = array( 'User' ); } 

I have a form so that the user can edit their machines (only one at the moment):

[...]

  <legend>Mes voitures</legend> <?php for($nbrvoiture = 0; $nbrvoiture <= 0; $nbrvoiture++) { ?><h3>Voiture <?php echo $nbrvoiture+1?></h3><?php $myLabelOptions = array('text' => "Marque"); echo $this->Form->input('Car.'.$nbrvoiture.'.CarMake', array('label' => array_merge($mainLabelOptions, $myLabelOptions))); $myLabelOptions = array('text' => "Modèle"); echo $this->Form->input('Car.'.$nbrvoiture.'.CarModel', array('label' => array_merge($mainLabelOptions, $myLabelOptions))); $myLabelOptions = array('text' => "Plaque d'immatriculation"); echo $this->Form->input('Car.'.$nbrvoiture.'.NumberPlate', array('label' => array_merge($mainLabelOptions, $myLabelOptions))); echo $this->Form->submit("Valider", array( 'class' => 'btn btn-default col-sm-offset-2' )); } 

The fact is that I can not save data in my database. Here is part of the user controller code:

 function edit() { // On récupère l'ID de l'utilisateur $user_id = $this->Auth->user('id'); // Si l'utilisateur n'a pas d'ID => il n'est pas connecté => il ne peut pas éditer son profil if(!$user_id){ $this->redirect('/'); die(); } debug($this->User->find('all')); $this->User->id = $user_id; $passError = false; if($this->request->is('put') || $this->request->is('post')){ $d = $this->request->data; $d['User']['id'] = $user_id; debug($d); if($this->request['pass'][0]=='cars') { if($this->User->saveAssociated($d, array('deep' => true))){ $this->Session->setFlash(__("Les informations sur la voiture ont bien été modifiées"), 'alert', array ( 'plugin' => 'BoostCake', 'class' => 'alert-success' )); }else{ $this->Session->setFlash(__("Impossible de modifier ou d'ajouter les infos"), 'alert', array ( 'plugin' => 'BoostCake', 'class' => 'alert-danger' )); } } 

When I save the data, it shows an error.

In my database, I have these 4 tables:

 Users(id, username, mail, password, created, lastlogin, active, firstname, lastname, gender, birthdate, phonenumber) Cars(id, CarMake, CarModel, NumberPlate, user_id) Addresses(id, address, city, state, postcode, country, user_id) Addresses_cars(address_id, car_id) 

This is an example of what I can get from the form ($ d variable):

 array( 'Car' => array( (int) 0 => array( 'CarMake' => 'Allard', 'CarModel' => '2005', 'NumberPlate' => '56QDS1' ) ), 'User' => array( 'id' => '1' ) ) 

I do not understand why this does not work ... Can you help me?

Thanks!:)

EDIT: I also tried with SaveAll, but I can't get it to work .. what's wrong?

+6
source share
1 answer

It has already been resolved by OP, but for future CakePHPers, if you have save() or saveAll() , which is not saved (marked with the if:else block), the most common problem is a validation error.

CakePHP 2.x Data Validation

CakePHP 3.x Data Validation

If this is not the case, try to look at any behavior that you use and check wherever you return and not (like the `beforeSave () method in it).

+1
source

Source: https://habr.com/ru/post/989867/


All Articles