Yii Framework Forum: underscore and camelCase interchangeable in Yii? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

underscore and camelCase interchangeable in Yii? a simple newbie question. please help me Rate Topic: -----

#1 User is offline   vann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 05-April 11

Posted 09 April 2011 - 07:13 AM

Hi, I'm really new to Yii. I was just wondering if using this_style and thisStyle really matters to Yii when referring to AR attributes. For example, how come I can write:

        $newProject=new Project;
        $newProjectName = 'Test Project Creation';
        $newProject->setAttributes(array(
                'name' => $newProjectName,
                'description' => 'This is a test for new project creation',
                'createTime' => '2009-09-09 00:00:00',
                'createUser' => '1',
                'updateTime' => '2009-09-09 00:00:00',
                'updateUser' => '1',
            )
        );


When the table columns are actually using underscores. (see snippet from fixture below) I'm kinda confused. Is Yii doing something automagically for me?

'project1'=>array(
        'name' => 'Test Project 1',
        'description' => 'This is test project 1',
        'create_time' => '',
        'create_user_id' => '',
        'update_time' => '',
        'update_user_id' => '',
    ),

0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 09 April 2011 - 02:19 PM

I assume that it is not throwing an error as you are using the setAttributes method.

Please review the following code:

public function setAttributes($values,$safeOnly=true)
	{
		if(!is_array($values))
			return;
		$attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());
		foreach($values as $name=>$value)
		{
			if(isset($attributes[$name]))  // <--- checks for attribute
				$this->$name=$value; // <--- if exists then set, nothing happens if doesnt exists
			else if($safeOnly)
				$this->onUnsafeAttribute($name,$value);
		}
	}


If you try to set the values using __set, __get from CComponent, then it will throw an error

// This is ok as update_time exists
$model->update_time = '2009-09-09 00:00:00';

// This will throw an error if table column is update_time
$model->updateTime = '2009-09-09 00:00:00';

¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#3 User is offline   chiefsucker 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 02-February 11
  • Location:/dev/random

Posted 02 May 2011 - 04:25 AM

Is there a way to map table column names to properties? Yii suggests to name variables with camelCase while most sources (including Yii’s blog tutorial) suggest to name columns in tables lowercase with underscores only. The main point seems to be maximizing compatibility across various systems.

I prefer to stick to these conventions, but haven’t found a way of mapping properties to column names yet.
0

#4 User is offline   lgespee 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 11-May 11

Posted 18 May 2011 - 07:09 AM

As chiefsucker already mentions, I agree it's quite inconsistent in Yii.

Looking at the Yii convention page under 2. Code it says:

Quote

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.

Under 6. Database it says:

Quote

  • Both database tables and columns are named in lower case.
  • Words in a name should be separated using underscores (e.g. product_order).

However column names in the database like department_manager (like the convention tells us) will convert to a model field name department_manager instead of departmentManager (preferred by the convention).

It would be really nice if Yii's __get and __set methods of CActiveRecord would transform names like department_manager to departmentManager.
1

#5 User is offline   vann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 05-April 11

Posted 18 May 2011 - 09:34 AM

Cool. thanks for your replies guys. :)
I'm really a configuration guy, but yii looked really interesting.

anyway, i went back to what i know (codeigniter). it's not as well designed imo, but hey projects have deadlines. Might study again when i have time.
0

#6 User is offline   edwaa 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 17-May 09
  • Location:Seattle, WA

Posted 19 March 2012 - 02:08 AM

I second this! In the meantime I suggest the documentation be revised so that it is not inconsistent.

View Postlgespee, on 18 May 2011 - 07:09 AM, said:

It would be really nice if Yii's __get and __set methods of CActiveRecord would transform names like department_manager to departmentManager.

0

#7 User is offline   markvr 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 17-June 12

Posted 20 July 2012 - 12:39 PM

Sorry to bounce an old thread but yeah I'm confused, what is the deal with this?

I camelCased my DB names because I care more about my PHP objects that my database, but then when doing a relation ($model->users) Yii seemed to be looking for user_id rather than userId despite my relations saying:
    public function relations()
    {
        return array(
            'user' => array(self::BELONGS_TO, 'User', 'userId'), 
        );
    }


So what IS the recommended way of naming db columns etc? Or is there not a convention for this?

cheers!
0

#8 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 20 July 2012 - 12:54 PM

Lowercase singular. :)
Underscore as word-delimiter, or lowercaseCamel for composite words (userId maps to user_id)
"Less noise - more signal"
0

#9 User is offline   markvr 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 17-June 12

Posted 20 July 2012 - 01:32 PM

Does Yii map underscores to camelCasing?

With a database column of login_provider, I get "HaLogins.loginProvider is not defined" if I camelCase the property name in PHP. i.e. $model->loginProvider = 'blah'
0

#10 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 493
  • Joined: 08-August 09

Posted 20 July 2012 - 09:54 PM

View Postmarkvr, on 20 July 2012 - 01:32 PM, said:

Does Yii map underscores to camelCasing?

No.
0

#11 User is offline   urmaul 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 02-July 12

Posted 24 July 2012 - 01:20 AM

The easiest way is to use camelCase in the names of DB fields.

Or you can find/write behavior that adds mapping. But I'm not sure you can cover all use cases.
0

#12 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 July 2012 - 07:31 AM

View Postyugene, on 20 July 2012 - 09:54 PM, said:

No.


It does.

If you have a table called model_manager Gii will map it to ModelManager.
"Less noise - more signal"
0

#13 User is offline   Imre 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 30-March 11

Posted 18 September 2012 - 01:44 PM

Hello, jacmoe
It would not do so for table attributes.

This naming convention thing is bugging me to no end:
say i have to now declare a custom field in my model, how should i do it

case1) use camelcase:
function getFullName() {...}

and then use it in gridView mixing different cases:
'columns'=>array('user_id', 'fullName')


OR

case2) use lowercase underscore which leads to ridiculous method name:
function getFull_name() {...}

but i can use it in gridView and it wont look differen there:
'columns'=>array('user_id', 'full_name')


neither looks clean to me.
0

#14 User is offline   Gessle 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 15-December 12

Posted 29 December 2012 - 06:14 AM

I agree that we should have a way to set default setting for member variables. Whether they are camelCased or something_else. I would prefer the first one.
0

#15 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 358
  • Joined: 23-July 09

Posted 29 December 2012 - 05:58 PM

In my opinion camel case for variables is awful, I will always use lower case/underscore, with one exception: If the variable represents a class name, then camel case is fine as that helps me to instantly recognise it as such, but otherwise, no. I think that the code then is far more readable.

For database columns, always lower case/underscore. I think that anything else is just looking for bugs.

Classes and methods, the Yii convention is good - instantly recognisable.
We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users