cakephp canot在数据库中保存表单控制字段

时间:2018-09-15 17:29:59

标签: cakephp

我正在尝试做一个简单的cakephp应用程序!

我有一个创建新文章的表格。我的问题是我有artice slug的输入字段,但是当我cakephp提交表单时,数据库中的slug字段仍然为空。.

这是我articleController中的add方法

public function add(){


    $article = $this->Articles->newEntity(); //gffdgfd
        if ($this->request->is('post')){

        $this->Articles->patchEntity($article, $this->request->data());
            if($this->Articles->save($article)){

            $this->Flash->success(__('Your Article has been saved!'));
            return $this->redirect(['action' => 'index']);


        }

        $this->Flash->error(__('Cannot save article! Please try again!!'));
    }
 $this->set('article', $article);

}

和我的add.ctp

<h1>Add Article</h1>

<?php

echo $this->Form->create($article);

echo $this->Form->control('user_id', ['type' => 'hidden', 'value'=> 1 ]);
echo $this->Form->control('published', ['type' => 'hidden', 'value'=> 1 ]);
echo $this->Form->control('title');
echo $this->Form->control('slug');
echo $this->Form->control('body', ['rows' => 5 ]);
echo $this->Form->button(__('Save Article'), ['class' => 'button', 'style' => 'margin-right:10px; margin-left:10px']);
echo $this->Html->link('Back', ['action' => 'index'], ['class' => 'button']); 
echo $this->Form->end();

?>

1 个答案:

答案 0 :(得分:0)

如果请求数据中存在子弹字段,则应检查是否可以在您的实体中访问此字段以进行分配。查看文件src/Model/Entity/Article.php,在类主体顶部,您将有一个名为$_accessible的数组-检查是否存在子弹字段,如果不存在,则将其设置为true

protected $_accessible = [
    /* other fields */
    'slug' => true
];

请检查有关文档中属性分配的更多信息:CakePHP 3 Entities - Mass Assignment