原则2:如何使用sql代码列扩展实体?
我有实体原则2:如何使用sql代码列扩展实体?,sql,doctrine-orm,native-sql,customcolumn,Sql,Doctrine Orm,Native Sql,Customcolumn,我有实体 mh\BTBundle\Entity\Post: type: entity table: post repositoryClass: mh\BTBundle\Repository\PostRepository fields: id: id: true type: integer unsigned: true nullable: false
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 have 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();
而不是
$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();
$this->getEM()->createQueryBuilder()
->选择('p')
->from('BTBundle:Post','p')
->其中('p.isModered=true')
->andWhere('p.pubDate<:date')
->andWhere('p.user=:user')
->setParameter(':date',new\DateTime())
->setParameter(':user',$user)
->getQuery()->execute();
最简单的方法是创建一个计算的。像这样
CREATE TABLE dbo.Example
(
ExampleId int IDENTITY (1,1) NOT NULL,
Value1 INT
Value2 INT
ComputedCol AS Value1 + Value2
);
您可以在AS语句中使用任何有效的SQL逻辑。是否尝试过:->where('p.pubDateCREATE TABLE dbo.Example
(
ExampleId int IDENTITY (1,1) NOT NULL,
Value1 INT
Value2 INT
ComputedCol AS Value1 + Value2
);