kdyby/doctrine EntityManager pouzitie v console
- nord
- Člen | 52
Zdravim mam takyto problem… V presenteroch mi EntityManager z Doctrine funguje bez problemov, ake akonahle sa ho snazim puzit v console, TestCommand tak mi hadze chybu..
[Nette\InvalidStateException]
Service of type Kdyby\Doctrine\EntityManager used in @var annotation at App\Console\TestCommand::$EntityManager not found. Did you register it in configuration file?
Vedel by mi niekto z vas prosim poradit ? Kde treba TestCommand zaregistrovat ? a ako samozrejme ?
pouzitie v TestCommand
/**
* @inject
* @var \Kdyby\Doctrine\EntityManager
*/
public $EntityManager;
Editoval nord (2. 8. 2017 9:06)
- nord
- Člen | 52
Jop.. nejak som sa dopatral ze consola ma problem s @inject
Tak sa snazim pouzit container
$container = $this->getHelper('container')->getContainer();
$container->getByType('Kdyby\Doctrine\EntityManager');
Ale pise mi chybu
[Nette\DI\MissingServiceException]
Service of type Kdyby\Doctrine\EntityManager not found.
Treba samotny EntityManager niekde registrovat ?
Editoval nord (2. 8. 2017 9:57)
- David Matějka
- Moderator | 6445
nemas pro konzoli nejaky specialni bootstrap/config? pokud ti to funguje v presenteru, tak by to melo fungovat i v konzoli
- nord
- Člen | 52
Naozaj neviem kde je problem…
Nemam nikde, teda neviem o nom.. Vlastnorucne som ho urcite nidke nedaval :)
instalujem vsetko cez composer.. Mas nejaky overeny sposob kotry by mi fungoval
v console ? V triede TestCommand aby som cez doctrine mohol pracovat
s tabulkou databazy ? Ked zmazem toto
$container = $this->getHelper('container')->getContainer();
$container->getByType('Kdyby\Doctrine\EntityManager');
z TestCommand ide to ako ma.. Len potrebujem nejake Data vlozit,updatnut a
zmazat z tabulky.. Je toto spravna cesta ? Alebo som uplne mimo.. Mam vytvorenu
entitu, naplnenu cez
$entita->attribute = ‚hodnota‘
Uz to len potrebujem dostat do tabulky.. lenze neviem ako..
resp. nejak commitnut
Editoval nord (2. 8. 2017 10:11)
- David Matějka
- Moderator | 6445
a jak teda pracujes s tou konzoli? mas nekde nejake console.php? nebo jak ji spoustis?
- Hug0
- Člen | 35
php www/index.php
je deprecated, koukni na Best Practice for Symfony Console in Nette.
Místo inject bych si EntityManager předal konstruktorem, ale na funkčnost by
to nemělo mít vliv. Máš Kdyby\Doctrine registrovanou jako extension?
- nord
- Člen | 52
Config.
parameters:
application:
errorPresenter: Error
mapping:
*: App\*Module\Presenters\*Presenter
session:
expiration: 14 days
services:
router: App\RouterFactory::createRouter
extensions:
console: Kdyby\Console\DI\ConsoleExtension
events: Kdyby\Events\DI\EventsExtension
annotations: Kdyby\Annotations\DI\AnnotationsExtension
doctrine: Kdyby\Doctrine\DI\OrmExtension
console:
commands:
- App\Console\TestCommand
doctrine:
user: root
password:
dbname: name
metadata:
App: %appDir%
TestCommand
<?php
namespace App\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Model\Entities\Book;
/**
* Class TestCommand
* @package App\Console
*/
class TestCommand extends Command
{
private $parser;
protected function configure()
{
$this->setName('cron:update-books')
->setDescription('Updates ... database');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getHelper('container')->getContainer();
$container->getByType('Kdyby\Doctrine\EntityManager');
$i = 1;
while(1) {
if ($i >= 10) {
break;
}
$book = new Book();
$i++;
}
$output->writeLn('End of parsing');
return 0;
}
}
Chyba v console
[Nette\DI\MissingServiceException]
Service of type Kdyby\Doctrine\EntityManager not found.
error v logoch
[2017-08-02 12-52-22] Nette\DI\MissingServiceException: Service of type Kdyby\Doctrine\EntityManager not found. in /var/www/html/project/vendor/nette/di/src/DI/Container.php:209 @ CLI (PID: 18094): www/index.php cron:update-books @@ exception--2017-08-02--12-52--d1717938d7.html
Editoval nord (2. 8. 2017 12:53)