Jump to content

Variables in the Model (MVC)


georgerobbo

Recommended Posts

Hello

 

I'm developing a Model-View-Controller framework for my personal learning curve. I've created an active record class and I've got stuck when using variables in a where clause. My problem is this:

 

When, in a where statement, you want to find the Users with the first name Mike, you might write

SELECT Firstname, Surname FROM Users WHERE Firstname = 'Mike'

 

Where Mike is enclosed with quotation marks. When you're comparing entities quotation marks shouldn't be used.

For example,

 

SELECT Firstname, Surname, OrderTitle FROM Users, Orders WHERE Firstname = 'Mike' AND Users.Firstname = Orders.Firstname

 

So when using variables in my where statements, like

 

$Name = Mike

$this->Where(Firstname',$Name);

$this->Where('Users.Firstname','Orders.Firstname');

 

How do I differentiate, in my function Where() when to enclose the string with quotation marks, i.e when it is a (Table.Entity) rather than a constant.

 

Thank you

Link to comment
Share on other sites

The method (pun intended) by which you build the syntax of your query should be separate and different from the method by which you put program driven data values into the query statement.

 

I would use place holders for the variable data, i.e. ? and use a different class method to build the query syntax from the class method used to populate any variable data in that query.

Link to comment
Share on other sites

.. and use a different class method to build the query syntax from the class method used to populate any variable data in that query.

 

Like:

 

$this->Join('Orders', 'Users.Firstname = Orders.Firstname');

 

An ActiveRecord class is not a Query Object (or Interpreter). The Active Record defines behavior specific to a record.

 

class User {
    public function verify($verificationCode) {
        /* after successfull validation, activates the user account */
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.