Issue
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!
Solution
I would not perform an active lookup in a virtual field. I think it's more conventional to only use it for simple things, with data already present.
I think a model (or table-like object) is a more suitable location for code like this.
You might find this question useful for this point: Cakephp 3 - How to integrate external sources in table?
You can make your own finders (neatly tucked inside Behaviors to group your logic) that combine your AWS data with your database data in this order:
- Collect database data, produce an array of lookup keys
- Use the array of keys to perform a single remote lookup
- Combine the remote data with database data.
Answered By - Roberto Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.