php - Symfony2, Doctrine2 update row without ID (this table is with only "one" row) -
i want create entity settings, have basic editable information page. i've created entity settings.php source:
<?php namespace acme\settingsbundle\entity; use doctrine\orm\mapping 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; } and not know, how tell in controller overwriting existing data, not creating new. controller 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 successfuly changed' ); } return $this->render('acmesettingsbundle:admin:index.html.twig', array('form' => $form)); } i didn't test it, believe, creates new settings object new data. help?
you should set field act id, if meant dummy.
assign default value use , should able update settings no problem.
usually, dbms wars absence of unique identifier same applies doctrine.
$settings = new settings(); $settings->setid(settings::dummy_identifier); // const within class # rest of logic # .... $em = $this->getdoctrine()->getmanager(); $em->persist($settings); try { $em->flush(); } catch (\pdoexception $e) { } you take approach: persist every property single row. however, need build more complex form type , execute more queries.
edit:
you use raw sql less flexible:
# raw query $sql = "update settings set page_name = ?, page_description = ?, page_email = ?"; # data $params = array( $settings->getpagename(), $settings->getpagedesc(), $settings->getpageemail()); # must specify type due protection against sql injection $types = array(\pdo::param_str,\pdo::param_str,\pdo::param_str); # execute $stmt = $this->getentitymanager()->getconnection()->executeupdate($sql, $params, $types);
Comments
Post a Comment