Naming convention for multiword view files in Yii? - yii

If I have a view with two or more words, e.g.:
public function actionApprovalQueue()
what is the naming convention of the view file itself?
approval-queue.php
approvalQueue.php
approvalqueue.php
??
The documentation on Conventions only says:
View files should be named after the view name. For example, the index
view is in the index.php file.
which gives no clue about views with two or more words.

When it comes to naming conventions, it comes down to what suits your organisation, or what is followed in your organisation, or what the rest of your team decides. The key is consistency throughout your code base.
I would say go with the dash(hyphen), because variables are generally named $xyzAbc or $xyz_abc. So it would make sense to use approval-queue.php.
Definitely do not go for approvalqueue.php.
Edit: Read more about Yii's conventions here.

Yii recommends naming variables, functions and class types in camel case which capitalizes the first letter of each word in the name and joins them without spaces. Variable and function names should have their first word all in lower-case, in order to differentiate from class names (e.g. $basePath, runController(), LinkPager). For private class member variables, it is recommended to prefix their names with an underscore character (e.g. $_actionList).
Because namespace is not supported prior to PHP 5.3.0, it is recommended that classes be named in some unique way to avoid name conflict with third-party classes. For this reason, all Yii framework classes are prefixed with letter "C".
A special rule for controller class names is that they must be appended with the word Controller. The controller ID is then defined as the class name with first letter in lower case and the word Controller truncated. For example, the PageController class will have the ID page. This rule makes the application more secure. It also makes the URLs related with controllers a bit cleaner (e.g. /index.php?r=page/index instead of /index.php?r=PageController/index).

Related

Yii CActiveRecord with Column Named "attributes"

I used the CRUD generator from a legacy database. When searching for a column value I get the following error:
htmlspecialchars() expects parameter 1 to be string, array given (/usr/local/share/yii/framework/web/helpers/CHtml.php:103)
The problem is that the model has an existing column named "attributes" which is creating a conflict. I removed the entry from the _search.php and commented out all instances in the model hoping to at least get it working but no luck. Any suggestions would be appreciated.
Thanks.
Every CActiveRecord instance (or CModel instance for that matter) has a getter/setter named attributes with which all the attributes can be set. This leads to a conflict because the generated crud code uses the attributes attribute expecting it works as described before.
The controller does something like:
$model->attributes=$_POST['ModelClassName'];
// or
$model->attributes=$_GET['ModelClassName'];
This is meant to set al the (safe) attributes of the model at once. Instead this overwrites the database attribute attributes of your legacy DB model.
This in turn leads to the error you describe, because $_GET['ModelClassName'] and $_POST['ModelClassName'] typically contain arrays of data.
I guess the easiest fix would be to directly call the setter function for the "normal" attributes behavior which would lead to replacing the lines mentioned above with something like the following:
// in the controller
$model->setAttributes($_POST['ModelClassName']);
// and
$model->setAttributes($_GET['ModelClassName']);
I think rest of the generated CRUD code (the views) could and should be left untouched to make it work.
If you want to know how and why this works, it's best to do some research into the __get and __set magic functions and how they're used in the yii framework.

Naming Convention for Property Name that has a Dot

Is there a naming convention for a property/variable that has a dot/decimal in the name of the thing that the property/variable represents?
For example:
A class has a property that represents a 2.5 volt battery. Since we can't name the property/variable as "2.5VoltBattery" (the idea being that we can't have the decimal point in the name), I have always used the convention "2Point5VoltBattery" (replace the dot with the word Point).
I'm curious if there is a naming convention for this.
No; it is preferential and will vary by team preference more than anything.
You can use Battery_2_5_Volts. Not sure whether is exact naming convention. To me it feels as a relevant one.

Clarification of the term "Namespace"

My understanding of the term "namespace" is essentially that of a class; a container of methods and variables. Although that seems to be doubling up on what I consider to be the definition of a class.
Can someone confirm or clarify that belief?
I would say a namespace is a way of logically grouping symbols (classes, functions, ... depending on the exact language you're working with) in a container that ensures that those symbols don't collide with other symbols (which could have the same name) in other namespaces.
Namespace is mainly used for avoiding name conflicts. Suppose if you a class named A but this class may be defined by others . so in this cases you need to separate your class from others. In that instance Namespace is come to act. For eg: you given namespace 'using yourname ' in this name space you defined a class A. so that this class can be distinguished by yourname.A. similary for methods ,variables all thing you can defined in your own namespace.
A namespace is used to have different programming rules in the same program module. Let's say you want to define the function 'string_addition' to mean 'string1' + 'string2' = 'string1string2', but later in the same program you want to define 'string_addition' to mean 'string1' + 'string2' = 'string3'. You can use namespaces, so that in the same file you can call on the different namespaces and get both kinds of rules.
namespace h:stringadd(string1, string2) = string1string2
namespace f:stringadd(string1, string2) = string3
a namespace provides a context for an identifier you are referring to. So in that sense, a class is also a namespace.
In the general sense, a namespace defines a set of names which are unique. In other words, a name is only unique within a namespace.
As this concept applies to different languages, there are many variants that include issues like encapsulation, namepsacing, scope and so on.
A namespace is the domain in which a given name remains unique. For example you may define many classes named Widget but they must all appear within seperate domains to be uniquely identifiable from one another.
The domain in which a name appears depends upon the context in which it is defined. For example in the .NET world, a class name must be unique within a namespace, which itself must be unique within an assembly, so one could say that the domain in which a class name is unique is a namespace and the domain of a namespace is an assembly
Namespace is a group of classes. It becomes a .dll after compilation. So we can add that .dll file to our project and can easily use those classes.
Namespaces aren't a feature of OOP. Specify a programming language.
And, yes, in general they are similar to classes, but only those classes which contain no non-static members (since a namespace couldn't be instantiated to form an object).

What's the benefit of postfixing arrays with "List" for variables?

I have seen numerous developers postfixing variable names with a "List" when it's an array. What's the point of this and would you encourage this style? For example:
// Java
String[] fileList;
String file;
// PHP
$fileList = array();
$file = '';
The idea applies to any language with support for arrays.
The idea? Readability - you can tell the variable is a collection in one glance. This can be achieved with pluralizing the variable name (ie. files).
If the fact that the data type is a list is significant (assuming several different collection types), then using that postfix is the right thing to do (as opposed to simply being a collection).
I personally tend to pluralize variable names, using a list postfix only if it adds information or can't be inferred from the name otherwise (say a list of lists).
I think this is mostly a matter of taste. I tend to name things to reflect what they are or what they do. So if I have a collection or array of File instances, the variable would most probably named files, or have a more specific name if the context allows it. Naming an array of Files fileList is in my humble opinion plain wrong, because, at least in Java, an array is not a List. But then, the compiler won't complain...
More complex collections like a Map get names like keyToValue. So if I had a map which assigns teachers to classrooms this would be called teacherToRoom in my code. I hate grepping through the code to find out what the variables are meant to do, so I try to be as specific as needed with the names.
In conclusion it's all about correct code, and variable names can not influence this outcome from the compiler perspective. But they can very well affect the outcome when it comes to humans working with the code, so it's best to do whatever works for the majority of people working on a codebase.

NHibernate Component mapping (Creating Criteria)

Is there any way to create an “alias” for a component?
I have a “Criteria Builder” that takes strings in the format of “Address.City” (or “User.Address.City”, …) and creates an ICriteria (filters and sorts) based on it.
I am using components to map the “Address” class so it stays in the same table as “User”.
The exception I am getting is:
NHibernate.QueryExceptioncould not resolve property: City of: MyNamespace.User
If I attempt to do not create an “alias” for the Address Component, it works just fine.
However, as it is a criteria builder, is there a way to detect that “Address” is a component and avoid the call criteria.CreateAlias(“Address”)? Any work around?
This is the same question as mine, however the solution is not viable to me (I do not create criteria manually for each query).
Any help would be much appreciated!
You can't create an Alias for Address because Address is not a mapped entity. The only difference between CreateAlias and CreateCriteria is that the former returns the original Criteria, whereas the latter returns the new Subcriteria. So the only classes you can create Criteria for are classes that have been mapped. Since components are not mapped classes, you can't create a criteria around them.
The only suggestion I have is to have your Address class either to implement an empty descriptor interface like IComponent or mark it with a custom ComponentAttribute. Then your CriteriaBuilder can check whether or not the class it's creating a criteria for has this meta data and ignore it.