Skip to content

Instantly share code, notes, and snippets.

@doMynation
Last active September 30, 2015 15:51
Show Gist options
  • Save doMynation/a42dedc8c16587586f6a to your computer and use it in GitHub Desktop.
Save doMynation/a42dedc8c16587586f6a to your computer and use it in GitHub Desktop.
Filtering a PersistentCollection and then persist the changes.
<?php
// ----------- This is a method part of the Order entity, located in Order.php ---------------
/**
* Rejects all attributes with the provided code from the order.
*
* @param string $code
*/
public function rejectAttribute($code)
{
$this->attributes = $this->attributes->filter(function ($attribute) use ($code) {
return $attribute->getCode() !== $code;
});
// Dumping $this->attributes here show the expected result
}
// ------------- Example usage ----------------- //
$order->rejectAttribute("shippingMethod");
$em->persist($order);
$em->flush();
// Refetching the order from the ORM and dumping the attributes shows nothing was persisted and the collection remain intact.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment