Symfony 2 Doctrine 2使用连接表查询构建器进行更新

时间:2013-07-05 15:02:54

标签: php sql symfony doctrine-orm query-builder

我需要在查询构建器中(或者在最糟糕的情况下在dql中)转换像这样的sql请求

UPDATE code c, element_code ec
SET c.url = 'test' 
WHERE c.element_code_id = ec.id
AND ec.project_id = 5
AND (c.id = 1 OR c.id = 2)

这是MCD和模型

http://i.imgur.com/TvKhbGR.png?1

class Code
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var string $url
 *
 * @ORM\Column(name="url", type="string", length=255, nullable=true)
 */
protected $url;

/**
 * @ORM\OneToOne(targetEntity="CodeBundle\Entity\ElementCode", inversedBy="code")
 * @ORM\JoinColumn(name="element_code_id", referencedColumnName="id")
 */
protected $elementCode;
}

======

class ElementCode
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="CodeBundle\Entity\Project", inversedBy="elementsCode")
 * @ORM\JoinColumn(name="project_id", referencedColumnName="id")
 */
protected $project;

/**
 * @ORM\OneToOne(targetEntity="CodeBundle\Entity\Code", mappedBy="elementCode")
 */
protected $code;
}

======

class Project
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\OneToMany(targetEntity="CodeBundle\Entity\ElementArbo", mappedBy="project")
 */
protected $elementsCode;
}

这是我已经制作但不起作用的查询构建器:/

$qb = $this->_em->createQueryBuilder();
$q = $qb->update('CodeBundle:Code', 'c')
        ->innerJoin('CodeBundle:ElementCode', 'ec')

        ->set('c.url', ':url')
        ->setParameter('url', 'test')

        ->where('c.elementCode = ec.id')
        ->andWhere('ec.project = :project')
        ->setParameter('project', $project)
        ->andWhere("c.id = 1")
        ->orWhere("c.id = 2");

有什么想法吗?谢谢:))

0 个答案:

没有答案