Cphalcon: 驴how set model instance on controller contructor?

Created on 26 May 2017  路  13Comments  路  Source: phalcon/cphalcon

class IndexController extends \Phalcon\Mvc\Controller
{

public $m_index;
public function onConstruct()
{
    $this->$m_index = new M_index;
}

public function indexAction()
{
    $this->view->render("", "login");
}

    public function consultaAction()
{
    $this->$m_index->m_consulta();
}

}

show this message:
Notice: Undefined variable: m_index in C:\xampp\htdocs\phalconOne\app\controllers\IndexController.php on line 9

Notice: Undefined variable: m_index in C:\xampp\htdocs\phalconOne\app\controllers\IndexController.php on line 19

Please help

Most helpful comment

But the problem is not framework, the problem is that you don't even know basic php standards, first learn php, then start using framework

http://www.phptherightway.com
http://php.net/manual/en/

Also start using any decent editor, VSC or phpstorm or even netbeans would tell you that $m_index is undefined variable.

All 13 comments

$this->$m_index != $this->m_index

Use forum for questions. @sergeyklay close it

correction:
$this->$m_index = new M_index();
show same error

Every step that I give as insignificant as it is, I delay until one day in solving it, it is a very sturdy framework, but it is difficult to get it going.

But the problem is not framework, the problem is that you don't even know basic php standards, first learn php, then start using framework

http://www.phptherightway.com
http://php.net/manual/en/

Also start using any decent editor, VSC or phpstorm or even netbeans would tell you that $m_index is undefined variable.

In php always instantiates models from the constructor, even in other frameworks, but phalcon does not allow it

Phalcon allow it too, the problem is you don't even know what this error mean and what's your problem, just toally forgot about framework for now, go to basics, php doc and phptherightway, read it, and go to some basic code, like defining variables, creating your own classes etc without any framework.

Don't start learning from using a framework.

@incubux your problem is bad php syntax, not the framework. You are using a variable variable..

驴how set model instance on controller contructor?

I will look for another framework, as I see, forums and tutorials are more than 2 years and many questions just like stupid to mine, maybe because of that the community activity in this framework is smaller compared to other frameworks, I think You have to be an incredibly intelligent being like Jurigag to use phalcon, there are few privileged ones, thanks for your time and sorry for the inconvenience, bye.

You know you will do same mistake in other framework, right? The mistake you made is not about framework.

@incubux change your code like this:

class IndexController extends \Phalcon\Mvc\Controller
{
    public $m_index;
    public function onConstruct()
    {
        $this->m_index = new M_index();
    }

    public function indexAction()
    {
        $this->view->render("", "login");
    }

        public function consultaAction()
    {
        $this->m_index->m_consulta();
    }
}

If you would ask this question on stackoverflow or forum i would really answer it and help your problem, but github issues are for framework bugs or new features, not questions about language basics, but i guess on stack it would get like -10 points instantly.

Was this page helpful?
0 / 5 - 0 ratings