3

我有实体

mh\BTBundle\Entity\Post:
    type: entity
    table: post
    repositoryClass: mh\BTBundle\Repository\PostRepository
    fields:
        id:
            id: true
            type: integer
            unsigned: true
            nullable: false
            generator:
                strategy: IDENTITY
        title:
            type: string
            length: 255
            nullable: false
        content:
            type: text
            nullable: false
        isModered:
            type: boolean
            nullable: false
        isPublished:
            type: boolean
            nullable: false
        pubDate:
            type: datetime
            nullable: false

现在在 1 分钟的 cron 调用脚本中设置 isPublished = true 如果 post 有 isModered = true 和 pubDate < :now。

$this->getEM()->createQueryBuilder()
            ->update('BTBundle:Post p')
            ->set('p.isPublished', 'true')
            ->where('p.pubDate < :date AND p.isModered = true')
            ->setParameter(':date', new \DateTime())
            ->getQuery()->execute();

但是我想定义自定义列 isPublished,如果 SQL ( p.pubDate < CURDATE() AND p.isModered = 1) 则返回 true。我怎样才能做到?

我想用

$posts = $this->getRepository('Post')->findBy(array(
            'user' => $user,
            'isPublished' => true,
        ));

代替

$this->getEM()->createQueryBuilder()
                ->select('p')
                ->from('BTBundle:Post', 'p')
                ->where('p.isModered = true')
                ->andWhere('p.pubDate < :date')
                ->andWhere('p.user = :user')
                ->setParameter(':date', new \DateTime())
                ->setParameter(':user', $user)
                ->getQuery()->execute();
4

1 回答 1

0

最简单的方法是创建一个计算。所以像这样。

CREATE TABLE dbo.Example
(
    ExampleId int IDENTITY (1,1) NOT NULL,
    Value1 INT
    Value2 INT
    ComputedCol AS Value1 + Value2
);

您可以在 AS 语句中使用任何有效的 SQL 逻辑。

于 2013-07-12T13:47:11.520 回答