我有实体
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();