how to config conf.neon to use Dependency Injection?

Notice: This thread is very old.
alnux
Member | 139
+
0
-

This is my Dependency Injection

namespace App\Model;

use \Nette\DI\Container,\Nette\Database\Context;
use App\Model\Paises;
/**
 * Contenedor de modelos.
 */
class ModeloContenedor extends Container
{

    /**
     * createServiceConnection - Crea la conexion a la base de datos
     * @return [type] [description]
     */
    function createServiceConnection(Context $conexion)
    {
        return $conexion;
    }

    // this call to a model class
    function crearPaises()
    {
       return new Paises($this->connection);
    }

}

and here my pressenter with @inject annotation

namespace App\Presenters;

use Nette,
	App\Model\ModeloContenedor;


/**
 * Homepage presenter.
 */
class HomepagePresenter extends BasePresenter
{

	// 3) Public variable with the @inject annotation:
    /** @inject @var \App\Model\ModeloContenedor */
    public $modeloContenedor;

	public function renderDefault()
	{
		//$this->template->anyVariable = 'any value';

		//$container = new ModeloContenedor;

		//$paises = $container->crearPaises();

		//dump ($container->crearPaises());

		//$this->template->con = $this->getContext()->;
	}

}

and finally my conf.neon file

parameters:
    database:
        driver: mysql
        host: localhost
        username: root
        password: usbw
        dbname: herbanette

nette:
    database:
        default:
            dsn: "%database.driver%:host=%database.host%;dbname=%database.dbname%"
            user: %database.username%
            password: %database.password%

when i try to run the app the next error show me

Nette\DI\MissingServiceException

Service of type App\Model\ModeloContenedor not found

that is becouse i added the next code to my conf.neon

services:
    modeloContenedor:
        class: App\Model\ModeloContenedor
        setup:
            - createServiceConnection("@nette.database.default.context")

and now the browser show me the next error

Nette\DI\ServiceCreationException

Service 'nette.presenterFactory': Multiple services of type Nette\DI\Container found: modeloContenedor, container``

Could some one how i have to conf my conf.neon to this can work

really thanks

Oli
Member | 1215
+
0
-

It's mutch easier.

ModeloContenedor

namespace App\Model;
class ModeloContenedor
{
	private $context;

	function __construct(\Nette\Database\Context $db)
	{
		$this->context = $db;
	}

	public function getPaises()
	{
		return $this->context->table('paises');
	}
}

HomepagePresenter

class HomepagePresenter extends BasePresenter
{
    /** @inject @var \App\Model\ModeloContenedor */
    public $modeloContenedor;

    public function renderDefault()
    {
	// you don't want give connection to template!
        $this->template->paises = $this->modeloContenedor->getPaises();
    }

}

config.neon

services:
	# context is automatically injected
	modeloContenedor: App\Model\ModeloContenedor
alnux
Member | 139
+
0
-

Hi oli and thanks for the fast answear, but is there any way to use “ModeloContenedor” as Container extension becouse my other models (eg Paises) have the querys that is way i add new Paises in my ModeloContenedor class

// this calling Paises class witch have the querys
return new Paises($this->connection);

I based on Dependency Injection document, and i want to do someting similar. Your answear works when call table on “ModeloContenedor” but instead of that if I call new Paises class does the connection doesn do anything

adding to my scripts above here you have Paises model Class

namespace App\Model;

use App\Model\BaseModel;
/**
 * Clase Modelo Paises.
 */
class Paises extends BaseModel {

	protected $tabla = AjustarTablas::T_PAISES;

}

and here my BaseModel class

namespace App\Model;

use \Nette\Object, Nette\Database\Context;
use App\Model\AjustarTablas;
/**
 * Modelo base.
 */
class BaseModel extends AjustarTablas {

	private $conexion;

	protected $tabla;

	/**
	 * Captura conexion a la base de datos
	 * @param Nette\Database\Context $database variable objeto de conexion a base de datos
	 */
    public function __construct(Context $conexion)
    {
        $this->conexion = $conexion;
        //dump($this->conexion);
    }


    /**
     * Retorna todos los items
     *
     * @return Nette\Database\Table\Selection
     */
    public function all()
    {
       return $this->conexion->table($this->tabla);
    }

}

AjustarTablas is extend of Object nette class and it just have constants about name of tables

Last edited by alnux (2014-06-13 16:02)

Oli
Member | 1215
+
0
-

I'm not sure, what you want to do. I suppouse, you want database connection in your component ModeloContenedor and in this component work with some data from database. If it is your target, it's easy. You have almost everything, i belive. The code should be:

Paises

namespace App\Model;
class Paises extends BaseModel
{
    private $context;

    function __construct(\Nette\Database\Context $db)
    {
        $this->context = $db;
    }
}

ModeloContenedor

class ModeloContenedor extends control
{
    private $paises;

    function __construct(\App\Model\Paises $paises)
    {
        $this->paises = $paises;
    }


    public function render()
    {
        $allRows = $this->paises->all();
    }

    // some code
}

config

services:
    modeloContenedor: App\Model\paises
    - : App\Model\ModeloContenedor(@modeloContenedor) # you can replace "-" with some name, if you need this class in another.

I don't know, why you want to inherite from Container. I don't think, it is good practice.

I hope, i understand your problem.

alnux
Member | 139
+
0
-

Thanks Oli, the before answear works so good , i was doing someting wrong on showing datas. But in witch cases can i use classes extends from container and how can i conf conf.neon to work it becouse as you saw in my first post i had

Nette\DI\ServiceCreationException

Service 'nette.presenterFactory': Multiple services of type Nette\DI\Container found: modeloContenedor, container``

Is just a general question . thanks again

Oli
Member | 1215
+
0
-

It's because (i think) Container is injected automatically and if you inherit it this instance is twice. It's same if you want use ChildModel extends ParentModel. You can set in config just ChildModel. If you set both, you will receive this error.