简体   繁体   中英

getRepository fails in foreach loop, Symfony2 Doctrine

I have two entries in my locations table which I am getting with this:

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

Now I loop through everything with 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++;

    }

The expected outcome:

1. Supplier
SupplierX

1. Company
CompanyX

1. Client
ClientX

2. Supplier
SupplierY

2. Company
CompanyZ

2. Client
ClientA

The actual outcome:

1. Supplier
SupplierX

1. Company
CompanyX

1. Client
ClientX

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

In this case, $suppliers->getSupplierName() is empty?

Why is that and why does it work for the first loop? There are only 2 entries in the location table though.

PS: I know it NOT recommended to use encryption as a security feature, but it was specifically wished for!

Any hint appreciated!

EDIT:

I may have found something that causes this. The decrypt is actually this function:

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;
}

I read that you need to close the "stream_get_contents" after each call, how would I do that? fclose cannot work as $string is a blob value from the DB.

Please consider use Doctrine as intended:

$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++;

}

and update the question with excerpt from your database with both locations and related records in 3 other tables.

Ok, I got it to work.

Changed

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

to

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

Further Info from 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.

So I am just reading the stream from the start again. Not sure if this is how it works, so I leave this open for further discussion if there is a better way.

I tried this as well, but since $string is a resource, it does not work:

 $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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM