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

Extension crashes, when trying to use via in place of viaTable to create many-many relation #8

Closed
pkolbas opened this issue Aug 24, 2015 · 2 comments

Comments

@pkolbas
Copy link

pkolbas commented Aug 24, 2015

No description provided.

@beowulfenator
Copy link
Contributor

Can you provide a concrete config example?

@pkolbas
Copy link
Author

pkolbas commented Aug 27, 2015

In ActiveRelationTrait.php:

public function via($relationName, callable $callable = null)
    {
        $relation = $this->primaryModel->getRelation($relationName);
        $this->via = [$relationName, $relation];
        if ($callable !== null) {
            call_user_func($callable, $relation);
        }
        return $this;
    }

and viaTable:

    public function viaTable($tableName, $link, callable $callable = null)
    {
        $relation = new ActiveQuery(get_class($this->primaryModel), [
            'from' => [$tableName],
            'link' => $link,
            'multiple' => true,
            'asArray' => true,
        ]);
        $this->via = $relation;
        if ($callable !== null) {
            call_user_func($callable, $relation);
        }

        return $this;
    }

in ManyToManyBehavior:

            // many-to-many
            if (!empty($relation->via) && $relation->multiple) {
                //Assuming junction column is visible from the primary model connection
                list($junctionTable) = array_values($relation->via->from);
                list($junctionColumn) = array_keys($relation->via->link);
                list($relatedColumn) = array_values($relation->link);

So author trying to use "via" as object, but in case of using via('relation') in place of viaTable - via as array, and application crashed.

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategories()
    {
        return $this->hasMany(Category::className(), ['id' => 'category_id'])
            ->viaTable('page_category', ['page_id' => 'id']);
    }

works good, and

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPageCategory()
    {
        return $this->hasMany(PageCategory::className(), ['page_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategories()
    {
        return $this->hasMany(Category::className(), ['id' => 'category_id'])
            ->via('pageCategory');
    }

crashes

voskobovich added a commit that referenced this issue Oct 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants