CakePHP多个匹配()条件与OR

时间:2015-05-19 15:00:56

标签: mysql matching cakephp-3.0

我尝试使用matching()函数在多个关联表中进行搜索。 我似乎无法在此搜索查询中包含“OR”,以下代码似乎正在运行,但似乎只有在所有关联表中存在LIKE%search%时才匹配。

非常感谢帮助。

if($this->request->is(['post','put']))
{
    $search = $this->request->data['search'];

    /* Conditions for this table query */
    $this->listConditions['Purchases.created LIKE'] = '%$'.$search.'%';

    $contain = ['Customers','Users','Professions.ProfessionsLanguages','Cities'];

    /* Conditions for associated table queries */
    $customerConditions = [
        'Customers.email LIKE' => '%'.$search.'%',
        'Customers.city LIKE' => '%'.$search.'%',
        'Customers.company_name LIKE' => '%'.$search.'%',
        'Customers.organization_number LIKE' => '%'.$search.'%',
    ];
    $userConditions = [
        'Users.email LIKE' => '%'.$search.'%',
        'Users.first_name LIKE' => '%'.$search.'%',
        'Users.last_name LIKE' => '%'.$search.'%',
    ];
    $profProflanguagesConditions = ['ProfessionsLanguages.name LIKE' => '%'.$search.'%'];
    $citiesConditions = ['Cities.name LIKE' => '%'.$search.'%'];

    /* Perform queries */
    $query = $this->Purchases->find('all')->where($this->listConditions)->contain($contain);

    $query->matching('Customers', function ($q) use ($customerConditions) {
        return $q->where($customerConditions);
    });

    $query->matching('Users', function ($q) use ($userConditions) {
        return $q->where($userConditions);
    });

    $query->matching('Professions.ProfessionsLanguages', function ($q) use ($profProflanguagesConditions) {
        return $q->where($profProflanguagesConditions);
    });

    $query->matching('Cities', function ($q) use ($citiesConditions) {
        return $q->where($citiesConditions);
    });

    debug($query->toArray());
    die();
}

我尝试过匹配这样的条件:

        $customerConditions = [
            'OR' => [
                ['Customers.email LIKE' => '%'.$search.'%'],
                ['Customers.city LIKE' => '%'.$search.'%'],
                ['Customers.company_name LIKE' => '%'.$search.'%'],
                ['Customers.organization_number LIKE' => '%'.$search.'%'],
            ]
        ];
        $userConditions = [
            'OR' => [
                ['Users.email LIKE' => '%'.$search.'%'],
                ['Users.first_name LIKE' => '%'.$search.'%'],
                ['Users.last_name LIKE' => '%'.$search.'%'],
            ]
        ];

但仅适用于第一次匹配()调用。

这是匹配条件中带有'OR'的查询。

SELECT
   Purchases.id AS `Purchases__id`,
   Purchases.customer_id AS `Purchases__customer_id`,
   Purchases.user_id AS `Purchases__user_id`,
   Purchases.invoice_id AS `Purchases__invoice_id`,
   Purchases.status AS `Purchases__status`,
   Purchases.created AS `Purchases__created`,
   Purchases.quantity AS `Purchases__quantity`,
   Purchases.quantity_max AS `Purchases__quantity_max`,
   Customers.id AS `Customers__id`,
   Customers.organization_number AS `Customers__organization_number`,
   Customers.company_name AS `Customers__company_name`,
   Customers.address AS `Customers__address`,
   Customers.address2 AS `Customers__address2`,
   Customers.zipcode AS `Customers__zipcode`,
   Customers.city AS `Customers__city`,
   Customers.email AS `Customers__email`,
   Customers.created AS `Customers__created`,
   Customers.removed AS `Customers__removed`,
   Users.id AS `Users__id`,
   Users.customer_id AS `Users__customer_id`,
   Users.type AS `Users__type`,
   Users.email AS `Users__email`,
   Users.password AS `Users__password`,
   Users.salt AS `Users__salt`,
   Users.first_name AS `Users__first_name`,
   Users.last_name AS `Users__last_name`,
   Users.invoice_text AS `Users__invoice_text`,
   Users.created AS `Users__created`,
   Users.removed AS `Users__removed`,
   Professions.id AS `Professions__id`,
   Professions.parent_id AS `Professions__parent_id`,
   Professions.lft AS `Professions__lft`,
   Professions.rght AS `Professions__rght`,
   ProfessionsPurchases.id AS `ProfessionsPurchases__id`,
   ProfessionsPurchases.profession_id AS `ProfessionsPurchases__profession_id`,
   ProfessionsPurchases.purchase_id AS `ProfessionsPurchases__purchase_id`,
   ProfessionsLanguages.id AS `ProfessionsLanguages__id`,
   ProfessionsLanguages.profession_id AS `ProfessionsLanguages__profession_id`,
   ProfessionsLanguages.language_id AS `ProfessionsLanguages__language_id`,
   ProfessionsLanguages.name AS `ProfessionsLanguages__name`,
   Cities.id AS `Cities__id`,
   Cities.state_id AS `Cities__state_id`,
   Cities.name AS `Cities__name`,
   Cities.active AS `Cities__active`,
   Cities.applicant_count AS `Cities__applicant_count`,
   CitiesPurchases.id AS `CitiesPurchases__id`,
   CitiesPurchases.city_id AS `CitiesPurchases__city_id`,
   CitiesPurchases.purchase_id AS `CitiesPurchases__purchase_id` 
FROM
   purchases Purchases 
INNER JOIN
   customers Customers 
      ON (
         (
            Customers.email like :c0 
            OR Customers.city like :c1 
            OR Customers.company_name like :c2 
            OR Customers.organization_number like :c3
         ) 
         AND Customers.id = (
            Purchases.customer_id
         )
      ) 
INNER JOIN
   users Users 
      ON (
         (
            Users.email like :c4 
            OR Users.first_name like :c5 
            OR Users.last_name like :c6
         ) 
         AND Users.id = (
            Purchases.user_id
         )
      ) 
INNER JOIN
   professions Professions 
      ON 1 = 1 
INNER JOIN
   professions_purchases ProfessionsPurchases 
      ON (
         Purchases.id = (
            ProfessionsPurchases.purchase_id
         ) 
         AND Professions.id = (
            ProfessionsPurchases.profession_id
         )
      ) 
INNER JOIN
   professions_languages ProfessionsLanguages 
      ON (
         ProfessionsLanguages.language_id = :c7 
         AND ProfessionsLanguages.name like :c8 
         AND Professions.id = (
            ProfessionsLanguages.profession_id
         )
      ) 
INNER JOIN
   cities Cities 
      ON Cities.name like :c9 
INNER JOIN
   cities_purchases CitiesPurchases 
      ON (
         Purchases.id = (
            CitiesPurchases.purchase_id
         ) 
         AND Cities.id = (
            CitiesPurchases.city_id
         )
      ) 
WHERE
   (
      (
         Purchases.user_id
      ) IS NOT NULL 
      AND (
         Purchases.customer_id
      ) IS NOT NULL
   )

0 个答案:

没有答案