Domanda

I already searched many forums for my really strange issue, but I still can't figure out whats going wrong during my save process... The issue: Cake says, my data was saved, creates an autoincrement-ID but no record is stored in the database.

The environment

I have a cake-1.3.13 app running for some time and now needed to add another database table, which is of course related to other tables. My problem is saving records for the habtm-relation table, which looks like this:

    CREATE TABLE IF NOT EXISTS `employees_projects_rejectreasons` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `employees_project_id` int(10) unsigned NOT NULL,
  `rejectreason_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `employees_project_id` (`employees_project_id`,`rejectreason_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6;

I scaffolded the simple model only with basic validation criteria.

<?php
class EmployeesProjectsRejectreason extends AppModel {
var $name = 'EmployeesProjectsRejectreason';

var $validate = array(
    'employees_project_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'rejectreason_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);


//The Associations below have been created with all possible keys, those that are not needed can be removed

var $belongsTo = array(
    'EmployeesProject' => array(
        'className' => 'EmployeesProject',
        'foreignKey' => 'employees_project_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Rejectreason' => array(
        'className' => 'Rejectreason',
        'foreignKey' => 'rejectreason_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

I created several records for Rejectreasons and EmployeesProjects, so I have some valid entries here in the database. Now I want to link them together by creating a new record in the given employees_projects_rejectreasons table. I try to do this from another controller (the EmployeesProjectsController). Here is my latest attempt to save the data:

$this->EmployeesProject->EmployeesProjectsRejectreason->create();

$eprData = array(
    'EmployeesProjectsRejectreason' => array(
        'employees_project_id' => (int)$id,
        'rejectreason_id' => (int)$rrId
    )
);
if($this->EmployeesProject->EmployeesProjectsRejectreason->save($eprData)) {
    debug('successfully saved EPR with ID '.$this->EmployeesProject->EmployeesProjectsRejectreason->__insertID);
} else {
    debug('could not save EPR with employees_project_id='.$id.' and rejectreason_id='.$rrId);
}

Now what happens

After I make an attempt to save a record, my debug gives me the following success report:

successfully saved EPR with ID 4

So the save() call returned true, a new ID was created by the auto_increment function of mySQL. So far so good. But when I check my database, there was no record created. But the auto_increment_counter was increased by 1, as if a record was stored, but it wasn't.

Running the app with debug-level 2, I can see the generated SQL-statement from cake, which looks perfectly fine to me:

INSERT INTO `employees_projects_rejectreasons` (`employees_project_id`, `rejectreason_id`) VALUES (3, 3)

If I run this statement directly on the sql server, the record ist inserted correctly.

What I already tried

I already tried different approaches with the save procedure. I tried working with setters instead of a data-array:

$this->EmployeesProject->EmployeesProjectsRejectreason->set('employees_project_id', $id);

as well, but it made no difference. After I wrote a custom save-method in the EmployeesProjectsRejectreason-Model, calling it from the controller, but it always produced the same result.

I tried

  • deleting the model-cache
  • restarting the server-instances and the server itself
  • Deleting the table and creating it again
  • disabling validation in the model
  • removing the unique foreign-key index
  • Saving with hard-coded and existing ids as foreign key

Some more strange behaviour

The last tests with hard-coded IDs in my controller code confronted me with more riddles: If I try storing existent foreign_key-IDs, the data is not saved as before. But if both IDs are hardcoded and NOT EXISTING (I used invented IDs 345 AND 567, which are definetely not existing in the database) a record was finally inserted!

Moreover I scaffolded Models, Views and Controllers for the new tables. When I run the scaffolded view "myApp/employees_projects_rejectreasons/add" and add a new record, everything works just fine.

I'm just not able to save the record from other controllers. Since I already have a huge headache, solving this problem, I highly appreciate any hint for a solution!!

Thanks in advance guys!

È stato utile?

Soluzione

I finally found a solution to solve the issue. I still don't know, why the save code before did not work, but here is how I changed my code to make it work:

From my form, the data array comes in the following format:

Array
(
    [EmployeesProject] => Array
        (
            [id] => 10
            [user_id] => 0
            [additional_information] => some comment text
            [state] => absage
            [Rejectreason] => Array
                (
                    [0] => 1
                    [1] => 8
                )

        )
)

I searched for some solutions to save habtm relations in cakePHP directly with one call, but that does not seem to be possible in cake-1.3. So I created this pretty simple save routine in my EmployeesProjectController, which works perfectly fine for me:

if (!empty($this->data)) {
            if ($this->EmployeesProject->save($this->data)) {
                if(array_key_exists('Rejectreason', $this->data['EmployeesProject'])) {
                    foreach($this->data['EmployeesProject']['Rejectreason'] as $key => $rrId) {
                        $this->EmployeesProject->EmployeesProjectsRejectreason->create();
                        $this->EmployeesProject->EmployeesProjectsRejectreason->set('rejectreason_id', $rrId);
                        $this->EmployeesProject->EmployeesProjectsRejectreason->set('employees_project_id', $this->data['EmployeesProject']['id']);
                        if($this->EmployeesProject->EmployeesProjectsRejectreason->save()) {

                        }
                    }
                }
            }
        }

Thanks @Yoggi for supporting me solving this issue!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top