autowiring 2 databases and getting ArticleManager to work properly

jdan1131x
Member | 41
+
0
-

I really appreciate Latte and Nette.

I need an example of ArticleManager.php showing
everything from the namespace declaration to the end
of the constructor, and common.neon too…

WHEN

local.neon has two databases, not one.

I have searched through a lot great documentation but I just
cant wire my brain to make ArticleManager work with two DB's
in local.neon if both are autowired.

i can get second UserManager to work without autowire…
with services:
App\Model\UserManager(@database.user.connection)

but am struggline to get UserManager to work if i set autowired: true in local.neon
tracy err: Multiple services of type Nette\Database\Explorer found: database.main.context, database.user.context (needed by $db in __construct())

because the tracy err leads to nothing similar when i google it..
thanks for any assistance in advance,
James

Marek Bartoš
Nette Blogger | 1275
+
0
-

if both are autowired

Two identical classes cannot be autowired at the same time. nette/di simply don't know which one do you want.

You can either extend the class and autowire by unique extended class

class Conn1 extends Connection{}
class Conn2 extends Connection{}
class UserManager{
  function __construct(Conn1 $conn1, Conn2 $conn2){}
}
services:
  database.user.connection:
    type: Conn1
  database.another.connection:
    type: Conn2

Or autowire array of services

class UserManager{
	/**
	 * @param Connection[] $conns
	 */
	function __construct(array $conns){}
}

Or configure service manually, without autowiring App\Model\UserManager(@database.user.connection)

Last edited by Mabar (2021-04-05 21:42)

jdan1131x
Member | 41
+
0
-

Thanks,
trying first solution, seems to work, causes some downstream construct mods,
which were going well until tracy threw err:

Unable to create service ‘database.main.connection’, value returned by factory is not App\Model\Conn1 type.

but common.neon file says otherwise->
database.main.connection:
type: App\Model\Conn1
database.user.connection:
type: App\Model\Conn2

so I think i will just skip autowiring the second db for now…
appreciate the help very much, I do understand now about what you said
that given two Explorer objects → nette DI cant decide…
James

Marek Bartoš
Nette Blogger | 1275
+
0
-

My bad, sorry. Instead of type use factory

jdan1131x
Member | 41
+
0
-

seemed to fix some things, but then ran into diff problem reports from tracy so
for now, just using connection object in new auth classes,
i'm comfortable with sql queries … and full auth is working with db,
so on to making that more robust before next features..

priority is get it working so can move to next step.
elegance, optimization, more autowiring down the road…

thanks again!
James

jdan1131x
Member | 41
+
0
-

Tracy Throws Error:
Service of type App\Model\ArticleManager: Multiple services of type Nette\Database\Explorer found: database.main.context, database.website.context (needed by $db in __construct())

i've tried everything above again, twice. and some derivations that seemed to help
but ultimately failed. even $db in construct was tested with an array name which it would not take…

configuring without autowire works, but delivers a Connection object not the preferred Explorer object.
i'll work on this as i have time, post solution if found. I'll probably try making my own explorer object
and passing it in as a service next…first try on that failed, but i still have 99 tries to go :-)

thanks very much,
James