We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42049 ☆ A M B ☆
    • 250 Posts
    I'm making a fancy jquery autocomplete call, and trying to use xpdo to get my results to show. I'm effectively posting a search term, and a search by, which maps to the correct scheme bits and bobs.

    I know that the code below works, however, the moment I introduce any join (which ive commented out below), it all ceases to work, and I get no responses returned.

    Any help greatly appreciated!

    $search = array(
    	className	=> 'MyClass',
    	packageName	=> 'myClass',
    	by		=> $_GET['by'], //examples are name, County.name, Country.name, and Offer.name
    	term 		=> $_GET['term'], //$_GET search var
    );
    
    //Build search criteria
    $criteria = array(
    	$search['by'].':LIKE' => '%'. $search['term'] .'%'
    );
    
    //Build query
    $q = $modx->newQuery($search['className']);
    //$q->innerJoin('Offer', 'Offer', $search['className'].'.offerId=Offer.id');
    //$q->innerJoin('County', 'County', $search['className'].'.countyId=County.id');
    //$q->innerJoin('Country', 'Country', $search['className'].'.countryId=Country.id');
    $q->where($criteria);
    $q->sortby('name','ASC');
    
    $o = $modx->getCollection($search['className'], $q);
    
    $results = array();
    
    foreach($o as $result) {
    	$i = array (
    		label			=> $result->get($search['by']),
    		value			=> $result->get($search['by'])
    	);
    	array_push($results, $i);
    }
    
    echo json_encode($results);
      GEL STUDIOS
      MODX Professional | MODX Ambassador

      Website | Email | Twitter | Facebook
      • 4172
      • 5,888 Posts
      what's the generated sql with and without your joins?
      may be you will need to add some $q->select(.........);

      $q->sortby('name','ASC');
      $q->prepare();echo $q->toSql();
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 42049 ☆ A M B ☆
        • 250 Posts
        Without joins...

        SELECT `OfferOne`.`id` AS `OfferOne_id`, `OfferOne`.`name` AS `OfferOne_name`, `OfferOne`.`addressLineOne` AS `OfferOne_addressLineOne`, `OfferOne`.`addressLineTwo` AS `OfferOne_addressLineTwo`, `OfferOne`.`addressLineThree` AS `OfferOne_addressLineThree`, `OfferOne`.`countyId` AS `OfferOne_countyId`, `OfferOne`.`postCode` AS `OfferOne_postCode`, `OfferOne`.`countryId` AS `OfferOne_countryId`, `OfferOne`.`url` AS `OfferOne_url`, `OfferOne`.`telephoneNumber` AS `OfferOne_telephoneNumber`, `OfferOne`.`offerId` AS `OfferOne_offerId`, `OfferOne`.`monday` AS `OfferOne_monday`, `OfferOne`.`tuesday` AS `OfferOne_tuesday`, `OfferOne`.`wednesday` AS `OfferOne_wednesday`, `OfferOne`.`thursday` AS `OfferOne_thursday`, `OfferOne`.`friday` AS `OfferOne_friday`, `OfferOne`.`saturday` AS `OfferOne_saturday`, `OfferOne`.`sunday` AS `OfferOne_sunday`, `OfferOne`.`availability` AS `OfferOne_availability`, `OfferOne`.`exclusions` AS `OfferOne_exclusions`, `OfferOne`.`photo` AS `OfferOne_photo`, `OfferOne`.`published` AS `OfferOne_published`, `OfferOne`.`deleted` AS `OfferOne_deleted` FROM `modx_offerOne` AS `OfferOne` WHERE `OfferOne`.`name` LIKE '%test%' ORDER BY name ASC


        With joins

        SELECT `OfferOne`.`id` AS `OfferOne_id`, `OfferOne`.`name` AS `OfferOne_name`, `OfferOne`.`addressLineOne` AS `OfferOne_addressLineOne`, `OfferOne`.`addressLineTwo` AS `OfferOne_addressLineTwo`, `OfferOne`.`addressLineThree` AS `OfferOne_addressLineThree`, `OfferOne`.`countyId` AS `OfferOne_countyId`, `OfferOne`.`postCode` AS `OfferOne_postCode`, `OfferOne`.`countryId` AS `OfferOne_countryId`, `OfferOne`.`url` AS `OfferOne_url`, `OfferOne`.`telephoneNumber` AS `OfferOne_telephoneNumber`, `OfferOne`.`offerId` AS `OfferOne_offerId`, `OfferOne`.`monday` AS `OfferOne_monday`, `OfferOne`.`tuesday` AS `OfferOne_tuesday`, `OfferOne`.`wednesday` AS `OfferOne_wednesday`, `OfferOne`.`thursday` AS `OfferOne_thursday`, `OfferOne`.`friday` AS `OfferOne_friday`, `OfferOne`.`saturday` AS `OfferOne_saturday`, `OfferOne`.`sunday` AS `OfferOne_sunday`, `OfferOne`.`availability` AS `OfferOne_availability`, `OfferOne`.`exclusions` AS `OfferOne_exclusions`, `OfferOne`.`photo` AS `OfferOne_photo`, `OfferOne`.`published` AS `OfferOne_published`, `OfferOne`.`deleted` AS `OfferOne_deleted` FROM `modx_offerOne` AS `OfferOne` JOIN `modx_offers` `Offer` ON OfferOne.offerId=Offer.id JOIN `modx_counties` `County` ON OfferOne.countyId=County.id JOIN `modx_countries` `Country` ON OfferOne.countryId=Country.id WHERE `OfferOne`.`name` LIKE '%test%' ORDER BY name ASC


        Putting in the below does help, but, it returns nulls for the values...

        $q->select($modx->getSelectColumns($search['className'], $search['className']));


        I thought I should of been able to do the below, but it doesnt work :S

        $result->get('County.name')
        [ed. note: gelstudios last edited this post 13 years, 5 months ago.]
          GEL STUDIOS
          MODX Professional | MODX Ambassador

          Website | Email | Twitter | Facebook
          • 42049 ☆ A M B ☆
          • 250 Posts
          You pointed me on the right track, thanks Bruno

          after the joins, had to do the following, the third param allows me to specify the alias, which is then used in my result->get

          $q->select($modx->getSelectColumns('County', 'County', 'County.'));
          $q->select($modx->getSelectColumns('Country', 'Country', 'Country.'));
            GEL STUDIOS
            MODX Professional | MODX Ambassador

            Website | Email | Twitter | Facebook
            • 42602
            • 81 Posts
            [Too late, had lunch when writing this X) ]
            In theory, you should not need those criteria strings for the joins if you have set the composite/aggregate relations with the schema for OfferOne and you have "build" the schema to maps and classes. The maps should make sure that the joins are done with the relation keys already.

            Also as you are using innerjoin, if any of the joins would return 'null', you wont get any results out for that row. Try using mysql command line or phpmyadmin to check if the query with joins really work. If you want to have those nulls, use leftJoin instead of innerJoin.
              • 42049 ☆ A M B ☆
              • 250 Posts
              Quote from: dunnock at Apr 23, 2013, 11:09 AM
              [Too late, had lunch when writing this X) ]
              In theory, you should not need those criteria strings for the joins if you have set the composite/aggregate relations with the schema for OfferOne and you have "build" the schema to maps and classes. The maps should make sure that the joins are done with the relation keys already.

              Also as you are using innerjoin, if any of the joins would return 'null', you wont get any results out for that row. Try using mysql command line or phpmyadmin to check if the query with joins really work. If you want to have those nulls, use leftJoin instead of innerJoin.

              Cheers for the extra info - will have a think and adjust my query to suit.

              Good lunch?
                GEL STUDIOS
                MODX Professional | MODX Ambassador

                Website | Email | Twitter | Facebook