Search code examples
mysqlsymfonysymfony-1.4doctrine-1.2

How can I make the query work with doctrine1 using symfony 1.4?


I am trying to run the following query using Doctrine 1 with symfony 1.4 but it says Couldn't find class (SELECT with 500 Internal Error. Is there any way I can make it work with Doctrine:

SELECT location_id FROM 
(SELECT DISTINCT location_id FROM location_feature_restriction WHERE feature_restriction_id IN(4,15,16)) as t1 
WHERE location_id 
NOT IN(SELECT DISTINCT location_id FROM location_feature_restriction WHERE feature_restriction_id = 5);

Currently I have it like this but it's not working:

/**
 * Search for venues with selected features
 * @param Doctrine_Query $qry     The SQL query
 * @param array          $values  The form values
 */
protected function addSearchFeatures(Doctrine_Query $qry, array $values = array()) {
  // "Dry" and "Bespoke Hire" options
  $qryParams = array();
  $qryParts = array();

  if (in_array("bespoke_hire", $values["features"]) && $values["features"][array_search("bespoke_hire", $values["features"])]) {
    $qryParts[] = "q.bespoke_hire = :BESPOKE_HIRE";
    $qryParams["BESPOKE_HIRE"] = 1;
    unset($values["features"][array_search("bespoke_hire", $values["features"])]);
  }
  if (array_key_exists("dry_hire", $values["features"]) && $values["features"][array_search("dry_hire", $values["features"])]) {
    $qryParts[] = "q.dry_hire = :DRY_HIRE";
    $qryParams["DRY_HIRE"] = 1;
    $values["features"][array_search("dry_hire", $values["features"])];
  }
  if (count($qryParts)) {
    $qry->andWhere(implode(" OR ", $qryParts), $qryParams);
  }

  // Search for selected features
  if (count($values["features"])) {
    // If anyone need to features should be searched using the OR condition then comment having condtion from following line.
    if ($values["no_noise_restrictions"]) {
      $subSql = "SELECT location_id FROM 
                (SELECT DISTINCT location_id FROM LocationFeatureRestriction WHERE feature_restriction_id IN(".implode(',', $values["features"]).")) as t1 
                WHERE location_id 
                NOT IN(SELECT DISTINCT location_id FROM LocationFeatureRestriction WHERE feature_restriction_id = 5)";
    } else {
      $subSql = "SELECT LocationFeatureRestriction.location_id 
         FROM LocationFeatureRestriction
         WHERE feature_restriction_id IN(".implode(',', $values["features"]).")
         GROUP BY location_id
         HAVING COUNT(DISTINCT feature_restriction_id) = ".count($values['features']);
    }

    $qry->andWhere('q.id IN ('.$subSql.')');
  }
}

Doctrine Error: enter image description here UPDATE:

I have simplified the query to the following but then it was giving error Couldn't get short alias for:

SELECT DISTINCT location_id FROM location_feature_restriction 
WHERE location_id NOT IN 
(SELECT DISTINCT location_id FROM location_feature_restriction 
WHERE feature_restriction_id = 5);

Then went ahead and added alias for the table but now it's giving error Couldn't find class featureTable:

SELECT DISTINCT location_id FROM location_feature_restriction 
WHERE location_id NOT IN 
(SELECT DISTINCT location_id FROM location_feature_restriction AS featureTable
WHERE featureTable.feature_restriction_id = 5);

Solution

  • The real credit goes to @Kris Peeling because he got my attention to this idea!

    Seems like I just needed to get the array of ID's so I used Doctrine_Manager (PDO) to do the job.

    Here is what I did to get the job done:

    if (isset($arrValues['features']) && count($arrValues['features']) > 0) {
      $this->addSearchFeatures($qry, $arrValues);
    } else {
      if ($arrValues['no_noise_restrictions']) {
        $subQuery = "SELECT DISTINCT LocationFeatureRestriction.location_id FROM LocationFeatureRestriction WHERE feature_restriction_id = 5";
        $qry->andWhere('q.id NOT IN ('.$subQuery.')');
      }
    }
    
    /**
     * Search for venues with selected features
     * @param Doctrine_Query $qry     The SQL query
     * @param array          $values  The form values
     */
    protected function addSearchFeatures(Doctrine_Query $qry, array $values = array()) {
      // "Dry" and "Bespoke Hire" options
      $qryParams = array();
      $qryParts = array();
    
      if (in_array("bespoke_hire", $values["features"]) && $values["features"][array_search("bespoke_hire", $values["features"])]) {
        $qryParts[] = "q.bespoke_hire = :BESPOKE_HIRE";
        $qryParams["BESPOKE_HIRE"] = 1;
        unset($values["features"][array_search("bespoke_hire", $values["features"])]);
      }
      if (array_key_exists("dry_hire", $values["features"]) && $values["features"][array_search("dry_hire", $values["features"])]) {
        $qryParts[] = "q.dry_hire = :DRY_HIRE";
        $qryParams["DRY_HIRE"] = 1;
        $values["features"][array_search("dry_hire", $values["features"])];
      }
      if (count($qryParts)) {
        $qry->andWhere(implode(" OR ", $qryParts), $qryParams);
      }
    
      // Search for selected features
      if (count($values["features"])) {
        // If anyone need to features should be searched using the OR condition then comment having condtion from following line.
    
        // When No Noise Restrictions is checked in Licensing Category
        if ($values['no_noise_restrictions']) {
          $subSql = "SELECT DISTINCT location_id FROM location_feature_restriction WHERE feature_restriction_id IN(".implode(',', $values["features"]).") AND location_id NOT IN (SELECT DISTINCT location_id FROM location_feature_restriction WHERE feature_restriction_id = 5)";
    
          $connection = Doctrine_Manager::connection();
          $statement = $connection->execute($subSql);
          $statement->execute();
          $resultset = $statement->fetchAll(PDO::FETCH_ASSOC);
          $location_ids = array();
          foreach ($resultset as $each) {
            $location_ids[] = $each['location_id'];
          }
    
          if (count($location_ids)) {
            $qry->andWhere('q.id IN ('.implode(',', $location_ids).')');
          } else {
            $qry->andWhere('q.id IN (0)');
          }
        } else {
          // Otherwise check for the normal features
          $subSql = "SELECT LocationFeatureRestriction.location_id 
                     FROM LocationFeatureRestriction
                     WHERE feature_restriction_id IN(".implode(',', $values["features"]).")
                     GROUP BY location_id
                     HAVING COUNT(DISTINCT feature_restriction_id) = ".count($values['features']);
          $qry->andWhere('q.id IN ('.$subSql.')');
        }
    
      }
    }