我的朋友用 CakePHP 构建了一个大表单应用程序。它有一个名为“Player”的中心模型和一堆表格和模型,代表表单中不同类别的问题。她让我帮助实施 Auth,我们遇到了一些麻烦。根据 Cake 网站上的简单身份验证教程,我能够将用户和播放器链接在一起,但我无法在与播放器相关的表上实现身份验证。问题出现在应用程序的“添加”类部分。在我实现之前,添加函数看起来像这样:
public function name( $id =null) //Create the TEAM team name ,type
{
$this->Team->create();
//$this->request->data['Team']['user_id'] = $this->Auth->user('id');
if ($this->Team->save($this->request->data))
{
$idt=$this->Team->id;
$this->Session->setFlash(__('The Team' .$idt . ' has been saved'));
$this->redirect(array('action' => 'position', $idt));
}
else
{
$this->Session->setFlash(__('The Team could not be saved. Please, try again.'));
}
$players = $this->Team->Player->find('list' , array('conditions' => array('Player.id' => $id)));
$this->set(compact('players'));
}
提交后,将创建一个团队并设置 player_id。但是,当我取消注释读取用户 ID 的注释行时,玩家 ID 不会写入数据库,这会扰乱每个后续插入。所有表都有一个 user_id 和 player_id 列,尽管 user 没有 player_id 列。真的不知道这些有多相关,但这里是模型中的关联:
用户
public $hasOne = array(
'Player' => array(
'className' => 'Player',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
));
播放器
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'ContIllnessDetail' => array(
'className' => 'ContIllnessDetail',
'foreignKey' => 'player_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'ContInjuryDetail' => array(
'className' => 'ContInjuryDetail',
'foreignKey' => 'player_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'NewIllnessDetail' => array(
'className' => 'NewIllnessDetail',
'foreignKey' => 'player_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'NewInjuryDetail' => array(
'className' => 'NewInjuryDetail',
'foreignKey' => 'player_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'PreviousInjuryDetail' => array(
'className' => 'PreviousInjuryDetail',
'foreignKey' => 'player_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
和团队
public $hasOne = array(
'TrainingDetail' => array(
'className' => 'TrainingDetail',
'foreignKey' => 'team_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $belongsTo = array(
'Player' => array(
'className' => 'Player',
'foreignKey' => 'player_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
我还按照 Auth 教程将 isOwnedBy 方法复制到了每个模型中。有人对我如何解决这个问题有任何建议吗?