Php 更改视图数据数组中的值

Php 更改视图数据数组中的值,php,cakephp,Php,Cakephp,我在模型中有一些字段,我想在控制器中填充这些字段(而不是让用户输入一些数据..)。例如:curentUserId或当前日期。。。 我找到了两个解决方案 echo $this->Form->input('delivered_by', array('type' => 'hidden', 'value'=> $_SESSION['id'])); 将字段隐藏在视图中,并为其指定一个值 $\u POST['data']['Sample']['delivered\u by']=

我在模型中有一些字段,我想在控制器中填充这些字段(而不是让用户输入一些数据..)。例如:curentUserId或当前日期。。。 我找到了两个解决方案

echo $this->Form->input('delivered_by', array('type' => 'hidden', 'value'=> $_SESSION['id']));
  • 将字段隐藏在视图中,并为其指定一个值
$\u POST['data']['Sample']['delivered\u by']=777

  • 在控制器中给定值

  • 如果我在视图中发表评论,例如:

$this->Form->input('delivered_by')

我没有在控制器的数据数组中获得字段“delivered_by”,我无法更改它

有没有正确/错误的方法? 你们其他人是怎么做到的? 非常感谢。 致以最良好的祝愿

以下是控制器的外观:

if ($this->request->is('post')) {


        //$this->request->data['coordinate_x'] = 7777777;   //=>>works with hidden field in view

        $this->Sample->create();
        $this->request->data['Sample']['delivered_by'] = 7;
        debug($_POST);

        if ($this->Sample->save($this->request->data)) {
            $this->Session->setFlash(__('The sample has been saved.'));
            //return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The sample could not be saved. Please, try again.'));
        }
    }
“delivered_by”变量没有更改,它保持为空

array(
'_method' => 'POST',
'data' => array(
    'Sample' => array(
        'task_id' => '3',
        'delivered_by' => '',
        'date_of_delivery' => array(
            'month' => '09',
            'day' => '02',
            'year' => '2014'
        ),
        'sample_label' => '2',
        'coordinate_x' => '2',
        'coordinate_y' => '2',
        'coordinate_z' => ''
    )
)

)您不需要通过POST发送此数据。您已经在后端拥有了它

当您并没有任何外部依赖项(如currentUserId)时,可以通过模型上的回调来实现这些行为


当您有外部依赖项时,您应该决定如何最好地将其解耦。

您没有使用post通过post编辑它,您可以在控制器中编辑它,如下所示:

$this->data['Sample']['delivered_by'] = 77777;

我需要隐藏“echo$this->Form->input('delivered_by',array('type'=>'hidden'));”以便在数据数组中获得这些字段。使用$this->data['Sample']['delivery_by']=7;我无法更新数组,但使用$\u POST['data']['Sample']['delivered\u by']=7;我更改字段值(请参见debug($\u POST);)问题是:。我是否可以从视图中显示/删除该行,或者必须将其隐藏,以便从视图中获得数据数组中的“delivery_by”字段,然后我可以更改该值?现在,经过几个小时的尝试,我终于看到有两个数组。一个是POST,一个是models数组。我当时在看POST数组,当然没有字段“delivery by.”。。tnx..数据数组不可更改?但是有了我的新变量,我可以做任何我想做的事情??是的,你可以更改新变量,但你不能更改
这个->数据
你用新变量检查过它了吗?我现在添加了它,它可以工作了。但是我需要隐藏:echo$this->Form->input('delivered_by',array('type'=>'hidden');。。。如果我在debug()中从视图中删除这一行;我看不到变量“delivered\u by”,如果从隐藏字段中删除,则无法进入
$this->data
。为什么要删除此隐藏字段,以及您想要此字段的内容?
$sampleData = $this->request->data['Sample'];
$sampleData['delivered_by'] = 7;
if ($this->Sample->save($sampleData)) 
{
       $this->Session->setFlash(__('The sample has been saved.'));
       //return $this->redirect(array('action' => 'index'));
} 
else 
{
    $this->Session->setFlash(__('The sample could not be saved. Please, try again.'));
}