Laravel-json-api: Extending relation classes

Created on 4 May 2018  路  9Comments  路  Source: cloudcreativity/laravel-json-api

I'm currently trying to extend the CloudCreativity\LaravelJsonApi\Eloquent\HasMany class because I want to add some functionality to the update-method.

My problem is that the used methods findRelated, sync and getWritableRelation are scoped private and so they can't be called from a derived class.

Is there a special reason you used private instead of protected as visibility for your methods?

enhancement

Most helpful comment

@flofloflo I've pushed this to develop. I'm not sure when I'm going to do the next tag, but you can use it in the meantime by doing composer require cloudcreativity/laravel-json-api:dev-develop.

I'll close this issue but feel free to re-open if you encounter any problems.

All 9 comments

Hi! The main use for private is just to keep the API surface as low as possible. I'm happy to make them protected if they definitely need to be overridden.

It would be good to know what you're trying to do because if it is a common use case I'd prefer to add in support for your use rather than making them protected. Can you explain what you're trying to do? If it isn't something that I'd put into the package by default then I'll be happy to make the methods protected.

I'm trying to add some stuff to audit the changes to BelongsToMany relationships.
So basically this is my overwritten update method:

public function update($record, RelationshipInterface $relationship, EncodingParametersInterface $parameters)
    {
        $related = $this->findRelated($record, $relationship);
        $relation = $this->getWritableRelation($record);

        if ($relation instanceof Relations\BelongsToMany) {
            // capture old state of the relationship
            $oldValues = $relation->get();
            // sync the changes
            $relation->sync($related);
            // capture new state of the n:m relationship
            $newValues = $relation->get();
            // if the model is auditable...
            if (method_exists($record, 'saveSyncAudit')) {
                // ... save the changes in the audit table
                $record->saveSyncAudit($this->field, $relation, $oldValues, $newValues);
            }
            return;
        } else {
            $this->sync($relation, $record->{$this->key}, $related);
        }
    }

The problem is that I can't call the private methods from my update method (unless I copy them into my class -> ugly).

Ah, ok yes can see this would be useful. I'm working on this package at some point over the weekend, so I'll make the methods protected then.

Very cool, thank you!

@flofloflo unfortunately the other things I was working on over the weekend took longer than I thought so I didn't get to this. I haven't forgotten and it's the next thing on my to-do list!

No problem!
I could also send a pull request for this, if it helps you.

Would normally say a PR would be good, but there's a todo in that class about splitting out the has-many-through into its own relation, so I'm going to do that at the same time as that might affect some of the current private methods on the class.

@flofloflo I've pushed this to develop. I'm not sure when I'm going to do the next tag, but you can use it in the meantime by doing composer require cloudcreativity/laravel-json-api:dev-develop.

I'll close this issue but feel free to re-open if you encounter any problems.

Awesome, how hard you're going at it this past time. Can't wait for the finished 1.0.0 version! Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tekord picture tekord  路  3Comments

GregPeden picture GregPeden  路  4Comments

featurecode picture featurecode  路  3Comments

lindyhopchris picture lindyhopchris  路  6Comments

JeanLucEsser picture JeanLucEsser  路  6Comments