Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propel / Filtering w. many to many #234

Open
lionelbzv opened this issue Jan 19, 2016 · 3 comments
Open

Propel / Filtering w. many to many #234

lionelbzv opened this issue Jan 19, 2016 · 3 comments

Comments

@lionelbzv
Copy link
Contributor

I've just done some tests to filter list results with objects related in many to many relations.

I've tried to fix this issue for Propel, but that's not finished so I'm just putting here some works to remind me later and to have feedbacks.

Fix config type:

    filter_types:
        propel:
            collection:    model

Fix PropelQueryFilter:

    public function addCollectionFilter($field, $value)
    {
        list($query, $filteredField) = $this->addTablePathToField($field);

        if ($value instanceof \PropelObjectCollection) {
            $value = $value->toArray();
        }

        if (!is_array($value)) {
            $value = array($value->getId());
        }

        if (strstr($field, '.')) {
            list($table, $field) = explode('.', $field);
        } else {
            $table = $field;
            $field = 'Id';
        }

        $query = call_user_func_array(array($query, 'use'.$table.'Query'), array());
        $query->filterBy($field, $value, \Criteria::IN)
            ->endUse()
            ->groupById();
    }

Finally, I've got an error in "counting result"

Stack trace:

An exception has been thrown during the rendering of a template ("Unable to execute COUNT statement [SELECT COUNT(*) FROM (SELECT foo.id, foo.title, foo.created_at, foo.updated_at, foo.slug FROM `foo` INNER JOIN `bar` ON (foo.id=bar.foo_id) WHERE bar.id IN (:p1,:p2) GROUP BY foo.id) propelmatch4cnt] [wrapped: Notice: Array to string conversion]") in Admingenerated/PolitizrAdminBundle/Resources/views/FooList/results.html.twig at line 6.

This SQL request throws the error:
every derivated table must have its own alias

@sescandell
Copy link
Member

👍 so good to read some Propel users :)

I think issue is here. It should be

$this->joins[$joinAlias] = call_user_func_array(array($parentQuery, 'use'.$field.'Query'), array($joinAlias, \Criteria::INNER_JOIN));

(note the $joinAlias at the end instead of $field).

According to this documentation

Let me know if it works,

Thank you @Lionel09

@lionelbzv
Copy link
Contributor Author

Thanks for your feedback!

I revert my change in PropelQueryFilter, but it seems that if I want the part of code you pointed to be executed, I have to put a field with a dot, but I don't find the right expression to set in the yml file.

Example with an object Document with "n:m" relation to objects Tag. The n:m table is DocumentTag.
In my Propel model, Document has a method called getTags to get the associated Tag objects.

If I try this config in the yml of Document:

            fields:
                Document.Tags:
                    dbType:                     model
                    formType:                   model
                    label:                      Tags
                    addFormOptions:
                        multiple:               true
                        required:               false
                        class:                  MyProject\Model\Tag
                        query:                  \MyProject\Model\TagQuery::create()->filterByOnline(1)->orderByTitle()

I've got an error:

An exception has been thrown during the rendering of a template ("Field "Document" for class "MyProject\Model\Document" is not an association.") in "../CommonAdmin/ListAction/filters.php.twig".

@sescandell
Copy link
Member

Hi @Lionel09

My mistake, I didn't carefully read your post...

I came back on you "corrected" version of addCollectionFilter. I think I see two issues in your corrected version:

  1. You don't provide any alias neither "type" of Join in your call_user_func call. I'd probably try something like $query = call_user_func_array(array($query, 'use'.$table.'Query'), array($field . '_table_filter_join', \Criteria::INNER_JOIN));
  2. You don't keep the joinAlias property up to date

But the thing is: I actually don't know what really is the initial issue. Do you have any additional informations to provide us? Do you have any error? In the console or in a page?

With all of this, we should find the right way to handle the collection filter.

Thanks for your help

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

No branches or pull requests

2 participants