我正在尝试从名为“EvidenceController.php”的控制器中保存数据。我遇到的问题是,与我的备注模型的所有关联都不会保存。只有Remark与Evidence的关联才会保存,它只会保存evidence_id和创建的日期。 Remark的其他协会都不会保存。以下是项目,证据,备注,用户,证据,标记,用户标记的表格设置:
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
以下是我的模特:
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);
}
}
}
}
我注释掉了重定向,因此我可以看到打印的数据数组。
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');
?>
这是从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 ) )
项目,证据和用户的所有其他协会都有效。如何从备注中获取数据以保存到适当的关联?
非常感谢任何帮助!
答案 0 :(得分:0)
您已在项目模型中覆盖 $ hasAndBelongsToMany属性(也在Remark.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' => '',
)
);
答案 1 :(得分:0)
我不得不切割数组以获得我想要的结果是我的代码:
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));
}
}
}
不确定这是否是最佳做法,但我为我工作。