0

I am creating a web app that will:

  1. Take parameters in a form submission
  2. Run an action which will query an api to retrieve report data based on those parameters
  3. Save that report as a file along with user data (user name, id, member path, and time of submission) as an entry to a database
  4. List those entries (along with links to download the files)

I have an idea about how to approach this but I wanted to check here to see if I'm on or off base before getting too deep into it. As of right now I have the first two items working well enough for testing: I can query the API and get the user data I need. It's the CRUDL best practices that I'm interested in learning for this question.

My thinking is that I would save this data as a channel entry and then list out the entries using the standard channel template tags. In other words, when someone makes a form submission it will validate and save that data and file as a channel entry.

What's the best way of going about this? Should I create a channel in the control panel Channel Manger and a corresponding directory in the File Manger and use the db helper to insert, edit, and delete those items in my action method?

1 Answer 1

2

The best way would be to use the Model service to create Channel Entries and files:

https://docs.expressionengine.com/latest/development/services/model.html

This will guarantee you are saving complete records, which you can't guarantee if you are just blindly inserting data in the database. This also means you are triggering EE's extensions system, so other add-ons can work with yours, such as EE building search indexes off of your entries.

The Model service functions as the modern API for interacting with EE's models. The documentation is a little light, but I think there is enough there to get started. You can just create an entry like this and dump it's structure to so you understand how to work with it:

$entry = ee('Model')->make('ChannelEntry');
var_dump($entry);
exit();

Once you know the structure a little bit, you just add your info and save:

// I DON'T KNOW the structure of a file Model, this is just a guess.
// Just make one and var_dump it to figure out what properties you need to
// set to save a file properly.
$file = ee('Model')->make('File');
$file->title = "File";
$file->name = "file.jpg";
$file->upload_path = "1";
$file->save();

$entry = ee('Model')->make('ChannelEntry');
$entry->title = "My title";
$entry->url_title = "my_title";
$entry->my_file_field = $file->id;
$entry->channel_id = "1";

// Save 
$entry->save();
unset($entry);
unset($file);

The old way would be to use the legacy API, but of course I wouldn't recommend it as it will probably go away in the next major version. But if you want to use it, it does work just fine:

https://docs.expressionengine.com/latest/development/legacy/api/api-channel-entries.html

And yes, as to your second part; just use the regular template codes to output your channel data.

1
  • 1
    Perfect. I'll play around with these models and see what happenes.
    – Shayne
    Apr 18, 2019 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.