CakePHP-Proffer icon indicating copy to clipboard operation
CakePHP-Proffer copied to clipboard

Afterpath Event Question

Open rob-vd2 opened this issue 5 years ago • 3 comments

I'm using the Cake4 branch to upload multiple images. I am using the afterPath event listener to customise the upload path so that my uploads are stored at:

articles\2020-07-04-104002\file_1.jpg

In order that the path does't include the folder "files" (my upload field as per the config) I had to do the following: - is this the best approach?

$path->setField(null);

Further, Is it possible to use the slug of the article in place of the data stamp, for example:

articles\my-article-title\file_1.jpg

I have looked into the createPath function of ProfferBehaviour but can't see how I can pass the parent entity (Articles) into the event where I'd need it to construct the path.

If this requires some custom development I would be happy to get involved and create a PR but I don't want to re-invent the wheel.

Thanks!

rob-vd2 avatar Jul 04 '20 09:07 rob-vd2

To change the folder your uploads are stored in, change the root config https://github.com/davidyell/CakePHP-Proffer/blob/cake-4/docs/configuration.md#configuring-the-behaviour-in-your-table

Well the path class constructor takes an entity, so you could pass some data into the path clas instance that way if you wanted to interpolate something into the path.

davidyell avatar Jul 16 '20 10:07 davidyell

Thanks - when you mention the path class are you referring to: ProfferPath.php. If so when I debug the $entity in the constructor the entity in this case is the image itself - since the Proffer behaviour is attached to a hasMany relationship - ArticleImages. Since articles hasMany ArticleImages I need access to the Article entity here (for the article name).

rob-vd2 avatar Jul 19 '20 11:07 rob-vd2

You may need a custom path class which can lookup the association. Or perhaps contain the association so it's present in the entity?

davidyell avatar Jul 20 '20 10:07 davidyell