cakephp canot save form control field in database

cakephp canot save form control field in database



i am trying to make an simple cakephp aplication!



I have a form that creates a new article.. my problem is that i have an input field for the artice slug but when i cakephp submits te form the slug field in database remains empty..



here is the add method from my articleController


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




and my 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 Answer
1



If slug field is present in your request data, then you should check if this field is accessible for assignment in your entity. Look at file src/Model/Entity/Article.php, on top of class body you will have an array named $_accessible - check if your slug field is present, and if not, set it to true:


src/Model/Entity/Article.php


$_accessible


true


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



Please check more about assignment of properties in docs: CakePHP 3 Entities - Mass Assignment



Thanks for contributing an answer to Stack Overflow!



But avoid …



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

Malaysia to Papua New Guinea by motorcycle?

List of Bhojpuri films

PHP code is not being executed, instead code shows on the page