编辑时cakephp获取字段(父键)的值
在cakephp 2.1上,我使用此结构在保存时拦截数据:编辑时cakephp获取字段(父键)的值,cakephp,redirect,Cakephp,Redirect,在cakephp 2.1上,我使用此结构在保存时拦截数据: $this->request->data['Setup']['active'] = 1; $this->redirect(array('controller' => 'projects', 'action' => 'edit', $ownParentId['Dir']['project_id'])); $this->redirect(array('controller' => 'proj
$this->request->data['Setup']['active'] = 1;
$this->redirect(array('controller' => 'projects', 'action' => 'edit', $ownParentId['Dir']['project_id']));
$this->redirect(array('controller' => 'projects', 'action' => 'edit', $this->request->data['Dir']['project_id']));
现在,在编辑记录(Dir)后,我想通过以下方式获取父id重定向到它的父(project_id):
$ownParentId = $this->request->data['Dir']['project_id'];
但这里没有传递任何值:
$this->redirect(array('controller' => 'projects', 'action' => 'edit', $ownParentId));
所以我的重定向失败了
====================================================由于以下原因,项目id似乎根本没有旅行: 在我的dir编辑表单上,我评论道:
//echo $this->Form->input('project_id');
因为用户不应该更改项目id
我刚刚尝试取消对该行的注释,表单现在显示一个空的或空的select控件,该控件不发布任何内容。
我希望有一个编辑控件可以隐藏或禁用
“我的目录”表上的字段项目id
CREATE TABLE IF NOT EXISTS `dirs` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`project_id` int(10) NOT NULL,
....
绑定到“我的项目”表上的id字段:
CREATE TABLE IF NOT EXISTS `projects` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`pr_number` varchar(10) NOT NULL,
`client_id` int(5) NOT NULL,
`exec_id` int(5) NOT NULL,
....
项目模型
var $hasMany = array('Daily', 'Dir');
dir模型
var $belongsTo = array('Project');
也许我只需要一个好的find()结构来填充编辑表单上的project\u id控件-或-要在保存编辑之前获取项目id值?(projectt\u id已保存,因为它是一个编辑。
我已经在上发布了完整的dirs控制器 你能帮忙吗?非常感谢
Carlos要在编辑或删除子记录后重定向,首先我获得父id(控制器上的编辑/删除方法) 然后在保存后重定向:
$this->request->data['Setup']['active'] = 1;
$this->redirect(array('controller' => 'projects', 'action' => 'edit', $ownParentId['Dir']['project_id']));
$this->redirect(array('controller' => 'projects', 'action' => 'edit', $this->request->data['Dir']['project_id']));
在添加一个子项后重定向到父项记录有点棘手,因为我是一个哑巴-
1.-在我看来,添加链接是否发送了一个参数,我称之为parentProject:
echo $this->Html->link(__('Add'), array('controller' => 'dirs', 'action' => 'add', 'parentProject' => $project['Project']['id']));
2.-在我的子控制器中,将此参数设置为数组(添加方法):
3.-保存前,设置我的子记录的字段值:
$this->request->data['Dir']['project_id'] = $this->request->params['named']['parentProject'];
4.-并在保存后使用相同的值重定向:
$this->request->data['Setup']['active'] = 1;
$this->redirect(array('controller' => 'projects', 'action' => 'edit', $ownParentId['Dir']['project_id']));
$this->redirect(array('controller' => 'projects', 'action' => 'edit', $this->request->data['Dir']['project_id']));
此外,这种方式允许我在添加/编辑子模型时获取父模型所需的所有数据:
$ProjectData = $this->Dir->Project->find
(
'first', array
(
// recursive 0 so no child tables travel, use 1 to retrieve ALL tables
'recursive' => 0,
'fields' => array('Project.name', 'Project.pr_e_start'),
'conditions' => array('Project.id' => $this->request->params['named']['parentProject'])
)
);
$this->set(compact('ProjectData'));
我很高兴它起作用了,我想和像我这样挣扎的首发球员分享
Carlos.表单中是否有Dir.project\u id?是否已发布?@Carlos请包含您正在编辑的代码…谢谢!我已更新了问题;我很难获取父项密钥(project\u id)。请阅读。再次感谢。