作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两张 table company(id, ...)
和 company_has_wtax(company_id, ....)
.我需要获取所有不在 company_has_wtax
中的公司 table 。在原始 SQL 中应该是这样的:
SELECT id FROM company LEFT JOIN (company_has_wtax)
ON company.id = company_has_wtax.company_id
public function findCompanyByDocument() {
$q2 = $this->createQueryBuilder('ApprovalBundle:CompanyHasWtax c2');
$query = $this->createQueryBuilder('c')
->leftJoin('c.CompanyHasWtax', 'chw')
->where($query->expr()->notIn('c.id', $q2->select('c2.company')->getDQL())
);
echo $query->getQuery()->getSQL();
// return $query->getQuery()->getResult();
}
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('RegisterCompanyBundle:Company')->findCompanyByDocument();
ContextErrorException: Notice: Undefined variable: query in /var/www/html/kraken/src/Company/RegisterCompanyBundle/Entity/Repository/CompanyRepository.php line 40
->where($query->expr()->notIn('c.id', $q2->select('c2.company')->getDQL())
.
Company.php
class Company {
/**
* @ORM\Id
* @ORM\Column(type="integer", unique=true, nullable=false)
* @ORM\GeneratedValue
*
*/
protected $id;
/**
* @ORM\Column(name="legal_name",type="string",nullable=false,length=255)
* @Assert\NotBlank(message="Este valor no debería estar vacío.")
* @Assert\Regex("/^[a-zA-Z0-9ñÑÁÉÍÓÚñáéíóú\s\,]+$/", message="Este valor debería ser de tipo alfanumérico.")
*/
protected $legalName;
/**
* @ORM\Column(name="social_reason",type="string",nullable=false,length=255)
* @Assert\NotBlank(message="Este valor no debería estar vacío.")
* @Assert\Regex("/^[a-zA-Z0-9ñÑÁÉÍÓÚñáéíóú\s\,]+$/", message="Este valor debería ser de tipo alfanumérico.")
*/
protected $socialReason;
/**
* @ORM\Column(name="tax_id",type="string",nullable=false)
* @Assert\NotBlank(message="Este valor no debería estar vacío.")
* @Assert\Regex("/^[\d{9}$]/", message="Este valor debería tener exactamente 9 caracteres.")
*/
protected $taxId;
/**
* @ORM\OneToOne(targetEntity="Common\MediaBundle\Entity\Media")
* @ORM\JoinColumn(name="logo", referencedColumnName="id")
*/
protected $logo;
/**
* @ORM\OneToOne(targetEntity="Common\MediaBundle\Entity\Media")
* @ORM\JoinColumn(name="banner", referencedColumnName="id")
*/
protected $banner;
/**
* @ORM\OneToOne(targetEntity="Company\RegisterCompanyBundle\Entity\NCompanyType")
* @ORM\JoinColumn(name="type", referencedColumnName="id")
*/
protected $type;
/**
* @ORM\OneToOne(targetEntity="Company\RegisterCompanyBundle\Entity\NCompanyStatus")
* @ORM\JoinColumn(name="status", referencedColumnName="id")
*/
protected $status;
/**
* @ORM\Column(type="integer",nullable=false,length=1)
*/
protected $certified;
}
CompanyHasWtax.php
class CompanyHasWtax {
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="\Company\RegisterCompanyBundle\Entity\Company")
* @ORM\JoinColumn(name="company", referencedColumnName="id")
*/
protected $company;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="\Configuration\FeeBundle\Entity\Fee")
* @ORM\JoinColumn(name="wtax", referencedColumnName="id")
*/
protected $wtax;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="\User\SecurityBundle\Entity\User")
* @ORM\JoinColumn(name="kuser", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\Column(name="from_date", type="datetime")
*/
protected $from_date;
/**
* @ORM\Column(name="to_date", type="datetime")
*/
protected $to_date;
}
public function findCompanyByDocument() {
$sql = "SELECT * FROM company c WHERE c.id NOT IN (SELECT chm.company FROM company_has_media chm WHERE chm.company = c.id)";
$em = $this->getEntityManager();
return $em->getConnection()->fetchAll($sql);
}
最佳答案
尝试这个:
$q2 = $this->createQueryBuilder('c')
->select('IDENTITY(c2.company)')
->join('RegisterCompanyBundle:CompanyHasMedia', 'c2', 'WITH', 'c2.company = c.id');
$query = $this->createQueryBuilder('c3');
$query->where($query->expr()->notIn('c3.id', $q2->getDQL()));
$companies = $query->getQuery()->getResult();
关于php - 用 Doctrine 查询语言写入 "NOT IN",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22615215/
我是一名优秀的程序员,十分优秀!