2

I'm trying to create a command line function, in my plugin, to import some data into entries. The problem is that the Craft\EntriesService->saveEntry() method seems to call the Craft\TemplatesService->renderObjectTemplate() method which uses the SERVER_NAMEconstant. Obviously there is no SERVER_NAME because the application is being run from the command line.

Is there a way to manually set this, or can this be bypassed in any other way?

I will include my stack trace here as well:

PHP Error[8]: Undefined index: SERVER_NAME in file myproject/craft/app/framework/web/CHttpRequest.php at line 352
0 myproject/craft/app/etc/console/ConsoleApp.php(252): Craft\ConsoleApp->handleError()
1 myproject/craft/app/framework/web/CHttpRequest.php(352): Craft\ConsoleApp->handleError()
2 myproject/craft/app/services/ConfigService.php(420): Craft\HttpRequestService->getHostInfo()
3 myproject/craft/app/helpers/UrlHelper.php(422): Craft\ConfigService->usePathInfo()
4 myproject/craft/app/helpers/UrlHelper.php(196): _getUrl()
5 myproject/craft/app/etc/templating/twigextensions/CraftTwigExtension.php(493): getUrl()
6 myproject/craft/app/vendor/twig/twig/lib/Twig/Environment.php(1276): Craft\CraftTwigExtension->getGlobals()
7 myproject/craft/app/vendor/twig/twig/lib/Twig/Environment.php(1200): Craft\TwigEnvironment->initGlobals()
8 myproject/craft/app/vendor/twig/twig/lib/Twig/Environment.php(1217): Craft\TwigEnvironment->getGlobals()
9 myproject/craft/app/vendor/twig/twig/lib/Twig/Template.php(355): Craft\TwigEnvironment->mergeGlobals()
10 myproject/craft/app/vendor/twig/twig/lib/Twig/Template.php(366): __TwigTemplate_5f77fd774cac1595e134b05c66db80e7998372598504ff29b7e2487f820a3d8c->display()
11 myproject/craft/app/services/TemplatesService.php(336): __TwigTemplate_5f77fd774cac1595e134b05c66db80e7998372598504ff29b7e2487f820a3d8c->render()
12 myproject/craft/app/services/EntriesService.php(157): Craft\TemplatesService->renderObjectTemplate()
13 myproject/craft/plugins/myplugin/services/Myproject_SomeService.php(312): Craft\EntriesService->saveEntry()
14 myproject/craft/plugins/myplugin/services/Myproject_SomeService.php(280): Craft\Myproject_SomeService->update()
15 myproject/craft/plugins/myplugin/consolecommands/ImportEmployeeCommand.php(24): Craft\Myproject_SomeService->createOrUpdate()
16 unknown(0): Craft\ImportCommand->actionImport()
17 myproject/craft/app/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs()
18 myproject/craft/app/framework/console/CConsoleCommandRunner.php(71): Craft\ImportCommand->run()
19 myproject/craft/app/framework/console/CConsoleApplication.php(92): Craft\ConsoleCommandRunner->run()
20 myproject/craft/app/framework/base/CApplication.php(185): Craft\ConsoleApp->processRequest()
21 myproject/craft/app/etc/console/yiic.php(81): Craft\ConsoleApp->run()
22 myproject/craft/app/etc/console/yiic(4): require_once()
1
  • Stupid question: is your plugin "installed?" How are you calling/bootstrapping it? If it's a one-off import, instead of throwing the logic into a plugin, you might try what Brandon recommends here. (His example is moving structures but you can do whatever you want within the code block.) May 24, 2016 at 16:10

1 Answer 1

1

I'm assuming you're using a multi-environment config. From wherever your plugin is bootstrapping yiic, you need to manually set the CRAFT_ENVIRONMENT PHP constant to the environment you want yiic to load in the config files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.