Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:32:43+00:00 2026-05-22T01:32:43+00:00

I’ve fairly new to PHP MVC and I’m not 100% sold I’m doing this

  • 0

I’ve fairly new to PHP MVC and I’m not 100% sold I’m doing this in the most logical way possible. This is a piece of code from one of my controllers. A friend said a majority of this code should be moved into the Model? Is this so?

Right now I have a DataAccess layer that does all the SQL work. For User work I have a UserDataAccess object, which will create, update and delete. It appears I’m using something along the lines of the Data Mapper pattern. My Model classes map directly to the database columns on a given table and that’s it.

I’m continually confused at where each piece of code should go.

// Posted values passed all checks.
        if(count($errors) == 0)
        {                   
            try
            {
                // Get a Connection.
                $connection = ConnectionFactory::GetConnection('mysql');

                try
                {
                    $connection->beginTransaction();                

                    // Create DataAccess object.
                    $uda = new UserDataAccess();

                    // Check if username and email are avail.                       
                    if(count($uda->CheckUsername($connection, $user->Username)) > 0)
                        $errors['username_taken'] = "Username is taken";
                    if(count($uda->CheckEmail($connection, $user->Email)) > 0)
                        $errors['email_taken'] = "Email is taken";      

                    if(count($errors) == 0)
                    {
                        // Perform Create.
                        $userId = $uda->CreateUser($connection, $user->GetArray());
                        // Add User to Rookie Role.             
                        $uda->AddUserToRole($connection, 1, $userId);   
                        // Commit connection.
                        $connection->commit();
                    }

                    // Clear connection.
                    $connection = null;

                    // Redirect user.   
                    if(count($errors) == 0)
                        header('location: /' . $user->Username);
                }
                catch(PDOException $e)
                {
                    $connection->rollBack();
                    $errors['internal'] = "Opps: There was an error processing the request.";
                }
            }
            catch(Exception $e)
            {
                $errors['internal'] = "Opps: There was an error processing the request.";   
            }                               
        }   
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:32:44+00:00Added an answer on May 22, 2026 at 1:32 am

    From the looks of it, it seems like some code that sound go into the controller.

    Here’s a break down of MVC:

    • Model: Accessing database, preferably using an object oriented method that treats data like object.
    • View: The HTML of the page.
    • Controller: Logic that allows a dynamic page to be built.

    That probably sounded really abstract. The general idea of views is that they are the HTML template, with minimum codes, preferably only echoing certain dynamic element of the page (NOT THE HTML, just plain text, usually) and/or a bit of if and foreach loops. Example:

    .... Your HTML code
    <?php foreach ($page->users as $user): /* Loops through all the users */ ?>
    <li><?php echo $user->name; /* echo their name */ ?></li> /
    <?php endforeach; ?>
    .... Your HTML Code
    

    The idea behind controllers is how you get your page to actually work, by handling the logic, getting input, and such. Example:

    class Controller extends BaseController{
        function indexpage($args=array()){
            if ($args[0] == 'user') $user = UserModel::listUsers(); // If the argument to the controller is 'user' (could be provided by GET, or just the URL, we won't go into that), list all users.
            $this->render('yourViewPage', array('user' => $user)); // renders the view file named yourViewPage.php and pass the value of $user into a variable named 'user'. As shown by above accessed via $page->user.
        }
    }
    

    Granted that the above is only a simple example, you get the point. The render() renders the page and passes the key => value pair in the array so that the view has access to them.

    The Model is the database interaction. It allows you to access the database without using SQL (preferably). Example:

    class UserModel{
        public $name;
        public $openid;
        public static function listUsers($offset=0, $max=20){
            global $persister;
            return $persister->list('UserModel', 0, 20, array('order'=>'NAME DESC'));
        }
    }
    
    // Create a new user. This usually goes into the controller
    
    $user = User(); 
    $user->name = 'Your User'; // sets name
    $user->openid = 'htto://theiropenidprovider.com/openid'; // sets openid
    $persister->track($user); // Adds the user to be tracked. (the tracking info is usually written in XML, but we won't go into that).
    $persister->flushAll(); // Saves to the database (kinda like commit)
    // Gets the user.
    $persister->find('UserModel', 'name', 'Your User') // Find the user that has the name of "Your User" in all UserModel instanced tracked.
    

    So Models don’t have to have the most coding. In my opinions controllers would have a lot of code, but that totally depends on the complexity of what you’re building.

    Hope that clears it up for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.