gpt4 book ai didi

php - 用 Doctrine 查询语言写入 "NOT IN"

转载 作者:行者123 更新时间:2023-12-02 08:35:01 25 4
gpt4 key购买 nike

我有两张 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

但我不知道如何在 DQL for Doctrine 上构建它,有什么帮助吗?

添加方法到存储库并从 Controller 调用它

这是我在@Javad 回答离开后制作的代码:
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();
}

这就是我在 Controller 中调用它的方式:
$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



其中第 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();

请注意,我们通过 reversedBy/mappedBy 从非相关实体创建查询
我们需要对相关表的特定字段使用IDENTIY

关于php - 用 Doctrine 查询语言写入 "NOT IN",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22615215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com