Correct way to drop an assignment

drmonkeyninja - 3 weeks ago

I'm needing to drop some assignments from a stream. What is the correct way of doing this?

Answer

ryanthompson - 3 weeks ago

I had it backwards - if you pop into the API there you can see it's actually:

public function findByStreamAndField(StreamInterface $stream, FieldInterface $field);

Yes the same example twice to demonstrate a point that it's not something special I guess. You can delete Eloquent models like public function findByStreamAndField(StreamInterface $stream, FieldInterface $field); or you can use our repository system. I like to mention to encourage people getting out of the habit of "the Pyro way" when "the Laravel" way is the same or similar and works OK too.

Take a peak at the $model->delete() as well as the one for streams and fields to get an idea of what I am talking about. Vs for example the \Anomaly\Streams\Platform\Assignment\AssignmentRepository which is just an Eloquent model.

Hope that makes sense - I could be rambling...

ryanthompson - 3 weeks ago

Using a regular migration (assuming this is what you're after - using migrations) you can do something like this:

$field = $this->fields()->findBySlugAndNamespace('foo', 'bar');
$stream = $this->streams()->findBySlugAndNamespace('foo', 'bar');

$this->assignments()->delete($this->assignments()->findByFieldAndStream($field, $stream));

Of course this is "to the T".. You can just do $field = $this->fields()->findBySlugAndNamespace('foo', 'bar'); $stream = $this->streams()->findBySlugAndNamespace('foo', 'bar'); $this->assignments()->delete($this->assignments()->findByFieldAndStream($field, $stream)); since it's Eloquent.

drmonkeyninja - 3 weeks ago

That seems to fail for me Ryan. I get the following error when I try and run the migration:-

Type error: Argument 1 passed to Anomaly\Streams\Platform\Model\EloquentRepository::delete() must be an instance of Anomaly\Streams\Platform\Model\EloquentModel, null given

drmonkeyninja - 3 weeks ago

Sorry Ryan, re-reading your response I'm a little confused as you seem to just give the same example twice, or am I missing the point?. The error I'm getting appears to be because there is no findByFieldAndStream() function in my version of Pyro. Has this recently been added?

ryanthompson - 3 weeks ago

I had it backwards - if you pop into the API there you can see it's actually:

public function findByStreamAndField(StreamInterface $stream, FieldInterface $field);

Yes the same example twice to demonstrate a point that it's not something special I guess. You can delete Eloquent models like public function findByStreamAndField(StreamInterface $stream, FieldInterface $field); or you can use our repository system. I like to mention to encourage people getting out of the habit of "the Pyro way" when "the Laravel" way is the same or similar and works OK too.

Take a peak at the $model->delete() as well as the one for streams and fields to get an idea of what I am talking about. Vs for example the \Anomaly\Streams\Platform\Assignment\AssignmentRepository which is just an Eloquent model.

Hope that makes sense - I could be rambling...

drmonkeyninja - 2 weeks ago

Thanks Ryan, that's done the trick.