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

how to define relations #17

Open
jelmerbou opened this issue Apr 4, 2018 · 2 comments
Open

how to define relations #17

jelmerbou opened this issue Apr 4, 2018 · 2 comments

Comments

@jelmerbou
Copy link

jelmerbou commented Apr 4, 2018

I have one product model with versioned data and I got a category model that has one or many products. When i want to delete a category it has to check if there are products related to the category. How to define the relation that it goes to the products_version table to check if the category_id exists here before deleting it?

@jelmerbou
Copy link
Author

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.category_id' in 'where clause' (SQL: select categories.*, (select count(*) from productsinner joinproducts_versiononproducts.id=products_version.ref_idandproducts_version.version=products.latest_versionwherecategories.id=products.category_idandproducts_version.deleted_atis null) asproducts_countfromcategorieswhereid = 1 limit 1)

@crashkonijn
Copy link
Collaborator

Hi there,

It does indeed appear that certain relations don't work. Fortunately I did just get hasMany relation working locally. I'm going to fix all relations, and put them under tests before committing them here.

For now you can use this hot fix on the model that has the hasMany relation, and replace it with hasManyVersioned.

trait HasVersionedRelationshipsTrait
{
    public function hasManyVersioned($related, $foreignKey = null, $localKey = null)
    {
        $instance = $this->newRelatedInstance($related);

        $foreignKey = $foreignKey ?: $this->getForeignKey();

        $localKey = $localKey ?: $this->getKeyName();

        return $this->newHasMany(
            $instance->newQuery(), $this, $instance->getVersionTable().'.'.$foreignKey, $localKey
        );
    }
}

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

2 participants