简体   繁体   中英

How can I check if a leftJoin is set in?

I have a form filter with few custom search fields.

In the addXXXQuery functions, I have to use join on a table which is the same for some fields.

Can I check if innerJoin for that particular table is already set in another addXXXQuery ?

[EDIT] example:

public function addIsPaidColumnQuery(Doctrine_Query $query, $field, $values)
{
  if ($values) {
    $rootAlias = $query->getRootAlias();
    $query->leftJoin($rootAlias.".Inscription i");
          ->andWhere("i.is_paid = ?", $values);
  }
}

public function addActTypeColumnQuery(Doctrine_Query $query, $field, $values)
{
  if ($values) {
    $rootAlias = $query->getRootAlias();
    $query->leftJoin($rootAlias.".Inscription i")
          ->leftJoin("i.Act a")

    $query->addWhere("a.act_type_id = ?", $values);
  }
}

Well, you can retrieve the $params from the query, and check if the join is already there, something like this:

public function addIsPaidColumnQuery(Doctrine_Query $query, $field, $values)
{
  if ($values)
  {
    $rootAlias = $query->getRootAlias();
    $leftjoin  = $rootAlias.".Inscription i";
    $params    = $query->getParams();

    if (!isset($params['join']) || (isset($params['join']) && !in_array($leftjoin, $params)))
    {
      $query->leftJoin($leftjoin);
    }

    $query->andWhere("i.is_paid = ?", $values);
  }
}

You can see in the code, how a leftJoin in added .

I couldn't find a method, that would tell me if a join for a table is set. However the more I thought about the existence of such method, the less sence it made for it. So to check if I already used a join in the filter query I set a class variable $leftJoinInscriptionSet .

private $leftJoinInscriptionSet = false;

...

public function addIsPaidColumnQuery(Doctrine_Query $query, $field, $values)
{
  if ($values) {
    $this->setLeftJoinInscription($query);

    $query->andWhere("i.is_paid = ?", $values);
  }
}

...


private function setLeftJoinInscription(Doctrine_Query $query) {
  if (!$this->leftJoinInscriptionSet) {
      $rootAlias = $query->getRootAlias();
      $query->leftJoin($rootAlias.".Inscription i");
      $this->leftJoinInscriptionSet = true;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM