我正在尝试保存名为"EvidenceController.php"的控制器中的数据。我遇到的问题是,并非所有与我的备注模型的关联都会保存。只有备注与证据的关联才会保存,并且只会保存evidence_id和创建日期。Remark的其他关联都无法挽救。以下是我对项目、证据、备注、用户、evidences_remarks users_remarks的表格设置:
projects.id、项目标题、项目描述、项目已创建、项目已批准、项目批准者、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.create
remarks.id, remarks.evidence_id, 备注.备注, 备注.创建
users.id, 用户.用户名, 用户.密码, users.full_name, 用户.类型, 用户.创建
evidences_remarks.id, evidences_remarks.evidence_id, evidences_remarks.remark_id
users_remarks.id, users_remarks.user_id, users_remarks.remark_id
这是我的模型:
项目.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' => '',
)
);
}
证据.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' => '',
)
);
}
备注.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' => ''
)
);
}
用户.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' => '',
)
);
}
证据控制者.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);
}
}
}
}
我注释掉了重定向,以便我可以看到打印的数据数组。
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');
?>
这是从证据控制器打印的数组.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 ) )
与项目、证据和用户的所有其他关联都有效。如何从备注中获取数据以保存到其适当的关联中?
任何帮助将不胜感激!
您已覆盖项目模型中的$hasAndBelongsToMany属性(也在备注.php中)。
尝试:
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' => '',
)
);
我必须对数组进行切片才能得到我想要的结果,这是我的代码:
证据控制者.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));
}
}
}
不确定这是否是最佳实践,但我为我工作。