-1

I'm facing an error message:

Fatal error: Call to a member function isUploaded() on a non-object in /www/htdocs/nether/http/123factuur/application/controllers/Helpers/ImportXls.php on line 30 

To my understanind this error message pops-up because I'm calling a method which doesn't exists in the object. But I'm sure that isUploaded() does exists.

The function isUploaded is defined in the class Zend_Form_Element_File. To check if $xls is an instance of the Zend_Form_Element_File I debugged the $xls variable.

Zend_Debug::dump($xls); //OUTPUT: object(Zend_Form_Element_File)#141 (29) {
exit;

Line 30 looks like this:

if ( $xls->isUploaded() ) {

The first thing I did was to check the expression value.

Zend_Debug::dump($xls->isUploaded()); //the output was: bool(true)
exit;

Then I checked the type of the $xls variable.

echo gettype($xls); //the output was object
exit;

I'm not fully understanding the error. Perhaps, I'm not interpreting the error message as it should be interpreted. Anyway, assistance is needed.

The code snippet: At the controller:

public function importAction() {
        $form = $this->getImportFrom();
        $this->view->form = $form;
        $this->view->allowedHeaders = array();

        $this->importInvoices($form);
        $this->importInvoiceArticles($form);
        $this->importInvoiceServices($form);
        foreach ($this->_lookupIssues as $issue) {
            $this->_flashMessenger->addMessage($issue);
        }
    }


public function importInvoiceArticles($form) {
        $model = 'Invoice_article';
        $config = Zim_Properties::getConfig($model);
        $Model = new Zim_Model($model, $config->model);
        $headerMapping = array_flip(array_intersect_key($Model->getHeaders(true), array_flip($this->_allowedArticleImportHeaders)));
        $this->getHelper('ImportXls')->handleImport($form, $headerMapping, $Model->getName(), $this->_modelName, null, null, array($this, 'saveImportedArticleData'), 'invoiceArticle');
    }

At the helper:

class F2g_Helper_ImportXls extends Zend_Controller_Action_Helper_Abstract {

public function handleImport($form, $allowedHeaders, $tableName, $modelName, $onDuplicateImportCallback, $importMethod = null, $saveMethod = null, $name = 'xls') {
        if ($this->getRequest()->isPost()) {
            $xls = $form->getElement($name);
            if ( $xls->isUploaded() ) {
               //some code
            }  
        }
    }
}
Julian
  • 4,396
  • 5
  • 39
  • 51

2 Answers2

3

I'm quite sure that the handleImport() method is called multiple times, possibly inside a loop, probably with different values for the $name parameter. You echo the variable and die in order to debug it, which works perfectly if the provided value for $name is correct on the first run - but since you kill the script - you don't get any debug information about subsequent calls.

Make sure the object has that method before calling it. You can either call method_exists() or instanceof to make that determination.

Code:

if ($xls instanceof Zend_Form_Element_File) {
    // object of correct type - continue (preferred version)
}

// or

if (method_exists($xls, 'isUploaded')) {
    // avoids the error, but does not guarantee that 
    // other methods of the Zend_Form_Element_File exist
}
motanelu
  • 3,945
  • 1
  • 14
  • 21
1

Add this to your condition to avoid the Fatal Error :

if ( !empty($xls) && is_object($xls) && $xls->isUploaded() ) {
    // do your job with serenity :)
}
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
  • Well that works but I don't understand why. – Julian Oct 31 '14 at 09:45
  • 1
    @Julian well apparently the code you're working on is being run multiple times. The first times it works because `$xlss` really is an object (which is what you inspected). The last time it doesn't work because `$xlss` isn't an object but you aren't inspecting it so bummer. – Mihai Stancu Oct 31 '14 at 09:57
  • 1
    Halayem Anis -- it's a good solution but it's worth mentioning that OP needs to check why `$xlss` isn't an object and see if there's other stuff to fix based on this. – Mihai Stancu Oct 31 '14 at 09:58