Typo 3 Scheduler任务:如果没有该类的记录,阻止任务访问类库?

时间:2017-11-17 08:33:26

标签: php typo3 task scheduler

所以基本上我正在使用一个Scheduler任务,我首先在每个类库中创建一个对象并在每个上使用函数findAll():

/** @var CustomerRepository $apprep2 */
$apprep2 = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\ApplianceRepository::class);
/** @var Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$apprep2->setDefaultQuerySettings($querySettings);
$appliances = $apprep2->findAll();

然后我在foreach循环中使用每个存储库对象来访问存储库中每个记录的特定属性(将它们写入文件 - 但这是另一个故事),如下所示:

foreach($appliances as $appliance)
{
    if($appliance->getKundeuid() == $kunde->getUid())
    {

        $name = $appliance->getIpv4intern();
        $address = $appliance->getIpv4extern();
        $file = '/etc/icinga2/conf.d/hosts/'.$kunde->getName().'/Appliance_'. $appliance->getIpv4intern().'_'.$kunde->getKundennummer() . '.conf';

        $code_a = 'object Host "';
        $code_b = '" {
        import "generic-host"
        address = "';
        $code_c = '"
        vars.notification["mail"] = {
            groups = [ "icingaadmins" ]
        }
        }';
        $fp = fopen("{$file}", 'wb');
        fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
        fclose($fp);
    }
}

我的问题是:我没有任何记录让我们说“家电”Typo 3这样的错误会让我误以为是因为没有它想要访问的“设备”记录。

Oops, an error occurred!
Call to a member function getIpv4intern() on null

使用if条件,如:

if($appliances != null){...}

...仍然让任务试图调用“getIpv4intern()”......

如果记录为空,我该怎么做才能阻止使用foreach循环(或我的代码中试图访问记录的任何其他部分?)

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题:而不是使用

if($appliances != null){...}

我只是检查了存储库中的记录是否为空:

foreach($appliances as $appliance)
{
    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(!empty($appliance));

    if(!empty($appliance)){


    if($appliance->getKundeuid() == $kunde->getUid())
    {
        $name = $appliance->getIpv4intern();
        $address = $appliance->getIpv4extern();
        $file = '/etc/icinga2/conf.d/hosts/'.$kunde->getName().'/Appliance_'. $appliance->getIpv4intern().'_'.$kunde->getKundennummer() . '.conf';

        $code_a = 'object Host "';
        $code_b = '" {
        import "generic-host"
        address = "';
        $code_c = '"
        vars.notification["mail"] = {
            groups = [ "icingaadmins" ]
        }
        }';
        $fp = fopen("{$file}", 'wb');
        fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
        fclose($fp);
    }
}
}