symfony 如何在Pimcore中正确运行PHP脚本?

oaxa6hgo  于 2023-11-22  发布在  PHP
关注(0)|答案(1)|浏览(153)

我试图运行我的简单pimcore脚本使用bin/console pimcore:run-script script.php命令。代码:

<?php
use \Pimcore\Model\Product;
namespace AppBundle\Command;
$user = \Pimcore\Model\DataObject\Product::getById(88228);
echo $user->getId();

字符串
但似乎没有具有该id的对象(但它存在):

Running script src/AppBundle/Command/script.php
15:02:25 WARNING   [cache] Failed to fetch key "object_88228" ["key" => "object_88228","exception" => Doctrine\DBAL\Exception\ConnectionException^ { …}]
15:02:25 ERROR     [console] Error thrown while running command "pimcore:run-script 'src/AppBundle/Command/script.php'". Message: "Call to a member function getId() on null" ["exception" => Error { …},"command" => "pimcore:run-script 'src/AppBundle/Command/script.php'","message" => "Call to a member function getId() on null"]

In script.php line 5:
                                             
  Call to a member function getId() on null


我做错了什么?谢谢你的帮助!
使用verbose:

Exception trace:
  at /home/ec2-user/my-project/src/AppBundle/Command/script.php:5
 include() at /home/ec2-user/my-project/vendor/pimcore/pimcore/bundles/CoreBundle/Command/RunScriptCommand.php:48
 Pimcore\Bundle\CoreBundle\Command\RunScriptCommand->execute() at /home/ec2-user/my-project/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /home/ec2-user/my-project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:1027
 Symfony\Component\Console\Application->doRunCommand() at /home/ec2-user/my-project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:97
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /home/ec2-user/my-project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:273
 Symfony\Component\Console\Application->doRun() at /home/ec2-user/my-project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:83
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/ec2-user/my-project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:149
 Symfony\Component\Console\Application->run() at /home/ec2-user/my-project/bin/console:36

shstlldc

shstlldc1#

看起来您没有ID为88228的类Product的DataObject。这是完全可以的--可以将其删除或取消发布。
只需在使用对象之前添加检查:

$product = \Pimcore\Model\DataObject\Product::getById(88228);
if ($product instanceof Product) {
    echo $product->getId();
}

字符串

相关问题