0

I am trying to save data from one of my controllers named "EvidenceController.php". The problem I am having is that not all the associations with my Remark Model will save. Only Remark's association with Evidence will save, and it only saves the evidence_id and the date created. None of Remark's other associations will save. Here are my table setups for projects, evidences, remarks, users, evidences_remarks, users_remarks:

projects.id, projects.title, projects.description, projects.created, projects.approved, projects.approvedby, projects.user_id

evidences.id, evidences.title, evidences.date, evidences.description, evidences.sourcetype, evidences.source, evidences.pdfloc, evidences.author, evidences.authorcred, evidences.user_id, evidences.created

remarks.id, remarks.evidence_id, remarks.remark, remarks.created

users.id, users.username, users.password, users.full_name, users.type, users.created

evidences_remarks.id, evidences_remarks.evidence_id, evidences_remarks.remark_id

users_remarks.id, users_remarks.user_id, users_remarks.remark_id

Here are my models:

Project.php

App::uses('AppModel', 'Model');

class Project extends AppModel {

    var $name = 'Project';
    public $displayField = 'title';


    public $validate = array(
        'id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),

            ),
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'title' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'created' => array(
            'datetime' => array(
                'rule' => array('datetime'),
            ),
        ),
        'approved' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'user_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
    );

    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public $hasAndBelongsToMany = array(
        'Evidence' => array(
            'className' => 'Evidence',
            'joinTable' => 'evidences_projects',
            'foreignKey' => 'project_id',
            'associationForeignKey' => 'evidence_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        ),
        'Remark' => array(
            'className' => 'Remark',
            'joinTable' => 'projects_remarks',
            'foreignKey' => 'project_id',
            'associationForeignKey' => 'remark_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );
}

Evidence.php

App::uses('AppModel', 'Model');

class Evidence extends AppModel {

    var $name = 'Evidence';
    public $displayField = 'title';

    public $validate = array(
        'id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'title' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'date' => array(
            'datetime' => array(
                'rule' => array('datetime'),
            ),
        ),
        'sourcetype' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'user_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'created' => array(
            'datetime' => array(
                'rule' => array('datetime'),
            ),
        ),
    );

    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public $hasMany = array(
        'Remark' => array(
            'className' => 'Remark',
            'foreignKey' => 'evidence_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
    );

    public $hasAndBelongsToMany = array(
        'Project' => array(
            'className' => 'Project',
            'joinTable' => 'evidences_projects',
            'foreignKey' => 'evidence_id',
            'associationForeignKey' => 'project_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );
}

Remark.php

App::uses('AppModel', 'Model');

class Remark extends AppModel {

    var $name = 'Remark';
    public $displayField = 'title';

    public $validate = array(
        'id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'evidence_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'created' => array(
            'datetime' => array(
                'rule' => array('datetime'),
            ),
        ),
    );

    public $hasAndBelongsToMany = array(
        'User' => array(
            'className' => 'User',
            'joinTable' => 'users_remarks',
            'foreignKey' => 'remark_id',
            'associationForeignKey' => 'user_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        ),
        'Project' => array(
            'className' => 'Project',
            'joinTable' => 'projects_remarks',
            'foreignKey' => 'remark_id',
            'associationForeignKey' => 'project_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );

    public $belongsTo = array(
        'Evidence' => array(
            'className' => 'Evidence',
            'foreignKey' => 'evidence_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

User.php

App::uses('AppModel', 'Model');

class User extends AppModel {

    var $name = 'User';
    public $displayField = 'title';

    public $validate = array(
        'id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'full_name' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            ),
        ),
        'type' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'created' => array(
            'datetime' => array(
                'rule' => array('datetime'),
            ),
        ),
    );

    public $hasMany = array(
        'Evidence' => array(
            'className' => 'Evidence',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public $hasAndBelongsToMany = array(
        'Remark' => array(
            'className' => 'Remark',
            'joinTable' => 'users_remarks',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'remark_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
        )
    );
}

EvidencesController.php

class EvidencesController extends AppController{
    public $components = array('Session');
    var $helpers = array( 'Form' );

    public function add(){
        $projectid  = isset($this->request->query['projectid']) ? $this->request->query['projectid']    :null;
        $projectData = $this->Evidence->Project->findById($projectid);
        $this->set('project',$projectData);
        if($this->request->is('post')){
            $this->Evidence->create();
            $this->request->data['Evidence']['user_id'] = Authcomponent::user('id');
            if($this->Evidence->saveAll($this->request->data)){
                $this->Session->setFlash('The Evidence has been created');
                //$this->redirect(array('controller' => 'projects', 'action'=> 'view', $projectid));
                print_r($this->request->data);
            }
        }
    }
}

I commented out the redirect so I could see the printed data array.

add.ctp

<h1>Create Evidence for <?php echo $this->Html->link($project['Project']['title'], array('controller' => 'projects', 'action'=> 'view',$project['Project']['id'])); ?></h1>
<?php
    echo $this->Form->create('Evidence');
    echo $this->Form->input('title');
    echo $this->Form->input('date');
    echo $this->Form->input('description');
    echo $this->Form->input('sourcetype');
    echo $this->Form->input('source');
    echo $this->Form->input('pdfloc');
    echo $this->Form->input('author');
    echo $this->Form->input('authorcred');
    echo $this->Form->input('Remark.remark');
    echo $this->Form->input('Project.id', array('type' => 'hidden', 'value' => $project['Project']['id']));
    echo $this->Form->end('Add Evidence');
?>

This is the array that is printed from EvidencesController.php

Array ( [Evidence] => Array ( [title] => EvTestTile [date] => Array ( [month] => 01 [day] => 25 [year] => 2015 [hour] => 09 [min] => 45 [meridian] => am ) [description] => EvTestDescription [sourcetype] => 1 [source] => EvTestSource [pdfloc] => EvTestPdfloc [author] => EvTestAuthor [authorcred] => EvTestAuthorcred [user_id] => 1 ) [Remark] => Array ( [remark] => ReTestRemark ) [Project] => Array ( [id] => 2 ) )

All other associations with projects, evidences, and users work. How do I get the data from remarks to save to its appropriate association?

Any help would be greatly appreciated!

user908759
  • 1,355
  • 8
  • 26
  • 48

2 Answers2

0

You have overridden $hasAndBelongsToMany properties in your Project model (also in Remark.php).

Try:

public $hasAndBelongsToMany = array(
    'Evidence' => array(
        'className' => 'Evidence',
        'joinTable' => 'evidences_projects',
        'foreignKey' => 'project_id',
        'associationForeignKey' => 'evidence_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    ),
    'Project' => array(
        'className' => 'Project',
        'joinTable' => 'evidences_projects',
        'foreignKey' => 'evidence_id',
        'associationForeignKey' => 'project_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    )
);
Salines
  • 5,674
  • 3
  • 25
  • 50
  • Thank you for your reply. I don't understand Why do I need to add a Project hasAndBelongsToMany association to Project? Also the Remark's Model has Evidence as a belongsTo association. With your Try example which model is that for? – user908759 Jan 25 '15 at 20:39
  • Look at your Project.php model, you have two time declared hasAndBelongsToMany properites. Second properties annul (overridden) the value of the first, and you can't save Evidence data. You have same sitauation in the Remark.php model. – Salines Jan 25 '15 at 21:29
  • Thanks once again for your help. I added the changes, and I get the same results... it still will not save the remarks associations. Not sure if this helps but if I add the following line $this->Evidence->Remark->saveAll($this->request->data); to EvidencesController.php in the add function. All the remarks associations save, BUT the Evidence data is saved twice. – user908759 Jan 25 '15 at 21:54
0

I had to slice the array to get the results I wanted here is my code:

EvidencesController.php

public function add(){
    $projectid  = isset($this->request->query['projectid']) ? $this->request->query['projectid']    :null;
    $projectData = $this->Evidence->Project->findById($projectid);
    $this->set('project',$projectData);
    $userData = $this->Evidence->User->findById(Authcomponent::user('id'));
    $this->set('user',$userData);
    if($this->request->is('post')){
        $this->Evidence->create();
        $this->request->data['Evidence']['user_id'] = Authcomponent::user('id');
        $RemarksArray = array_slice($this->request->data, 1);
        $EvidenceArray1 = array_slice($this->request->data, 0,1);
        $EvidenceArray2 = array_slice($this->request->data, 2);
        $EvidenceArray = array_merge($EvidenceArray1,$EvidenceArray2);

        if($this->Evidence->saveAll($EvidenceArray)){
            $RemarksArray1 = array_slice($this->request->data, 1);
            $RemarksArray = array_merge($RemarksArray1,array('Evidence'=>array('id'=>$this->Evidence->getLastInsertId())));
            if(strlen(trim($this->request->data['Remark']['remark'])) >0){
                $this->Evidence->Remark->saveAll($RemarksArray);
            }
            $this->Session->setFlash('The Evidence has been created With Remark length: '.strlen(trim($this->request->data['Remark']['remark'])));
            $this->redirect(array('controller' => 'projects', 'action'=> 'view', $projectid));
        }
    }
}

Not sure if it is best practices, but I works for me.

user908759
  • 1,355
  • 8
  • 26
  • 48