Welcome Guest, Not a member yet? Register   Sign In
The Controlller and Model same filename -> wrong ?
#11

[eluser]drewbee[/eluser]
My controllers reflect a little bit of both. They are very loose.

I will never enter a table name in a controller as it is taken care of in the model, however, I do not force columns to be entered within the model (if that makes sense?).

IE

I have a model method that grabs users get_users().

This method will take one parameter that is an array filling out the $where clause, so when I call it:
$where = array('account_id' => 32);
$this->users_model->get_users($where);
#12

[eluser]slowgary[/eluser]
[quote author="jedd" date="1239213288"]Snoob, Kromack, et al

If you're new to CI, and especially if you're new(ish) to PHP, you should probably avoid renaming Models in this way - it's likely to lead to confusion.

I think a superior approach is to spend a moment more on the design of your system - remember that Controllers are your user-facing component, and each should reflect and encapsulate the functions of a resource within your system. Your Models are your interface to your data, and should reflect the way your models will need to retrieve, store and modify that data. Yes, there are times that these two things are in perfect alignment, but it's not a requirement that they are, and it's often easier to conceptualise things if you keep the two goals, or intents, of Models and Controllers in mind.

It is also an excellent way of avoiding nomenclature clashes. User is the most frequent one that people stumble over. I find the '_model' or '_mdl' suffix feels cumbersome. I've found that thinking of users as People or Members makes things easier, as I can still have a users table (and maybe a model to manage that table), and my People/Member/etc controller will then manage the human related aspects of my site.

Just a different response to the problem of having a User controller, and a User_model model that you rename to User.[/quote]

Jedd, could you give us some examples?

I've been naming my models '_model' for lack of a better method, but it feels wrong for several reasons. One being that it implies a direct relationship between the model and similarly named controller, which is true, but sometimes several controllers may refer to the same model.
#13

[eluser]Dam1an[/eluser]
I also use the '_model' suffix for my models
My controlers are generally called the same as the table name (plural) which they are primarily linked to
My models are generally the singular of the table name they primarily manipulate

I don't do this because its some sort of naming convention (using it for the sake of using it, or because I'm told to) but because it seems right...
It makes sense to me to have a controller calles messages, with view, edit etc methods, and it seems right to have these access the db through a model called message (I alias my models to lose the suffix)

Just my 2 cents/pence Smile
#14

[eluser]Colin Williams[/eluser]
Wow, jedd. Sounds like you jump through a lot of weird, theoretically-supported hoops just to avoid a simple naming clash. Your conclusions, such as "Controllers are your user-facing component" and "Models are your interface to your data," are inclusive with regards to the existence of naming clashes.

I say stop trying to convince yourself that "People" is a better Controller-class name for users, and "User" is a better Model-class name for users, and just append "_model" to your Model, if for only the simple and obvious reason being that you don't want to do cartwheels with your routes because the Model class name is so damned precious and vital that it can't be bullied by suffixes.
#15

[eluser]drewbee[/eluser]
Dam1an,

That is the some methodology I am now using, except the exact opposite. My models are plural, and controllers are singular.
#16

[eluser]sl3dg3hamm3r[/eluser]
[quote author="drewbee" date="1239221619"]This method will take one parameter that is an array filling out the $where clause, so when I call it:
$where = array('account_id' => 32);
$this->users_model->get_users($where);[/quote]

drewbee, I think that isn't any good approach to call your model-function like that. You deal with knowledge about your table-format outside of your model. I would definitely pass only the Id, and build the where-statement within your model. Knowledge about the data-format belongs IMHO into the model, only.
I see that your way may keeps the signature of your function-call very flexible, but overpowered flexibility might also lead to unwanted problems, which might become difficult to debug.
#17

[eluser]drewbee[/eluser]
[quote author="sl3dg3hamm3r" date="1242045489"][quote author="drewbee" date="1239221619"]This method will take one parameter that is an array filling out the $where clause, so when I call it:
$where = array('account_id' => 32);
$this->users_model->get_users($where);[/quote]

drewbee, I think that isn't any good approach to call your model-function like that. You deal with knowledge about your table-format outside of your model. I would definitely pass only the Id, and build the where-statement within your model. Knowledge about the data-format belongs IMHO into the model, only.
I see that your way may keeps the signature of your function-call very flexible, but overpowered flexibility might also lead to unwanted problems, which might become difficult to debug.[/quote]

Yeah, I debated on ways to do this. This is the one and only time I will ever say this, but this is one of the times that I wish functions could work in the way they work within cold fusion.

I would hate to create two functions ie get_users_by_id() or, get_users_by_email() etc etc., and a huge list of parameters is just something that I do not want to manage. I would then need one parameter for every column in the table.
#18

[eluser]slowgary[/eluser]
I looked at coldfusion for a few minutes and ran away as fast as I could. How do functions work in coldfusion? You could always make a get_users_by_query('email', '[email protected]')

I think I'll just stick with appending a suffix onto my model names. Probably '_model' or '_file_that_has_database_code_in_it'.
#19

[eluser]drewbee[/eluser]
Functions are built a little funkier in Cold Fusion, and there is no order to which you call parameters of a function. This one function below can be used strictly

Code:
<cffunction name="get_users" access="public" output="false" returntype="query">
    <cfargument name="account_id" required="false" type="numeric">
    <cfargument name="email" required="false" type="string">
    <cfargument name="name" required="false" type="string">
    <cfargument name="country" required="false" type="numeric">
    // Etc etc

    <cfquery name="local.query" datasource="#dsn#">
    SELECT *
    FROM accounts
    WHERE 0=0
    <cfif StructKeyExists(arguments, 'account_id')>
        AND account_id = #arguments.account_id#
    </cfif>
    <cfif StructKeyExists(arguments, 'email')>
        AND email = '#arguments.email#'
    </cfif>
    <cfif StructKeyExists(arguments, 'name')>
        AND name = '#arguments.name#'
    </cfif>
    <cfif StructKeyExists(arguments, 'country')>
        AND country = #arguments.country#
    </cfif>
    </cfquery>

    <cfreturn local.query />
</cffunction>

get all users
Code:
<cfinvoke method="get_users" returnvariable="users">

get user by id of 23
Code:
<cfinvoke method="get_users" account_id="23" returnvariable="users">

get user by account id of 23 and lives in USA (country code: 1)
Code:
<cfinvoke method="get_users" account_id="23" country="1" returnvariable="users">

get all users in the USA
Code:
<cfinvoke method="get_users" country="1" returnvariable="users">

As you can see one get method can be completely refactored into calling pretty much anything you need from the users table, whether it comes from building the query from lists of ids or really anything. It's the one thing I hate about C based languages, parameter lists in functions.
#20

[eluser]Dam1an[/eluser]
@drewbee: When I first saw that CF function, I freaked out (all I've ever seen is C/C++, C#, Java and PHP)
But I can see it being quite useful...
Still, I think I'll stick to my 'standard' functions for now (although I do have the occasional 'clever' one a bit like your example




Theme © iAndrew 2016 - Forum software by © MyBB