New nette and making factories

Notice: This thread is very old.
kaylo2122
Member | 6
+
0
-

Hello there.

I had to create, some abstract methods for models, and create one model for testing etc, but if i want to create new instance of model, i dont know how to do it

i have module called: frontendModule inside is one presenter HomePagePresenter and Basepresenter.

And if i call $this->createService(“articles”), nette will throw me fattal error Call to undefined method frontendModule\HomePagePresenter::createService() .

In neon configuration i have:

services:
	articles:
		class: Model\Articles
		autowired: no

So service, is registered, but i dont know, i have seen David's article about upgrades in new nette ( actual ) but is not work.

Can u tell me how to create instance of model via DI container ? or what is wrong ?

Or did you mean that better way how to create model instance is direct ? \

Thanks.

honos
Member | 109
+
0
-

May be $this->getContext()->createService(“articles”); ?

Vojtěch Dobeš
Member | 1316
+
0
-

You can do:

$this->context->createInstance('Model\Articles');

Ale předávat si celý DI\Container kvůli jedné službě není moc fajn. Ideální je napsat si továrničku ve formě interfacu:

interface IArticlesFactory {
	/** @return Model\Articles */
	fuction create();
}

Tenhle interface se zaregistruje jako služba:

services:
	- IArticlesFactory

Do presenteru si injectneš tuhle IArticlesFactory a můžeš volat:

$articlesFactory->create();
kaylo2122
Member | 6
+
0
-

vojtech.dobes wrote:

You can do:

$this->context->createInstance('Model\Articles');

Ale předávat si celý DI\Container kvůli jedné službě není moc fajn. Ideální je napsat si továrničku ve formě interfacu:

interface IArticlesFactory {
	/** @return Model\Articles */
	fuction create();
}

Tenhle interface se zaregistruje jako služba:

services:
	- IArticlesFactory

Do presenteru si injectneš tuhle IArticlesFactory a můžeš volat:

$articlesFactory->create();

Ano to je pravda, rozhodne je lepsi, mit cistejsi reseni, kazdopadne ale nevim jak na nej.
Co je obsahem funkce create v teto mini tovarnicce ? Jak zajistim ze se mi prave vytvori objekt.

Jde totiz o to ja si vytvoril kompletni abstraktni vrstvu pro praci s tabuky, ted potrebuji vytvorit novou instanci tohto objektu. A pracovat s nim ( jedinne co si mi zde u nette nelibi, ze v prikladech je to volane prave timto spusobem pres DI kontejner ), spise bych pro modely volil prime volani a databazi nejak obecneji, ale budiz.

Kazdopadne, pokud by se volal DI v metode create neni to totez ? ( mimochodem je potreba predat databazove spojeni v konstruktoru )

*Yes, it's truth. But still is better to have cleaned solution, but i don;t know how to do it.
What content is inside of create method of mini factory ? How can i check that i create current model in this mini facotry.

in this fact: i had to create fully abstract layout for working with db table, now i need to create new instance of current model and work with the model. ( I think that better way to make model is direct way )
But in all case, if will be called DI in mini factory, is not the same as in presenter ?
*

//aglicky to pisu jenom proto ze jsem na anglickem foru :) tak at to vedi alspon ostatni taky.

Vojtěch Dobeš
Member | 1316
+
0
-

I am sorry but you have lost me. There is no such thing like “calling DI in mini-factory”. Just write your classes as normally, pass dependencies in constructor and don't forget typehints. Nette can help you with explicit passing of the dependencies – you just setup methods (constructors), and Nette will pass appropriate arguments.

Now to the main topic – sometimes you don't want to get some class created by Nette\DI Container. In such case, proper way is to create factory class for this class. Because such factory class is pretty straight-forward, Nette can assist you again by writing it for you – you just write interface (so create() method has no body written by you, and that's fine).

Sidenote: from your example, do you need more instances of Model\Articles class? If not, the best approach is to register it as normal service.

kaylo2122
Member | 6
+
0
-

Vojtech, i had try your example, but it doesnt work.

I dont understand how it works, can u explain me it more please ?

I need to understand, if i have presenter, how to call model from presenter, better if u will show me some example.

I need know the best way how to do it, because i think that Nette has do, lot of operation without your act.
So in this fact, if i want to call dirrect instance of model, i must get DB connection too and paste it to construtor, i mean.

Or is other way how to do it without DI container ?

Thank you guys.

honos
Member | 109
+
0
-

No ale pokus byl :)

Majkl578
Moderator | 1364
+
0
-

English, please, guys.

Vojtěch Dobeš
Member | 1316
+
0
-

It might be better to switch to Czech in this case :).

Ad topic: if you want to create instance directly in presenter, you can of course do it:

# code in presenter
$articles = new \Model\Articles($this->database);

But then you have to somehow get $this->database filled in the presenter, right? You should probably do it with DI, it's really simple – probably easiest way is to use @inject annotation, like this:

# code in presenter

/** @var \DibiConnection @inject */ # maybe you use different class, I don't know
public $database;

You just have to have class from this inject annotation registered as service in DI config.

And now to big turning point – if you need this $this->database only for new \Model\Articles, it is better to write factory class for it. I described the most efficient way (using the interface) in my first post.

PLEASE: feel free to ask next questions, maybe I am missing important point that would clarify all this to you.

Last edited by vojtech.dobes (2014-01-31 09:39)