我正在尝试使用
bin/console pimcore:run-script script.php
命令运行我的简单 pimcore 脚本。
代码:
<?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
我做错了什么?感谢您的帮助!
详细:
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
看起来您没有 ID 为 88228 的 Product 类的 DataObject。 绝对没问题——可以删除或取消发布。
只需在使用对象之前添加检查即可:
$product = \Pimcore\Model\DataObject\Product::getById(88228);
if ($productinstanceof Product) {
echo $product->getId();
}