0

I have a function in my model to sum several columns of a table.

public static function get_sums()
    {
        // Create a new query object.
        $db    = $this->getDbo();
        $query = $db->getQuery(true);

        // Select the required fields from the table.

        $query
            ->select('SUM'($db->quoteName(array('pb_p','pb_s','pb_t','pb_r', 'jc_p','jc_s','jc_t','jc_r','sc_p', 'sc_s','sc_t','sc_r','my_p','my_s','my_t','my_r'))));

        $db->loadResults();

    }

In my view.html file added

$model = $this->getModel( );
$this->get_sums= $model->get_sums();

But it throws an error

Using $this when not in object context

I am stumped trying to get this to work, can anyone offer advice where I am going wrong? TIA Steve

  • The issue here is on the top, literally. static function can't have $this-> in it. – Alexandr Nov 20 '17 at 15:47
  • @Alexandr - He has changed his question since then. – FFrewin Nov 20 '17 at 17:40
  • Steve - if you have a different question - just open up a new one. Don't overwrite and changing your previous question. – FFrewin Nov 20 '17 at 17:41
  • Thanks guys I just used a foreach loop to sum the columns it was easier than trying to figure out the query – Steve C. Nov 20 '17 at 18:10
  • @Alexandr and you are right about the static functions. – FFrewin Nov 20 '17 at 18:13
0

The title of your question should be something like calling Model Methods from View - the "sum a query from database" is misleading as title.

By the way, for your actual problem and not for the title of the question: Correct / Best way of calling model methods inside view.html.php.

Just to prepare you, when you manage to find your way to call your model's method from your view, you will encounter another error.

$db->loadResults() - As far as I know, there isn't a such method in JDatabase class.

I haven't read your query at all to know what you are doing in there and what kind of data you will expect, but the JDatabase on retrieving data from DB should help:

Selecting Data using JDatabase

| improve this answer | |
  • Thanks for the link, it should be load Result no s thanks – Steve C. Nov 18 '17 at 23:02
  • The issue here is not related to the question that marked as duplicate. – Alexandr Nov 20 '17 at 15:45
  • I don't see a setQuery() call before loadResult() either @FFr – mickmackusa Aug 27 '19 at 22:46

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