1

This is a weird request but I've done it before and it worked wonders.

I have a need to make a custom PHP file in my sites root folder. I need to give this file access to all of the magento functions inside.

$setup = new Mage_Eav_Model_Entity_Setup(‘core_setup’);

I did this once before successfully when I wrote a script to parse a massive database of trading cards into useful data then has the php script automatically add the cards to the database.

My problem is I did this on a old version of XMAPP that I sadly lost the backup files of when my pc died.

What files do I need to include in a blank php file to have access to the required magento core functions?

2 Answers 2

4

Try this

umask(0);
require 'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Now Magento is all fired upp and ready to go

5
  • Thanks, now I can finally play with my Module install script without having to delete the messed up versions 20 times an hour :) Commented Sep 18, 2013 at 8:49
  • 3
    Setting the store is not mandatory. Commented Sep 18, 2013 at 9:04
  • 1
    AFAIK, It's mandatory in case if you want some write operation to the database.
    – MagePsycho
    Commented Sep 18, 2013 at 13:39
  • 1
    @MagePsycho Only mandatory for updates to the product and category entities.
    – benmarks
    Commented Feb 6, 2014 at 21:56
  • @ChrisMorris this script will not trigger the update workflow. You will want to call Mage_Core_Model_Resource_Setup::applyAllUpdates()
    – benmarks
    Commented Feb 6, 2014 at 21:56
5

I use this one in my /cron/ folder:

<?php

session_start();
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
setlocale(LC_TIME, 'en_US');
umask(0);

$compilerConfig = 'includes/config.php';
if (file_exists($compilerConfig)) {
    include($compilerConfig);
}

$mageFilename = dirname(__FILE__) . '/../app/Mage.php';

require_once $mageFilename;

Mage::setIsDeveloperMode(true);

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

// If you're going to use store view emulation:
Mage::app()->getLocale();

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.