desbest
desbest

Reputation: 4896

How do I run a model inside a Magento controller?

Here's my controller.

public function mockcron_newmatchAction(){
    $task = Mage::getModel('showdown/cron::makematch');
    var_dump($task);
}

And here's the cron function located at app/code/local/Desbest/Showdown/Model

<?php
class Desbest_Showdown_Model_Cron
{
    public function makematch(){
        $var = "apples";
        return $var;
    }
}

The problem is that $task = Mage::getModel('showdown/cron::makematch'); does not run and I want that model to run. What do I do?

The variable prints as false, regardless of whether I have chosen an existing model or not.

Upvotes: 0

Views: 1591

Answers (1)

Alana Storm
Alana Storm

Reputation: 166066

The :: syntax only works if you're providing a source model in a system.xml XML.

ex. 
#File: app/code/core/Mage/Paypal/etc/system.xml
<source_model>paypal/config::getApiAuthenticationMethods</source_model>

It doesn't work when you're writing regular PHP code. The syntax you want is

$task = Mage::getModel('showdown/cron')->makematch();

The call to Mage::getModel('showdown/cron') instantiates your model object, and then the ->makematch(); calls a method, as per standard PHP. When you say

Mage::getModel('showdown/cron::makematch');

you're asking magento to instantiate the class with an alias of showdown/cron::makematch. Since that's an invalid alias alias, this will always return false.

Upvotes: 1

Related Questions