We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    got a query I need to figure out here:

    $criteria = $this->modx->newQuery('Tipa');
    
    $criteria->leftJoin('Entities','Entities');
    
    $criteria->leftJoin('Program','Program');
    
    $criteria->leftJoin('Location','Location', 'Program.location_id = Location.id');
    
    $criteria->where(array('Tipa.id' => $id));
    
    $criteria->where(array('Tipa.status_id' => 2,'OR:Tipa.status_id:=' => 3));
    
    $criteria->where(array(
    	'Entities.first_name:LIKE' => "%$searchQuery%",
    	'OR:Entities.cams:LIKE' => "%$searchQuery%",
    	'OR:Entities.last_name:LIKE' => "%$searchQuery%",
    	'OR:CONCAT(`Entities`.`first_name`, " ", `Entities`.`last_name`):LIKE' => 'CONCAT("%", replace("'.$searchQuery.'", " ", "%"), "%")',
    	));
    	
    $criteria->where(array('Location.id:IN' => $mygroups));
    
    $options = $this->modx->getCollectionGraph('Tipa', '{ "Entities": {}, "Program": { "ProgramName": {}, "Location": {}, }, }', $criteria);



    I ~could~ manage to write this as just an SQL string, but I was wondering if it was possible to write this part:

    'OR:CONCAT(`Entities`.`first_name`, " ", `Entities`.`last_name`):LIKE' => 'CONCAT("%", replace("'.$searchQuery.'", " ", "%"), "%")',


    as part of the criteria.

    Right now this does not work, the sql it actually generates looks like:

    SELECT `Tipa`.`id` AS `Tipa_id`, `Tipa`.`created` AS `Tipa_created`, `Tipa`.`modified` AS `Tipa_modified`, `Tipa`.`token` AS `Tipa_token`, `Tipa`.`unixtime` AS `Tipa_unixtime`, `Tipa`.`status_id` AS `Tipa_status_id`, `Tipa`.`client_id` AS `Tipa_client_id`, `Tipa`.`payee_id` AS `Tipa_payee_id`, `Tipa`.`program_id` AS `Tipa_program_id`, `Tipa`.`requester_uid` AS `Tipa_requester_uid`, `Tipa`.`approver_uid` AS `Tipa_approver_uid`, `Tipa`.`start_date` AS `Tipa_start_date`, `Tipa`.`end_date` AS `Tipa_end_date`, `Tipa`.`comment` AS `Tipa_comment` FROM `flow_tipa` AS `Tipa` LEFT JOIN `flow_entities` `Entities` ON `Tipa`.`client_id` =  `Entities`.`id` LEFT JOIN `flow_program` `Program` ON `Tipa`.`program_id` =  `Program`.`id` LEFT JOIN `flow_location` `Location` ON Program.location_id = Location.id
    
     WHERE  (  ( `Tipa`.`status_id` = ? OR `Tipa`.`status_id` = ? )  
    AND  ( `Entities`.`first_name` LIKE ? OR `Entities`.`cams` LIKE ? OR `Entities`.`last_name` LIKE ? OR `CONCAT(`Entities`.`first_name`, " ", `Entities` LIKE ? )  
    AND `Location`.`id` IN (1,2,3,5,6,7,8,10,11,12,13,15,17,18,19) ) 



    ideas?

    This question has been answered by amitpatil. See the first response.

      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 4172
      • 5,888 Posts
      you can try something like that:

      $q = '
          `Entities`.`first_name` LIKE "%' . $searchQuery . '%"
          OR `Entities`.`cams` LIKE "%' . $searchQuery . '%"
          OR `Entities`.`last_name` LIKE "%' . $searchQuery . '%"
          OR CONCAT(`Entities`.`first_name`, " ", `Entities`.`last_name`) LIKE CONCAT("%", replace("' . $searchQuery . '", " ", "%"), "%")';
      
      $criteria->where($q, xPDOQuery::SQL_AND);
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 26503
        • 620 Posts
        yes, like I said, I ~could~ do that, but was wondering if it was possible to write the conditions in the pdo format...

        Quote from: Bruno17 at Jan 12, 2015, 01:40 AM
        you can try something like that:

        $q = '
            `Entities`.`first_name` LIKE "%' . $searchQuery . '%"
            OR `Entities`.`cams` LIKE "%' . $searchQuery . '%"
            OR `Entities`.`last_name` LIKE "%' . $searchQuery . '%"
            OR CONCAT(`Entities`.`first_name`, " ", `Entities`.`last_name`) LIKE CONCAT("%", replace("' . $searchQuery . '", " ", "%"), "%")';
        
        $criteria->where($q, xPDOQuery::SQL_AND);
        
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 15083
          • 697 Posts
          Quote from: sean69 at Jan 12, 2015, 11:30 AM
          yes, like I said, I ~could~ do that, but was wondering if it was possible to write the conditions in the pdo format...

          Yes it is possible I believe.
          You could make it much safer/cleaner by using bindParam from PDO.

          $q = 'YOUR SQL HERE WITH :PDOPARAM';
          
          $query = new xPDOCriteria($modx, $q);
          $query->prepare();
          $query->stmt->bindParam(':PDOPARAM', $whatever, PDO::PARAM_STR);
          $query->stmt->execute();
          
          //run the query to get our results
          return $query->stmt->fetchAll(PDO::FETCH_ASSOC);
          
            • 36467
            • 73 Posts
            www.amitpatil.me Reply #5, 10 years ago
            Quote from: jusmeig at Jan 16, 2015, 01:59 AM

            Yes it is possible I believe.
            You could make it much safer/cleaner by using bindParam from PDO.

            I tried as you said, but in my case it didnt work sad

            $q = "SELECT fullname, email, extended FROM modx_user_attributes WHERE extended like '%:code%'";
            $query = new xPDOCriteria($modx, $q);
            $query->prepare();
            $query->stmt->bindParam(':code', $code, PDO::PARAM_STR);
            $query->stmt->execute();
            
            //run the query to get our results
            $r = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
            print_r($r);
            


            If i replace :code with $code it works, but thats not safe so i want to make it work with :code
              • 15083
              • 697 Posts
              Quote from: amitpatil at Oct 17, 2016, 06:22 AM
              Quote from: jusmeig at Jan 16, 2015, 01:59 AM

              $q = "SELECT fullname, email, extended FROM modx_user_attributes WHERE extended like :code";
              $query = new xPDOCriteria($modx, $q);
              $query->prepare();
              $query->stmt->bindParam(':code', $code, PDO::PARAM_STR);
              $query->stmt->execute();
              
              //run the query to get our results
              $r = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
              print_r($r);
              


              I think you dont add the ''
              That's what the param does, escape the string properly.

              Same usage as here:
              http://php.net/manual/en/pdostatement.bindparam.php

              • discuss.answer
                • 36467
                • 73 Posts

                @jusmeig : Thanks for the link, i found answer on same page. Here is the exact link http://php.net/manual/en/pdostatement.bindparam.php#99698

                Below is what worked

                  $code = $_POST['code'];
                  $querycode = "%".$code."%";
                  $q = "SELECT id,internalKey,email,fullname, email, extended FROM modx_user_attributes WHERE extended like :code";
                  $query = new xPDOCriteria($modx, $q);
                  $query->prepare();
                  $query->stmt->bindParam(':code', $querycode);
                  $query->stmt->execute();
                  if($query->stmt->rowCount()){
                    $user = $query->stmt->fetchAll(PDO::FETCH_ASSOC);
                  }