<p>I'm assuming you're using a multi-environment config.  From wherever your plugin is bootstrapping <code>yiic</code>, you need to manually set the <a href="https://craftcms.com/docs/php-constants#craft-environment" rel="nofollow"><code>CRAFT_ENVIRONMENT</code></a> PHP constant to the environment you want <code>yiic</code> to load in the config files.</p>

CC BY-SA 3.0
2016-05-24T16:43:15.953Z
2016-05-24T16:43:15.953Z
Brad Bell
<p>I'm trying to create a command line function, in my plugin, to import some data into entries. The problem is that the <code>Craft\EntriesService-&gt;saveEntry()</code> method seems to call the <code>Craft\TemplatesService-&gt;renderObjectTemplate()</code> method which uses the <code>SERVER_NAME</code>constant. Obviously there is no <code>SERVER_NAME</code> because the application is being run from the command line. </p>

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

<p>I will include my stack trace here as well:</p>

<pre><code>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-&gt;handleError()
1 myproject/craft/app/framework/web/CHttpRequest.php(352): Craft\ConsoleApp-&gt;handleError()
2 myproject/craft/app/services/ConfigService.php(420): Craft\HttpRequestService-&gt;getHostInfo()
3 myproject/craft/app/helpers/UrlHelper.php(422): Craft\ConfigService-&gt;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-&gt;getGlobals()
7 myproject/craft/app/vendor/twig/twig/lib/Twig/Environment.php(1200): Craft\TwigEnvironment-&gt;initGlobals()
8 myproject/craft/app/vendor/twig/twig/lib/Twig/Environment.php(1217): Craft\TwigEnvironment-&gt;getGlobals()
9 myproject/craft/app/vendor/twig/twig/lib/Twig/Template.php(355): Craft\TwigEnvironment-&gt;mergeGlobals()
10 myproject/craft/app/vendor/twig/twig/lib/Twig/Template.php(366): __TwigTemplate_5f77fd774cac1595e134b05c66db80e7998372598504ff29b7e2487f820a3d8c-&gt;display()
11 myproject/craft/app/services/TemplatesService.php(336): __TwigTemplate_5f77fd774cac1595e134b05c66db80e7998372598504ff29b7e2487f820a3d8c-&gt;render()
12 myproject/craft/app/services/EntriesService.php(157): Craft\TemplatesService-&gt;renderObjectTemplate()
13 myproject/craft/plugins/myplugin/services/Myproject_SomeService.php(312): Craft\EntriesService-&gt;saveEntry()
14 myproject/craft/plugins/myplugin/services/Myproject_SomeService.php(280): Craft\Myproject_SomeService-&gt;update()
15 myproject/craft/plugins/myplugin/consolecommands/ImportEmployeeCommand.php(24): Craft\Myproject_SomeService-&gt;createOrUpdate()
16 unknown(0): Craft\ImportCommand-&gt;actionImport()
17 myproject/craft/app/framework/console/CConsoleCommand.php(172): ReflectionMethod-&gt;invokeArgs()
18 myproject/craft/app/framework/console/CConsoleCommandRunner.php(71): Craft\ImportCommand-&gt;run()
19 myproject/craft/app/framework/console/CConsoleApplication.php(92): Craft\ConsoleCommandRunner-&gt;run()
20 myproject/craft/app/framework/base/CApplication.php(185): Craft\ConsoleApp-&gt;processRequest()
21 myproject/craft/app/etc/console/yiic.php(81): Craft\ConsoleApp-&gt;run()
22 myproject/craft/app/etc/console/yiic(4): require_once()
</code></pre>

CC BY-SA 3.0
2016-05-24T16:10:15.800Z
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](http://craftcms.stackexchange.com/a/4467/1016). (His example is moving structures but you can do whatever you want within the code block.)
RitterKnight
<plugin-development><plugins>
Entry not saving through command line
# Entry not saving through command line

(Asked by: username_0 on 2016-05-24)

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()

**Tags:** plugin-development, plugins

> **Comments:**
> **username_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](http://craftcms.stackexchange.com/a/4467/1016). (His example is moving structures but you can do whatever you want within the code block.)  

---

## 1 answers

### Answer by username_2 on 2016-05-24. Score: 1 | Accepted answer

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`](https://craftcms.com/docs/php-constants#craft-environment) PHP constant to the environment you want `yiic` to load in the config files.


---

