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

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);
  • $model = $objectManager->get('\Namespace\Module\Model\Core'); Try to remove "\" – MR.R DNATH Jun 6 '18 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, privacy policy and cookie policy

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