0

I recently discovered the excellent redbean ORM library for PHP, which makes performing CRUD operations trivial in my web application, but I recently implemented some additional functionality that I'm starting to question. To make saving data even easier, I created a procedural form-processing script called redbean.php. Whenever this script is specified as the form action, it will create a new bean dynamically based on the data submitted to it:

<?php Template::startContent(); ?>

<form action="forms/redbean.php" method="POST">
    <input type="hidden" name="bean" value="book"/>
    <input type="text" name="author"/>
    <input type="text" name="pubDate"/>
    <input type="submit" value="Save Bean!"/>
</form>

<?php Template::endContent(); ?>

The reason I'm starting to question this approach is because everything else in my framework uses the MVC pattern, but this feels like cheating because the data is going straight from the view to the ORM library. Anyways, I'm trying to implement this application using best practices for PHP and web programming in general, so my question is whether or not this approach represents an anti-pattern that I'm not aware of, or if there is anything else I should consider with this implementation.

jerry
  • 2,743
  • 9
  • 41
  • 61

2 Answers2

2

If you are really trying to stick to the MVC pattern, then yes, you are not adherring to the true pattern by randomly calling a script in your view to add data to the database. One of the purposes of MVC is uniformity. Your form data is passed to the controller, which calls the model to validate it and save it, and then return the error or success call back to the view that is presented. Your script, from what you have described, just ignores all that and creates the bean.

Now, considering you do have validation in your script, when you make any changes to the validation, you will have to reflect it in this script. Simplicity is the name of the game and repeating code in multiple places is just a waste.

If you desire to adhere to MVC rules, just copy a lot of it over to your model and handle it there. It will let you use your script and continue the use of MVC.

Ultimately though, its your script. If it works, it works. If you are working with production materials or in a team, I would not use it. If it is personal use or just for your personal website, go for it.

Tim Withers
  • 12,072
  • 5
  • 43
  • 67
0

I think your approach is just fine - just keep an eye on security and validation. RedBeanPHP also facilitates this approach using the Cooker, it does exactly what you describe but also adds support for relations: http://www.redbeanphp.com/cooker

It's a bit scary because it is really, really powerful and you can easily create security holes, but if handled correctly you can by-pass crufty MVC patterns and do some really hi-speed development.

Don't pay too much attention to OOP, MVC and patterns, they are often very useful but people tend to overuse them. Also, many of these techniques have been developed in a different domain where they function better, they have been copied from this domain to the web where they... well sort-of-work-but-less-optimal. For instance MVC comes from desktop GUIs, OOP from Smalltalk etc. While they are still useful in PHP you should not be too concerned to not use them right - because they have been imported in the PHP domain there no longer is a right way. Just do whatever works. The most important thing is that you (and your co-workers) can easily read and maintain the code. That's all.

Gabor de Mooij
  • 2,997
  • 17
  • 22