How can I use the $ this-> doctrine ()-> getManager () method in the constructor?

Source: Internet
Author: User
My current structure is to have a CommonController and then write the query menu method in it. When the subclass inherits, you must call this method. Can I use the $ this-& amp; gt; doctrine ()-& amp; gt; getManager () function in the constructor. In the constructor... my current structure is to have a CommonController and then write the query menu method in it. When the subclass inherits, you must call this method.
Can I use the $ this-> doctrine ()-> getManager () function in the constructor. An error is reported when I write it in the constructor. The has () cannot be found ().
What can I do? Thank you ~

Parent. php

class AParent{    private $menu;    public function __construct(){            $this->menu = $this->doctrine()->getManager()                        ->getRepository(Menu')->findAll();    }}

Child. php

class Child extends AParent{    public function __construct(){        parent::__construct();        var_dump(parent::$menu);    }}

Reply content:

My current structure is to have a CommonController and then write the query menu method in it. When the subclass inherits, you must call this method.
Can I use the $ this-> doctrine ()-> getManager () function in the constructor. An error is reported when I write it in the constructor. The has () cannot be found ().
What can I do? Thank you ~

Parent. php

class AParent{    private $menu;    public function __construct(){            $this->menu = $this->doctrine()->getManager()                        ->getRepository(Menu')->findAll();    }}

Child. php

class Child extends AParent{    public function __construct(){        parent::__construct();        var_dump(parent::$menu);    }}

If this is the case, you need to use Dependency Injection:

Your

class AParent{    private $menu;    public function __construct(){            $this->menu = $this->doctrine()->getManager()                        ->getRepository('Menu')->findAll();    }}

You can change it:

class AParent{    private $menu;    private $container;    public function __construct( $container){        $this->container = $container    }    public function makeMenu()    {        $this->menu = $this->container->get('doctrine')->getManager()                        ->getRepository('Menu')->findAll();    }    public function getMenu()    {        return $this->menu;    }    public function setMenu( $menu)    {        $this->menu = $menu;        return $this;    }}

Use in controller:

public function indexAction(){    $menuBuilder = new \XXX\XXX\AParent( $this->container );    $menuBuilder->makeMenu();    $menu = $menuBuilder->getMenu();}

You can also set it to servive:

Services. yml:

services:    menu.builder:        class: XXX\XXX\AParent        arguments: [@service_container]

Then use the following in the controller:

public function indexAction(){    $menuBuilder = $this->get('menu.builder');    $menuBuilder->makeMenu();    $menu = $menuBuilder->getMenu();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.