1

Some background:I am trying to debug a component(JoomShopping) which is not working correctly after I migrate 2.5 to 3.7 (I followed their migrate instruction and didn't encounter any problem during the upgrading process, but when I go to some certain page, there is a 1054 database error)enter image description here

Question: In my past experiences I can put var_dump anywhere to see the value of a variable. But this time it doesn't output anything. For example, I am trying to debug the 3rd item in the above picture(attribut.php):

class JshoppingModelAttribut extends JshoppingModelBaseadmin{

    protected $tableFieldOrdering = 'attr_ordering';

    public function getNameAttribut($attr_id) {
        $db = JFactory::getDBO();
        $lang = JSFactory::getLang();
        $query = "SELECT `".$lang->get("name")."` as name FROM `#__jshopping_attr` WHERE attr_id = '".$db->escape($attr_id)."'";
        $db->setQuery($query);
        return $db->loadResult();
    }

    public function getAllAttributes($result = 0, $categorys = null, $order = null, $orderDir = null){
        $lang = JSFactory::getLang();
        $db = JFactory::getDBO();
        $ordering = "A.attr_ordering asc";
        if ($order && $orderDir){
            $ordering = $order." ".$orderDir;
        }
        $query = "SELECT A.attr_id, A.`".$lang->get("name")."` as name, A.attr_type, A.attr_ordering, A.independent, A.allcats, A.cats, G.`".$lang->get("name")."` as groupname
                  FROM `#__jshopping_attr` as A left join `#__jshopping_attr_groups` as G on A.`group`=G.id
                  ORDER BY ".$ordering;
        extract(js_add_trigger(get_defined_vars(), "before"));
        $db->setQuery($query);
        var_dump($db); //debug
        $list = $db->loadObjectList();
        .......

The var_dump doesn't output anything. Why is this?

Also I want to ask why this 1054 error doesn't get logged in PHP's error_log? More importantly, how do I efficiently debug this component? (I post a question in the component's forum but I doubt I would get much help from there)

  • Please endeavor to progress all of your questions to a system recognized resolution (the green tick). You are rewarded by doing so AND the volunteers that have helped you will be rewarded too -- this is a win-win for the community. If any of your questions do not have a suitable solution provided, please take the time to clarify your question, so that a working solution can be posted for you. – mickmackusa Jul 20 '18 at 7:07
3

Sometimes there are subsequent actions that take away the focus without displaying the var_dump statement. There are two ways I use to view debug information in these conditions. First you can follow your var_dump statement with an exit() statement. Include a string if you need to know which exit statement was used (ie: exit('here'). The other option is to display the variable in a Joomla message instead:

JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');

Between these two solutions (and var_dump and echo) you can always get the debug info you need.

1

Have you tried echoing anything instead of the var_dump? For example, you can have something like echo('here'); or die('here');.

I think the likely issue is that your code is not even reaching the var_dump line, and that's why it's not printing the variable.

0

how do I efficiently debug this component?

Use try{}catch{} blocks and include $e->getMessage(), dump(), and resultset checks. Using Joomla query methods is also a good idea.

public function getNameAttribut($attr_id) {
    $db = JFactory::getDBO();
    $lang = JSFactory::getLang();
    try {
        $query = $db->getQuery(true)
                    ->select($db->qn($lang->get("name")) . " AS name")
                    ->from($db->qn("#__jshopping_attr"))
                    ->where("attr_id = " . (int)$attr_id);  // assuming attr_id is an integer
        echo $query->dump();  // never show query to the public
        $db->setQuery($query);
        if (($result = $db->loadResult()) === null) {
            echo "Null Resultset - Possible Query Logic Error @ getNameAttribut()";
        }
        return $result;
    } catch (Exception $e) {
        echo "Syntax Error", $e->getMessage();  // never show error messages to the public
        return false;
    }
}

and

public function getAllAttributes($result = 0, $categorys = null, $order = null, $orderDir = null) {
    if (!$order) {
        $order = 'a.attr_ordering';
    }
    if ($orderDir != 'DESC') {
        $orderDir = 'ASC';
    }
    $lang = JSFactory::getLang();
    $db = JFactory::getDBO();
    try {
        $query = $db->getQuery(true)
                    ->select(
                        array(
                            "a.attr_id",
                            "a." . $db->qn($lang->get("name")) . " AS name",
                            "a.attr_type",
                            "a.attr_ordering",
                            "a.independent",
                            "a.allcats",
                            "a.cats",
                            "G." . $db->qn($lang->get("name")) . " AS groupname"
                        )
                      )
                    ->from($db->qn("#__jshopping_attr", "a"))
                    ->leftJoin($db->qn("#__jshopping_attr_groups") . " G ON " . $db->qn("a.group") . " = G.id")
                    ->order($db->qn($order) . " " . $orderDir);
        //extract(js_add_trigger(get_defined_vars(), "before"));
        echo $query->dump();  // never show query to the public
        $db->setQuery($query);
        if (!$result = $db->loadObjectList()) {
            echo "Null Resultset - Possible Query Logic Error @ getAllAttributes()";
        }
        return $result;
    } catch (Exception $e) {
        echo "Syntax Error", $e->getMessage();  // never show error messages to the public
        return false;
    }
}

I do not endorse the use of extract(). I have never had a good reason to use it in any of my projects. There is risk involved with its call, that it could overwrite global variables AND this generally floods the global variable pool. I don't know what it is meant to achieve in this code, but I would urge you to find another way.

I have not tested the above code blocks, so if anyone discovers a typo let me know and I'll fix it up.

p.s. It appears that your $lang value is not being rendered, since the column that is not found is called name_en-GB. I think the first thing to investigate is the $lang value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.