Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Filters break when having two relations to the same class #2491

Open
fonsekaean opened this Issue · 0 comments

2 participants

@fonsekaean

if there is an object like this

class Order extends DataObject {


    private static $has_one = array(
            "Purchaser"   => "Member",
            "Staff"           => "Member"
    );

    private static $searchable_fields = array(
            "Purchaser.Name"
    );

}

when a module admin search is done by the purchaser name it looks for Orders which have Staff.Name as the purchaser,

this happen because the DataObject->getReverseAssociation function does an array flip to and return the get the foreign keys.

I could fix by modifying these functions

  1. DataObject->getReverseAssociation
public function getReverseAssociation($className, $relation = "") {
        if (is_array($this->many_many())) {
            $many_many = array_flip($this->many_many());
            if(!empty($relation)){
                if(array_key_exists($relation, $this->many_many())){
                    $many_many = array(
                        $className => $relation
                    );
                }
            }
            if (array_key_exists($className, $many_many)) return $many_many[$className];
        }
        if (is_array($this->has_many())) {
            $has_many = array_flip($this->has_many());
            if(!empty($relation)){
                if(array_key_exists($relation, $this->has_many())){
                    $has_many = array(
                        $className => $relation
                    );
                }
            }
            if (array_key_exists($className, $has_many)) return $has_many[$className];
        }
        if (is_array($this->has_one())) {
            $has_one = array_flip($this->has_one());
            if(!empty($relation)){
                if(array_key_exists($relation, $this->has_one())){
                    $has_one = array(
                        $className => $relation
                    );
                }
            }

            if (array_key_exists($className, $has_one)) return $has_one[$className];
        }

        return false;
    }
  1. DataQuery->applyRelation

lined 589 of DataQuery.php

$foreignKey = $model->getReverseAssociation($component, $rel);

@simonwelsh simonwelsh added the 3.1 label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.