0

I have a custom extension model like this:

app/code/Namespace/Module/Model/Core.php

<?php
namespace Namespace\Module\Model;
class Core
{
  public function save($data){
    ....
  }
}

I want to create a PHP script inside the same Magento project folder like this:

script/test.php

$model = \app\code\Namespace\Module\Model\Core; //roughly like this
$data = 'test';
$model->save($data);

is it even possible to do this?

2 Answers 2

2

Try this code:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$model = $objectManager->get('\Namespace\Module\Model\Core');
$data = 'test';
$model->save($data);
1
  • $model = $objectManager->get('\Namespace\Module\Model\Core'); Try to remove "\" Commented Jun 6, 2018 at 8:11
0

Try Below Code ................

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);


use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$searchItems = $objectManager->create('VendorName\Modulename\Model\Core')->functionName(); // functionName inside Core.php file
echo '<pre>';var_dump($searchItems);

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.