Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Object property name


Object property name

Closed Thread
  #1 (permalink)  
Old 12-19-11, 05:51 PM
redexpertos redexpertos is offline
New Member
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Object property name

Hello, I have many database fields with minus character in name like "COD-NOMINA"

I got a object using joomla framework
$consulta = "SELECT * FROM tmp_aportes WHERE `COD-NOMINA`='3108801'";
$db->setQuery($consulta);
$aportes = $db->loadObject();

but canīt access property anymore because I can not escape the - sign, how can I access those properties?
  #2 (permalink)  
Old 12-20-11, 08:44 AM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
property?? huh? which property??
  #3 (permalink)  
Old 12-20-11, 12:29 PM
redexpertos redexpertos is offline
New Member
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Joomla framework method setQuery builds and populate the object automagically
Joomla! Help Site - [REVIEW] database->loadObject

Properties also known as object vars
PHP: get_object_vars - Manual

The problem is that object var names are taken directly from database field names, and database field names have the minus character inside the name like "COD-NOMINA".

$consulta = "SELECT * FROM tmp_aportes WHERE `COD-NOMINA`='3108801'";
$db->setQuery($consulta);
$aportes = $db->loadObject();

So it is necesary to escape those characters to avoid php attemps to operate a substraction (-) when evaluates the code

for example in this code

echo $aportes->COD-NOMINA ;

because if is not escaped php checks for a "property" $aportes->COD instead the required $aportes->COD-NOMINA

Thanks for any ideas.
  #4 (permalink)  
Old 12-20-11, 12:59 PM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
never really used joomla but from what what you posted it seems to be returning the result as a constant:
Code:
echo $aportes->COD-NOMINA ;
so what you could try instead is
Code:
echo $aportes->constant("COD-NOMINA") ;
  #5 (permalink)  
Old 12-20-11, 02:53 PM
redexpertos redexpertos is offline
New Member
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks bradgrafelman, it works.

Quote:
$aportes->{'COD-NOMINA'}
and surely, changing names would be a good decision but it is not an option at this time.
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
arranging data mailme420 PHP 0 04-13-11 08:29 AM
Creating a browser RPG in javascript SolidSamurai JavaScript 26 10-20-08 03:27 PM
Adding Expandable Property Support? tim8w Windows .NET Programming 1 07-10-07 11:22 AM
TreeView Control - Background Image? tim8w Windows .NET Programming 1 04-11-07 12:43 PM
ASP upload prob minority ASP 1 06-27-05 08:35 AM


All times are GMT -5. The time now is 08:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.