0
votes

I am a beginner in cakePHP , i want to save some data into my database , but i always get : the Message : Sorry, Error while saving data.

Here is my code : ( Controller : UsersController.php : ( and i have added the Model 'UserState to the array $uses )

if($onbreak == 'true'){
    $userStatus = 1;
   //$response['userStatus'] = 1;
} else {
    $userStatus = 0;
    //$response['userStatus'] = 0;
}

//add the new user_status to the $newUserState
$newUserState['UserState']['user_id'] = $userID;// $userID = 1
$newUserState['UserState']['user_status'] = $userStatus;
//adding values for fields which should not be NULL
$newUserState['UserState']['user_phone_nb'] = '4343';

//

saving data
if($this->UserState->save($newUserState)){
    $response['success'] = 'true';
    $response['message'] = '';
}else{
    $response['success'] = 'false';
    $response['message'] = 'Sorry, Error while saving data.';
}      

Thanks in advance

EDIT : Structure of the table userstates :

id : BIGINT primary key AUTO INCREMENT, user_id : INT NOT NULL, user_status : SMALLINT NOT NULL, user_phone_nb = VARCHAR(15) NULL

the Model Userstate :

class Userstate extends AppModel {

    var $useTable = 'userstates';
    public $name = 'UserState';
}

EDIT 2 :

when i debugg the variable $newUserState i got this ( which seems that is OK ) :

Array
(
    [UserState] => Array
        (
            [user_id] => 18
            [user_status] => 0
            [user_phone_nb] => 4343
        )

)
5
Do you have DEBUG set to 2 in the core.php?Lars
see my edit , i've added the result of my debug of the array $newUserStateHoucine

5 Answers

2
votes

I'm not sure what you are doing here, could you post your table structure and the insert-query cake is generating? You find that on the bottom if you use the standard cake-layout and have debugging mode on.

I suspect (but am not sure without further data) that there is another field in the table wich needs a value (cannot be null).

1
votes

I am assuming the field in the DB is 'userStatus' and there is only one value to save. But this should work and you should be able to expand on it to save other values.

Hope this helps.

Saving a new Record

if($onbreak == 'true'){
    $userStatus = 1;
} else {
    $userStatus = 0;
}

//add the new user_status to the $newUserState
$newUserState['UserState']['user_id'] = $userID;// $userID = 1
$newUserState['UserState']['user_status'] = $userStatus;
//adding values for fields which should not be NULL
$newUserState['UserState']['user_phone_nb'] = ' ';

// Must include id as it is a primary Key and thus required for saving.
$newUserState['UserState']['id'] = $userstate_id;


//saving data
if($this->UserState->save($newUserState)){
    $response['success'] = 'true';
    $response['message'] = '';
}else{
    $response['success'] = 'false';
    $response['message'] = 'Sorry, Error while saving data.';
}

Ok try this.

Rename your Model to user_state.php

Then add the following to it, based on Cake 1.2 cookbook http://book.cakephp.org/view/436/useTable

<?php 
class Userstate extends AppModel {

    var $useTable = 'userstates';
    public $name = 'UserState';
}

Also in the config/core.php make sure the line with debug reads as follows.

Configure::write('debug', 2);

Try this and let me know your results.

0
votes

Does this work for you:

$this->UserState->set('UserStatus', 'New title for the article');
$this->UserState->save();

I hope it helps

0
votes

i found the solution,

in my Model , i have the variable $validate with some parameters which have the required variable setted to true. so i've three Solutions :

First :

just commenting the variable and dont use it ^^

Second:

just initialize the required variables with a default value.

Third :

i found that in the doc , we can tell cakePHP to save our data without perform validation :

save(array
$donnees = null, boolean $valider = true, array $listeDesChamps = array())

so all what i did is saving data and passing the second parameter with a false value like this : $this->UserState->save($newUserState, false); , ( personally i've used this Third Solution ^^ )

Thanks you all for your help :)

Regards,

0
votes

Some tips at the end:

  1. For cases like these it's nice to have multiple validation sets made possbile by this: http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model

  2. Next time post complete code. The validation thing would be first guessed if you hadn't post just a 4 line represenation of your model which everyone thought was final.

  3. You should reinclude the sql dump in your layout. It will not show up in live mode due of DEBUG => 0 and will help you enormous in situation like these.

  4. I think the class name of the UserState model should be "UserState" and like already mentioned the file name "user_state.php". That would save a line of code :P

Greetings func0der