0

I am using the theme "Love Fashion" for magento, however this is just for info if anyone has come across the same problem with this theme before.

I get the following error Notice: Undefined variable: deal. The error occurs on two lines, 50 and 61 - however the error shows up twice on line 50 eg.:

Notice: Undefined variable: deal on line 50
Notice: Undefined variable: deal on line 50
Notice: Undefined variable: deal on line 61

This happens for the following code (starting on line 49, ending on line 67):

public function addFilter($filterName,$filtervalue,$condition='='){
        if ($deal instanceof Sm_Deal_Model_Deal){
            $deal = $deal->getId();
        }
        if (!$this->_joinedFields){
            $this->joinFields();
        }
         $this->getSelect()->where('deal.'.$filterName.' '.$condition.' ?', $filtervalue);
        return $this;
    }

public function OrderbyAdd($orderName,$ordervalue){
        if ($deal instanceof Sm_Deal_Model_Deal){
            $deal = $deal->getId();
        }
        $this->getSelect()->order('deal.'.$orderName.' '.$ordervalue);

        return $this;
    }

So my question is, how do i fix the undefined variable in this case?

For example, i use the following code earlier in the same document, but this doesn't give any errors: (starting at line 26, ending at line 47)

public function addDealFilter($deal){
    if ($deal instanceof Sm_Deal_Model_Deal){
        $deal = $deal->getId();
    }
    if (!$this->_joinedFields){
        $this->joinFields();
    }
    $this->getSelect()->where('related.deal_id = ?', $deal);
    return $this;
}

public function joinFieldsdeal(){

        $this->getSelect()->join(
            array('deal' => $this->getTable('deal/deal')),
            'deal.entity_id = related.deal_id',
            array('deal.end_date','deal.start_date','deal.name')
        );
        $this->_joinedFields = true;

    return $this;
}

The question is not a duplicate of "Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?" as this simply explains the basic of functions - which is not the case here.

7
  • 1
    possible duplicate of Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors? Commented Apr 28, 2015 at 20:07
  • Hi IMSoP - it might be the same sort of problem, but the question you link to, only defines the basic of functions, which I already know. The code i listed above, i don't understand the scoping of, as this is not as clear to me, as the scoping you linked to. Commented Apr 28, 2015 at 20:19
  • 1
    Given the scoping rules explained in the linked answer, where do you think $deal will come from on the line highlighted by the Notice? Commented Apr 28, 2015 at 20:22
  • 1
    Ah, so the function addFilter($filterName,$filtervalue,$condition='=') isn't defining $deal within the addFilter() function, and therefore it cannot be found by the if statement?? Commented Apr 28, 2015 at 20:28
  • 1
    If you add it as a parameter, it takes on whatever value you give it when you call that function - every time you call the function, you have to decide what it is that you want that parameter to be. The whole point of scope is that $deal can mean different things in different places. Consider this: function foo($deal) { var_dump($deal); } $deal='hello'; foo($deal); foo(42); $old_deal=$deal; $deal++; foo($deal); foo($old_deal); etc. Commented Apr 28, 2015 at 20:41

1 Answer 1

1

In both of those functions (addFilter, OrderbyAdd), the offending bit of code is here

if ($deal instanceof Sm_Deal_Model_Deal){
    $deal = $deal->getId();
}

The only variables available in a function are

  • The function parameters ($filterName,$filtervalue,$condition and $orderName,$ordervalue), respectively.
  • Variables defined in the function
  • Any global variables

The variable $deal is none of these. It is, as the error message says, not defined. The clause

if ($deal instanceof Sm_Deal_Model_Deal){
    $deal = $deal->getId();
}

Has no practical impact on your program. The variable $deal is never an instance of Sm_Deal_Model_Deal because the variable $deal does not exist. You can remove the entire if clause from your code and you won't change how your system behaves (except to remove that Notices)

That said -- it's not clear why this code is here in the first place. Someone added it for a reason -- or possibly removed portions of each function that defined $deal. It'd be worth checking these files against a clean version of your theme.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.