Note: I am working through installing an AWS PHP SDK api connection in this question, although my question is entirely about CakePHP structure. Any of the AWS terminology I am using should be pretty simple and is only there to demonstrate the issue with Cake I am running into, if your not familiar with AWS.
I am currently writing some new tables / entities for a CakePHP application I maintain, called Files. They represent images stored on a bucket in Amazon S3. Files contains a value called 'Key' which is more or less the filename of the file represented by the my entity in Cake, matching the filename on S3. The entity looks like this:
<?php
class File extends Cake\ORM\Entity
{
protected $_accessible = [
'file_name' => true,
'key' => true,
'unique_id' => true,
'email_task_message_id' => true,
'email_task_message' => true,
];
}
I am integrating the AWS PHP SDK which allows me to exchange the name of the bucket and filename (called key above, to match AWS terminology) for a pre-signed url pointing the the resources on S3. This pre-signed url needs to be displayed on ever File entity.
What I am wondering is where should I integrate the AWS PHP SDK call into the Cake model structure?
I would like to do this as a Cake Virtual Field on the File Entity (above). But there can literally be hundreds of files included on a single query, so that would cause the virtual field to re-connect to the API for each of those to the best of my knowledge. So I would need some way of injecting an already instantiated version of the variable containing AWS PHP SDK connection into the entity on generation.
Or would it make more sense to modify this somehow though the initialize call on the table / behavior representing the File entities, FilesTable?
Thanks in advance!