我想创建我的实体Settings,它将包含有关我的页面的基本可编辑信息。我Settings.php用这个来源创建了我的实体:
<?php
namespace Acme\SettingsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @ORM\Table(name="settings")
*/
class Settings
{
/**
* @ORM\Column(type="string", length=100)
*/
protected $page_name;
/**
* @ORM\Column(type="string", length=100)
*/
protected $page_description;
/**
* @ORM\Column(type="string", length=100)
*/
protected $page_email;
}
而且我不知道,如何在我的控制器中告诉它只会覆盖现有数据,而不是创建新数据。这是我的控制器AdminController.php
public function indexAction(Request $request)
{
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
}
$settings = new Settings();
$form = $this->createForm('settings', $settings);
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($settings);
try {
$em->flush();
} catch (\PDOException $e) {
// sth
}
$this->get('session')->getFlashBag()->add(
'success',
'Settings was successfuly changed'
);
}
return $this->render('AcmeSettingsBundle:Admin:index.html.twig', array('form' => $form));
}
我没有测试它,但我相信,它会创建一个带有新数据的新 Settings 对象。有什么帮助吗?