【问题标题】:cakephp Get value of field (parent key) when editingcakephp在编辑时获取字段(父键)的值
【发布时间】:2012-06-02 17:37:31
【问题描述】:

在 cakephp 2.1 上,我使用这种结构在保存时截取数据:

$this->request->data['Setup']['active'] = 1;  

现在,在编辑记录 (Dir) 之后,我想通过这种方式获取父 id 来重定向到它的父 (project_id):

$ownParentId = $this->request->data['Dir']['project_id'];

但它只是没有在此处传递任何值:

$this->redirect(array('controller' => 'projects', 'action' => 'edit', $ownParentId));

所以我的重定向失败了。

=============回复后==============
由于以下原因,project_id 似乎根本没有旅行:

在我的目录编辑表单上,我评论道:

//echo $this->Form->input('project_id');

因为用户不应该更改项目 ID。

我刚刚尝试取消注释该行,表单现在显示一个空的或空的选择控件,它什么都不发布。 我期待一个可以隐藏或禁用的编辑控件。

我的 dirs 表上的字段 project_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');

目录模型

var $belongsTo = array('Project');

也许我只需要一个漂亮的 find() 结构来填充我的编辑表单上的 project_id 控件? -或-
在保存编辑之前获取 project_id 值? (projectt_id 已保存,因为是编辑。
我已经在http://carlosgarcia.us/support/DirsController.txt 发布了完整的目录控制器。

你能帮忙吗?非常感谢。

卡洛斯

【问题讨论】:

  • Dir.project_id 是否在表单中 - 是否已发布?
  • @carlos 请包含您正在编辑的代码...
  • 谢谢!我已经更新了问题;我很难获得父密钥(project_id)。请阅读。再次感谢。

标签: cakephp redirect


【解决方案1】:

要在编辑或删除子记录后重定向,首先我得到了父 ID(我的控制器上的编辑/删除方法)

     $ownParentId = $this->Dir->find
                    (
                    'first', array
                (
                // recursive 0 so no child tables travel, use 1 to retrieve ALL tables  
                'recursive' => 0,
                'fields' => array('Dir.project_id'),
                'conditions' => array('Dir.id' => $id)
                    )
    );

保存后再重定向:

$this->redirect(array('controller' => 'projects', 'action' => 'edit', $ownParentId['Dir']['project_id']));

在添加孩子后重定向到父记录有点棘手 - 因为我是一个假人 -

1.- 在我看来,添加链接是否发送了一个我称为 parentProject 的参数:

echo $this->Html->link(__('Add'), array('controller' => 'dirs', 'action' => 'add', 'parentProject' => $project['Project']['id']));  

2.- 在我的子控制器中,将此参数设置为数组(添加方法):

$this->set('parentProject', $this->request->params['named']['parentProject']);

3.- 保存前,为我的子记录设置字段值:

$this->request->data['Dir']['project_id'] = $this->request->params['named']['parentProject'];  

4.- 保存后使用相同的值重定向:

$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'));

很高兴它成功了,并想分享给像我这样苦苦挣扎的初学者。

卡洛斯。

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多