如何在cakephp中隐藏id输入并为其赋值

如何在cakephp中隐藏id输入并为其赋值,cakephp,Cakephp,我有两个相关的模型,我想做的是在我的添加视图中,我想将id字段设置为隐藏字段,这样它就不会显示在我的添加视图中,当我将其设置为隐藏时,表单不会提交。有没有办法将id值作为隐藏字段传递 这是我的添加视图 <?php echo $this->Form->create('ItQueryComment'); ?> <?php echo __('Add It Query Comment'); ?> <?php echo $this->Form->hid

我有两个相关的模型,我想做的是在我的添加视图中,我想将id字段设置为隐藏字段,这样它就不会显示在我的添加视图中,当我将其设置为隐藏时,表单不会提交。有没有办法将id值作为隐藏字段传递

这是我的添加视图

<?php echo $this->Form->create('ItQueryComment'); ?>
<?php echo __('Add It Query Comment'); ?>
<?php
echo $this->Form->hidden('it_query_id');
echo $this->Form->input('comment');
?>

<?php echo $this->Form->end(__('Submit')); ?>

提前感谢您

您不会将它们添加到视图表单中,然后只会再次将它们传递给控制器

在保存之前,请尝试将这些内容添加到数据数组中:

if ($this->request->is('post')) {
    $this->ItQuery->create();
    // add the content before passing it on to the model
    $this->request->data['ItQuery']['it_query_id'] = $id;
    if ($this->ItQuery->save($this->request->data)) {
        ... 
    }
}
表单不必知道它们。没有人可以阅读或篡改它们


请参见

Nice post,Mark!我在StackOverflow上的活动时间不长,但我越来越觉得需要一些编写良好的CakePHP教程/最佳实践页面来指导人们。(如何以及要清除哪些缓存目录,如何启用调试…),在编写答案时可能会节省大量时间:)@mark如果我在我的控制器$this->request->data['ItQuery']['it_query_id']='id'中添加此代码;它给了我一个未定义索引的错误:它不太可能-这个错误应该是同一行吗?还要注意的是,您只需在此处将一个变量放入
'
if ($this->request->is('post')) {
    $this->ItQuery->create();
    // add the content before passing it on to the model
    $this->request->data['ItQuery']['it_query_id'] = $id;
    if ($this->ItQuery->save($this->request->data)) {
        ... 
    }
}