The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sun Apr 15, 2012 5:30 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Apr 15, 2012 5:00 pm
Posts: 3
Hello,

I'm having a problem with a component I am writing, specifically a survey form meant for a company project. I'll present it in two parts. The main problem is presented in part 1. Those who want to know more can read on to part two.

*****Part 1*****

One of my colleagues, on completing the survey and clicking submit, gets the error:
Fatal error: Cannot use object of type JUser as array in /home/hpa/sites/dev/components/com_hrbpcalculator/models/hrbpcalculator.php on line 17
(this is with error_reporting -1 and display_errors on in .htaccess).

However, this is not an error I get myself when I complete this survey. Unless I am mistaken, this is a PHP, server-side problem, independent of anything an individual user might have (OS, browser) or be doing.

So, how is this possible? Have I overlooked something?

*****Part 2*****

A little more detail.

It is a survey component, with three pages. Starts on a form where users fill in personal info (name, organisation, etc.). When they submit the controller stores the post data in a jsession (not serialised), and they are sent to the next page, the survey itself. When they submit this the post data is taken, the user data is "get" from the jsession, and both are passed to a method in the model meant to store them in the database (Mysql).
This is the method it appears to be choking on (but for my colleague only). This is the method (part of models/hrbpcalculator.php):

public function store ($response, $user)
{

$db = JFactory::getDBO();

$title = $user['title']; // This is line 17, alluded to in the error above
$fname = $user['fname'];
$sname = $user['sname'];
$org = $user['org'];
$occ = $user['occ'];
$email = $user['email'];
$query = "INSERT INTO `#__hrbp_survey_response` "
. "(survey_id, title, first_name, surname, organisation, "
. "job_title, email) VALUES (1, '$title', '$fname', "
. "'$sname', '$org', '$occ', '$email')";
$db->setQuery($query);
$db->query();

$srvy_resp_id = $db->insertid();
$count = count($response);

for ($i = 1; $i <= $count; $i++) {
$query = "INSERT INTO `#__hrbp_response_int` "
. "(survey_response_id, question_id, response) "
. "VALUES (".$srvy_resp_id.", ".$i.", "
.$response[$i].")";
$db->setQuery($query);
$db->query();
}

return true;

}

$response being the post data from the survey, $user being the post data from the user form (here I'm taking out the title, first name, surname, etc.). Is $user a special variable in php or something?

At no point in my component have I used JUser (to my knowledge, unless another object I am calling uses it).

I have tested this myself and get no error on my own browser (Iceweasel/Firefox on Debian GNU/Linux), nor any other browser I have tried (IE, Chrome/Chromium, Epiphany), nor any other OS (XP, Vista). My colleague has Firefox 5 on Vista (a combination I have tested elsewhere, which worked).

You can see why it has really stumped me. Any suggestions would be appreciated. Please say if I've omitted any useful information.

Thanks,

Dom


Top
 Profile  
 
PostPosted: Sun Apr 15, 2012 11:14 pm 
User avatar
Joomla! Ace
Joomla! Ace

Joined: Sat Oct 28, 2006 11:16 pm
Posts: 1051
Location: Texas
In your component, at any point did you use JFactory::getUser()?

If so, that method returns a JUser object.

JUser is an object - not an array, so you must access its properties as you would an object.

The strange thing is that you're accessing properties not present in the JUser object.

Which particular line of the the code above is line 17?

_________________
Honk if this signature offends you.


Top
 Profile  
 
PostPosted: Mon Apr 16, 2012 8:15 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Apr 15, 2012 5:00 pm
Posts: 3
Hi, thanks for the reply.

No I didn't, I haven't used it at all in my component.

The line commented above ($title = $user['title']) is line 17. $user should just be the POST array, which should be accessible in this way ('title' refers to the name of one of the html input elements in the form).


Top
 Profile  
 
PostPosted: Mon Apr 16, 2012 4:01 pm 
User avatar
Joomla! Ace
Joomla! Ace

Joined: Sat Oct 28, 2006 11:16 pm
Posts: 1051
Location: Texas
I think I know what's going on here.

The $user var is retrieved automatically by the system if you're logged in when you submit the form.

You should be retrieving your POST vars using JRequest.

Here's how I think your store function should look using JRequest:

Code:
public function store ($response, $user)
{
    $db = JFactory::getDBO();

    $title = JRequest::getString('title','');
    $fname = JRequest::getString('fname','');
    $sname = JRequest::getString('sname','');
    $org = JRequest::getString('org','');
    $occ = JRequest::getString('occ','');
    $email = JRequest::getString('email','');
    $query = "INSERT INTO `#__hrbp_survey_response` "
    . "(survey_id, title, first_name, surname, organisation, "
    . "job_title, email) VALUES (1, '$title', '$fname', "
    . "'$sname', '$org', '$occ', '$email')";
    $db->setQuery($query);
    $db->query();

    $srvy_resp_id = $db->insertid();
    $count = count($response);

    for ($i = 1; $i <= $count; $i++) {
        $query = "INSERT INTO `#__hrbp_response_int` "
        ."(survey_response_id, question_id, response) "
        ."VALUES (".$srvy_resp_id.", ".$i.", "
        .$response[$i].")";
        $db->setQuery($query);
        $db->query();
    }

    return true;
}

_________________
Honk if this signature offends you.


Top
 Profile  
 
PostPosted: Tue Apr 17, 2012 9:55 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Apr 15, 2012 5:00 pm
Posts: 3
I've finally found the problem (or at least I think I have).

I had overridden (if that's the correct terminology, redefined maybe) the default display() method in the view, but my new method wasn't compatible with the parent method. (I had done "public function display()", rather than "public function display($tpl=null)", and called parent::display() without the $tpl parameter). Which evidently led to some unpredictable results.

I'll need to investigate further to be certain this was the problem, but for now it looks like it was.

Thanks for you help stutteringpoet,

Dominic


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 



Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group