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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)