duang5049 2013-10-07 14:03
浏览 59
已采纳

Symfony2,Doctrine2更新没有ID的行(此表只有“一行”)

I want to create my entity Settings, which will have basic editable information about my page. I've created my entity <kbd>Settings.php</kbd> with this source:

<?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;                  
}

and I do not know, how to tell in my controller that will be only overwriting existing data, not creating new. This is my controller <kbd>AdminController.php</kbd>

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

I didn't test it, but I believe, it creates a new Settings object with new data. Any help?

  • 写回答

1条回答 默认 最新

  • douyudouchao6779 2013-10-07 14:09
    关注

    You should always set some field to act as an ID, even if it meant to be dummy.

    Assign some default value that you will always use and then you should be able to update your settings with no problem.

    Usually, DBMS wars you about absence of unique identifier so same applies to Doctrine.

    $settings = new Settings();
    $settings->setId(Settings::DUMMY_IDENTIFIER); // const within class
    
    # rest of the logic
    # ....
    $em = $this->getDoctrine()->getManager();
    $em->persist($settings);
    
    try {
        $em->flush();
    } catch (\PDOException $e) {
    }
    

    You could take another approach: persist every property as a single row. However, you would need to build more complex form type and execute more queries.

    EDIT:

    You could use raw SQL but that is 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 to protection against sql injection
    $types = array(\PDO::PARAM_STR,\PDO::PARAM_STR,\PDO::PARAM_STR);
    
    # execute it
    $stmt = $this->getEntityManager()->getConnection()->executeUpdate($sql, $params, $types);
    

    http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#using-prepared-statements

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员