getRepository 在 foreach 循环中失败,Symfony2 Doctrine

getRepository fails in foreach loop, Symfony2 Doctrine

我在我的位置 table 中有两个条目,我得到的是:

$em = $this->getDoctrine()->getManager($this->getUser()->getDbuser());
$locations = $em->getRepository('AppBundle:Location')->findAll();

现在我用 foreach 遍历所有内容:

$i = 1;
foreach ($locations as $location){

        $clientId = $location->getClient()->getId();
        $supplierId = $location->getSupplier()->getId();
        $companyId = $location->getCompany()->getId();

         echo $i.". Supplier<br />";
        $suppliers = $em->getRepository('AppBundle:Supplier')->find($supplierId);
        echo $this->get('global_functions')->decrypt($suppliers->getSupplierName())."<br /><br />";

        echo $i.". Company<br />";
        $companies = $em->getRepository('AppBundle:Company')->find($companyId);
        echo $this->get('global_functions')->decrypt($companies->getCompanyName())."<br /><br />";

        echo $i.". Client<br />";
        $clients = $em->getRepository('AppBundle:Client')->find($clientId);
        echo $this->get('global_functions')->decrypt($clients->getClientName())."<br /><br />";

       $i++;

    }

预期结果:

1. Supplier
SupplierX

1. Company
CompanyX

1. Client
ClientX

2. Supplier
SupplierY

2. Company
CompanyZ

2. Client
ClientA

实际结果:

1. Supplier
SupplierX

1. Company
CompanyX

1. Client
ClientX

2. Supplier
Warning: mdecrypt_generic(): An empty string was passed 

这样的话,$suppliers->getSupplierName()是空的?

为什么会这样,为什么它适用于第一个循环?不过 table 位置只有 2 个条目。

PS:我知道不建议使用加密作为安全功能,但特别希望如此!

感谢任何提示!

编辑:

我可能找到了导致此问题的原因。解密其实就是这个函数:

public function decrypt($string) {
    $decrypter = new TripleDES(CRYPT_DES_MODE_ECB);
    $decrypter->setKey($this->container->getParameter('secure_token'));
    $decrytped_string = $decrypter->decrypt(stream_get_contents($string));

    return $decrytped_string;
}

我了解到您需要在每次通话后关闭 "stream_get_contents",我该怎么做? fclose 无法工作,因为 $string 是来自数据库的 blob 值。

请考虑按预期使用 Doctrine:

$i = 1;
foreach ($locations as $location){

    $client = $location->getClient();
    $supplier = $location->getSupplier();
    $company = $location->getCompany();

    echo $i.". Supplier<br />";
    echo $this->get('global_functions')->decrypt($supplier->getName())."<br /><br />";

    echo $i.". Company<br />";
    echo $this->get('global_functions')->decrypt($company->getName())."<br /><br />";

    echo $i.". Client<br />";
    echo $this->get('global_functions')->decrypt($client->getName())."<br /><br />";

   $i++;

}

并使用从您的数据库中摘录的位置和其他 3 个表中的相关记录更新问题。

好的,我让它工作了。

已更改

  $decrypted_string = $decrypter->decrypt(stream_get_contents($string));

  $decrypted_string = $decrypter->decrypt(stream_get_contents($string,-1,0));

来自 php.net 的更多信息:

string stream_get_contents ( resource $handle [, int $maxlength = -1 [, int $offset = -1 ]] )
offset (integer) Seek to the specified offset before reading. If this number is negative, no seeking will occur and reading will start from the current position.

所以我只是从头开始阅读流。不确定这是否是它的工作原理,所以如果有更好的方法,我将此留待进一步讨论。

我也试过了,但是由于 $string 是一个资源,所以它不起作用:

 $stream = fopen('php://temp','r+');

    $decrypter = new AES(CRYPT_DES_MODE_ECB);
    $decrypter->setKey($this->container->getParameter('secure_token'));

    fwrite($stream, stream_get_contents($string));
    rewind($stream);
    $decrypted_string = $decrypter->decrypt(stream_get_contents($stream));
    fclose($stream);

    return $decrypted_string;