【问题标题】:not equals isnt working in cakephp3不等于在 cakephp 3 中不起作用
【发布时间】:2017-02-27 01:00:39
【问题描述】:

我无法让不等于条件起作用。发生的情况是它忽略了所有数据,因为cancelled_by 字段一直是null。如果cancelled_by 字段为null,则不等于不等于Tutor

这个小错误让我损失了 5 天的错误数据。我犯了一个错误吗?一旦我删除了不等于条件,所有数据都按预期检索。

下面的 2 行是我使用的,但都没有用。

'Lessons.cancelled_by not like' => '%Tutor%'   

'Lessons.cancelled_by !=' => 'Tutor' 
$searchDate = date('Y-m-d');

$query3 = $this->find()
    ->contain([
        'Tutors', 'Subjects', 'Students', 'Students.Guardians', 'XeroInvoices'
    ])
    ->select([
        'Lessons.id', 'Lessons.lesson_date', 'Lessons.start_time', 'Lessons.end_time',
        'Tutors.id', 'Tutors.first_name', 'Guardians.id', 'Tutors.last_name',
        'Lessons.timesheet_status', 'Students.id', 'Students.first_name',
        'Students.last_name', 'Students.has_credit', 'XeroInvoices.invoice_number',
        'Guardians.guardian_email', 'Guardians.guardian_mobile',
        'Guardians.guardian_last_name', 'Guardians.guardian_first_name',
        'Lessons.due_date', 'Lessons.cancelled_by', 'Subjects.name', 'Lessons.revenue',
        'Lessons.discount', 'Lessons.surcharge', 'Lessons.future_makeup_lesson_id',
        'Lessons.cancelled_pastlesson_id'
    ])
    ->where([
        'Lessons.due_date' => $searchDate,
        'Lessons.lesson_inactive' => false,
        'Students.has_credit like' => '%postpaid%',
        'Lessons.cancelled_by !=' => 'Tutor'
    ])
    ->order([
        'Guardians.id',
        'Lessons.lesson_date' => 'ASC',
        'Lessons.start_time' => 'ASC'
    ])
    ->hydrate(false);

【问题讨论】:

  • 等式运算符通常不能与 SQL 中的 null 值很好地交互。您应该酌情使用'cancelled_by IS' => NULLIS NOT 添加“OR”条件。
  • 这与 PHP 无关。在 SQL 语言中,NULL 不等于任何定义。
  • 如果我理解了这个问题,那么我就不会寻求帮助,我也不会在 cakephp3 中发布这个。
  • @jagguy 对不起,如果我冒犯了你,我唯一的目的是提供有关根本问题的信息。

标签: cakephp cakephp-3.0 nullable query-builder comparison-operators


【解决方案1】:

'Lessons.cancelled_by !=' => 'Tutor' 是正确的,但它不适用于空值。 'Lessons.cancelled_by IS NOT' => 'NULL' 将改为适用于 NULL 值。我认为您可以同时使用两者。所以结果只会是 (cancelled_by != 'Tutor' AND cancelled_by IS NOT NULL),这是您需要的结果。

【讨论】:

    【解决方案2】:

    这是此处海报的正确答案。如果那个人可以把答案放在这里。等式运算符通常不会与 SQL 中的空值很好地交互。您可能应该酌情添加一个“或”条件,其中“cancelled_by IS”=> NULL 或 IS NOT。

    【讨论】:

      【解决方案3】:

      使用 ->notEq()

      例子

      $query = $articles->find()
          ->where(function ($exp) {
              return $exp
                  ->eq('author_id', 2)
                  ->eq('published', true)
                  ->notEq('spam', true)
                  ->gt('view_count', 10);
          });
      

      在此处阅读更多信息Cakephp 3

      【讨论】:

      • 这应该如何解决NULL 值的问题? notEq() 将产生与问题中所示完全相同的比较,即它将创建一个 != 运算符。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多