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

Closed
fonsekaean opened this Issue Oct 4, 2013 · 3 comments

Comments

Projects
None yet
4 participants
@fonsekaean
Contributor

fonsekaean commented Oct 4, 2013

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 Mar 15, 2014

@Makreig

This comment has been minimized.

Show comment Hide comment
@Makreig

Makreig Oct 13, 2015

Contributor

I think that i'm getting this same issue. I'm filtering an array list then filtering it again by the same association (products filtered by 2 categories). However i can't get the above fix to work.

Contributor

Makreig commented Oct 13, 2015

I think that i'm getting this same issue. I'm filtering an array list then filtering it again by the same association (products filtered by 2 categories). However i can't get the above fix to work.

@kinglozzer

This comment has been minimized.

Show comment Hide comment
@kinglozzer

kinglozzer Oct 13, 2015

Member

I believe this was fixed in #3956. See #3954 also.

@Makreig ArrayList is completely separate to DataList/DataQuery, so I don’t think your issue is likely to be related to this one. If you’re certain you’ve found a bug, create a new issue for it with steps to reproduce please :)

Member

kinglozzer commented Oct 13, 2015

I believe this was fixed in #3956. See #3954 also.

@Makreig ArrayList is completely separate to DataList/DataQuery, so I don’t think your issue is likely to be related to this one. If you’re certain you’ve found a bug, create a new issue for it with steps to reproduce please :)

@kinglozzer kinglozzer closed this Oct 13, 2015

@Makreig

This comment has been minimized.

Show comment Hide comment
@Makreig

Makreig Oct 13, 2015

Contributor

sorry i got the wrong word, i meant DataList.

Contributor

Makreig commented Oct 13, 2015

sorry i got the wrong word, i meant DataList.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment