Nette Di factories must have just one non-static method get()

alnux
Member | 139
+
0
-

Hi, im using Nette Di and based on the nette documentation example i did the next

Model

declare(strict_types=1);
namespace App\Modules\Admin\User\Model;

use Nette;
final class User {
	protected $user;

    public function __construct(private Nette\Database\Explorer $db,) {
        $this->user = 'user';
	}

	public function all() {
		return  $this->db->table($this->user)->where('active = ? AND deleted = ?',1,0);
	}
}

Factory

declare(strict_types=1);
namespace App\Modules\Admin\User\Model;

use App\Modules\Admin\User\Model\User;

interface UserModelsFactory {
    function createUser(): User;
}

and neon

services:
	- App\Modules\Admin\User\Model\UserModelsFactory

but when i go to browser, it show me. the message that i have to have a get function

Service '02': Interface App\Modules\Admin\User\Model\UserModelsFactory must have just one non-static method get(). search►

if get() function is not optional what i have to do with this??? becouse on example talks about in case of call a service of another connection you can call it by this way but the in case of not?? witch is the way

really thanks

Marek Bartoš
Nette Blogger | 1261
+
0
-

The error message is misleading, it should say get() or create(). create() is for generated factories, get() for generated accessors.
You should be able to fix it by renaming createUser() to create()

alnux
Member | 139
+
0
-

Marek Bartoš wrote:

The error message is misleading, it should say get() or create(). create() is for generated factories, get() for generated accessors.
You should be able to fix it by renaming createUser() to create()

hi marek, thanks for the fast response, but the thing is if in case if i using a Multifactor?? … i have to uderstand that if the interface has more than one create, just in that case i have to use create<name>??? is that correct

update
i just create another model and add to factory

declare(strict_types=1);
namespace App\Modules\Admin\User\Model;

use App\Modules\Admin\User\Model\User;
use App\Modules\Admin\User\Model\Prube;

interface UserModelsFactory {
    function createUser(): User;
    function createPrube(): Prube;
}

but it does not work, show the same message, but as you told me with just create method works, but what about multifactory ??

Service '02': Interface App\Modules\Admin\User\Model\UserModelsFactory must have just one non-static method get(). search►

Last edited by alnux (2023-07-29 17:56)

Marek Bartoš
Nette Blogger | 1261
+
+1
-

Never used them so I don't really know, but your example seems correct

I would just go with simple factories and if you want multifactory, then combine them manually https://forum.nette.org/…h-parameters#…

Sniclman
Member | 15
+
+1
-

You must change neon:

services:
	- App\Modules\Admin\User\Model\User
	- App\Modules\Admin\User\Model\Prube
	- App\Modules\Admin\User\Model\UserModelsFactory(
		user: @App\Modules\Admin\User\Model\User
		prube: @App\Modules\Admin\User\Model\Prube
	)

Then this will work

declare(strict_types=1);
namespace App\Modules\Admin\User\Model;

use App\Modules\Admin\User\Model\User;
use App\Modules\Admin\User\Model\Prube;

interface UserModelsFactory {
    function createUser(): User;
    function createPrube(): Prube;
}