Correct way to drop an assignment
Created 6 years ago by drmonkeyninja

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

ryanthompson  —  6 years ago Best Answer

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 $model->delete() 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 \Anomaly\Streams\Platform\Assignment\AssignmentRepository as well as the one for streams and fields to get an idea of what I am talking about. Vs for example the AssignmentModel which is just an Eloquent model.

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

ryanthompson  —  6 years 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 $this->assignments()->findByFieldAndStream($field, $stream)->delete(); since it's Eloquent.

drmonkeyninja  —  6 years 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  —  6 years 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  —  6 years ago Best Answer

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 $model->delete() 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 \Anomaly\Streams\Platform\Assignment\AssignmentRepository as well as the one for streams and fields to get an idea of what I am talking about. Vs for example the AssignmentModel which is just an Eloquent model.

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

drmonkeyninja  —  6 years ago

Thanks Ryan, that's done the trick.