gpt4 book ai didi

doctrine-orm - where-clause 中的 Doctrine 2 日期(查询构建器)

转载 作者:行者123 更新时间:2023-12-04 05:02:44 25 4
gpt4 key购买 nike

我有以下问题:我想要一个 where 子句来检查用户是否处于事件状态以及他是否有正确的日期。
一个用户包含以下内容:

  • 国家
  • 开始日期
  • 结束日期

  • 所以,State 应该保持在 1,然后他应该寻找 state = 1 并且当前日期在开始和结束日期之间。我现在有以下内容,并且工作正常。但是不需要开始和结束日期。所以它可能是NULL。我怎样才能得到这样的查询:
    SELECT * 
    FROM user/entity/user
    WHERE
    state = 1
    AND (
    (startdate <= CURRENT_DATE AND enddate >= CURRENT_DATE)
    OR startdate == NULL
    OR enddate == NULL
    )
    ,所以我得到了我所有的活跃用户,而不仅仅是临时用户。
    我现在已经设置了以下代码:
    存储库:
            public function searchUser($columns, $order_by, $order)
    {
    //Create a Querybuilder
    $qb = $this->_em->createQueryBuilder();

    //andx is used to build a WHERE clause like (expression 1 AND expression 2)
    $or = $qb->expr()->andx();

    //Select all from the User-Entity
    $qb->select('u')
    ->from('User\Entity\User', 'u');



    foreach($columns as $column => $name)
    {
    if($column == 'state')
    {
    if($columns['state']['value'] == '1') {
    $or = $this->betweenDate($qb, $or);
    $or = $this->like($columns, $qb, $or);
    } elseif($columns['state']['value'] == '2') {
    $or = $this->like($columns, $qb, $or);
    } elseif($columns['state']['value'] == '3') {
    $or = $this->outOfDate($qb, $or);
    }
    } else {
    //Get a where clause from the like function
    $or = $this->like($columns, $qb, $or);
    }
    }

    //Set the where-clause
    $qb->where($or);

    //When there is a order_by, set it with the given parameters
    if(isset($order_by)) {
    $qb->orderBy("u.$order_by", $order);
    }

    //Make the query
    $query = $qb->getQuery();

    /*echo('<pre>');
    print_r($query->getResult());
    echo('</pre>');*/

    //Return the result
    return $query->getResult();

    }

    public function betweenDate($qb, $or)
    {
    $or->add($qb->expr()->lte("u.startdate", ":currentDate"));
    $or->add($qb->expr()->gte("u.enddate", ":currentDate"));
    //$or->add($qb->expr()->orx($qb->expr()->eq("u.startdate", null)));
    $qb->setParameter('currentDate', new \DateTime('midnight'));

    return $or;
    }

    //This one works perfect
    public function like($columns, $qb, $or)
    {
    //Foreach column, get the value from the inputfield/dropdown and check if the value
    //is in the given label.
    foreach($columns as $label=>$column){
    $value = $column['value'];
    $or->add($qb->expr()->like("u.$label", ":$label"));
    $qb->setParameter($label, '%' . $value . '%');
    }

    return $or;
    }
    我也将此“用户搜索”用于其他领域。所以它应该从数据库中取出所有数据,只有状态不同,因为“过时”不在数据库中。所以它必须以不同的方式检查。希望有人能帮忙。
    问题解决
        $startdate = $qb->expr()->gt("u.startdate", ":currentDate");
    $enddate = $qb->expr()->lt("u.enddate", ":currentDate");

    $or->add($qb->expr()->orX($startdate, $enddate));

    最佳答案

    这就是我将如何构建 where 子句:

        //CONDITION 1 -> STATE = 1
    $state = $qb->expr()->eq( 'state', ':state' );

    //CONDITION 2 -> startdate <= CURRENT_DATE AND enddate >= CURRENT_DATE
    $betweenDates = $qb->expr()->andX(
    $qb->expr()->lte("u.startdate", ":currentDate"),
    $qb->expr()->gte("u.enddate", ":currentDate")
    );

    //CONDITION 3 -> startdate == NULL
    $startDateNull = $qb->expr()->isNull( 'startdate' );

    //CONDITION 4 -> enddate == NULL
    $endDateNull = $qb->expr()->isNull( 'enddate' );

    //CONDITION 5 -> <CONDITION 2> OR <CONDITION 3> OR <CONDITION 4>
    $dates = $qb->expr()->orX( $betweenDates, $startDateNull, $endDateNull );

    //CONDITION 6 -> <CONDITION 1> AND <CONDITION 5>
    $whereClause = $qb->expr()->andX( $state, $dates );

    关于doctrine-orm - where-clause 中的 Doctrine 2 日期(查询构建器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15925643/

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