gbuckingham89
gbuckingham89

Reputation: 385

PHP MVC Structure

As part of a university project I'm developing a web application in PHP and trying to follow MVC principles as much as possible. I'm not using an off-the-shelf framework because I want as much of the project to be my work as possible to help get a higher mark.

To delete an item (each item has an id, status, title, parent_id) from a database I go do the following process....

I also want to delete all child items in the database of the item that has just been deleted. Where is the best place to do this? In the the controller? In a separate functions file? In a model file?

I'm pretty new to the whole MVC idea, so any help / advice is much appreciated.

Upvotes: 0

Views: 919

Answers (3)

GordonM
GordonM

Reputation: 31780

The basic breakdown of MVC is

  • Model: Business logic
  • View: Presentation logic
  • Controller: Glue logic (triggers model functionality based on user input, render views based on model output).

The statement "Deleting an item will cause all its children to also be deleted" is a description of something that needs to happen in the business logic. Therefore, it belongs in the model.

Implementation is up to how you have implemented getting child items and deleting items in your model, but it will typically consist of getting a collection of your model's children in its delete method, and invoking the delete method for each child in the collection (each child will also look for children and delete them when this happens). Now the view and controller don't have to worry about whether or not a model has children, as it's all taken care of in the model's business logic.

Upvotes: 3

SERPRO
SERPRO

Reputation: 10067

I would probably do that outside the PHP MVC pattern. Using MySQL innoDB tables you can achieve that using Foreign Keys:

CREATE TABLE parent (id INT NOT NULL,
                     PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
                    INDEX par_ind (parent_id),
                    FOREIGN KEY (parent_id) REFERENCES parent(id)
                      ON DELETE CASCADE
) ENGINE=INNODB;

Upvotes: 1

Felix
Felix

Reputation: 89626

Part of being a good developer is knowing when not to reinvent the wheel. And this is certainly one of those situations. I suggest you first ask your professor(s) whether you are allowed to use an off-the-shelf framework or not. This "I want as much of the project to be my work" is pretty much nonsense. If you really want it to be your own work, do it in assembler :).

If you still have to write everything from scratch (in PHP), I suggest you first look at well-established MVC frameworks (e.g. Symfony), in order to get the feel of how such a framework should work. Maybe have a look at the source code, too.

Upvotes: 1

Related Questions