I am doing insert multiple rows in symfony. My code inserts very slowly. Does anyone have any ideas that won't help me! Thank you !
$manager = $this->getContainer()->get('doctrine')->getEntityManager();
// data : object
foreach ($data as $value) {
$entitty = $stkModel->getRepository()->findOneBy(['phone' => $value->getPhone()]);
if ($entity) {
$stk = $stkModel->getEntity();
$stk->setName('mina');
$stk->setStatus(1);
$manager->persist($stk);
$manager->flush();
}
if ($stk) {
$entitty = $titleModel->getRepository()->findOneBy(['stkId' => $stk->getId()]);
$title = $titleModel->getEntity();
$title->setName('dev');
$title->setOrder(2);
$manager->persist($title);
$manager->flush();
}
// Here I take $title->getId();
}
First, you need to call persist only at entities that are created with new. But this will not give you speed performance.
The flush slows the application down every time you use it, because here the query will be send to the database. To speed up your application you have to minimize the use of flush. In your case, just do one flush after you updated your entites.
$manager = $this->getContainer()->get('doctrine')->getEntityManager();
// data : object
foreach ($data as $value) {
$entitty = $stkModel->getRepository()->findOneBy(['phone' => $value->getPhone()]);
if ($entity) {
$stk = $stkModel->getEntity();
$stk->setName('mina');
$stk->setStatus(1);
// $manager->persist($stk); <-- Don't need persist here because your object is coming out from the entity manager
}
if ($stk) {
$entitty = $titleModel->getRepository()->findOneBy(['stkId' => $stk->getId()]);
$title = $titleModel->getEntity();
$title->setName('dev');
$title->setOrder(2);
// $manager->persist($stk); <-- same as mention above
}
}
// call flush once at the end
$manager->flush();
In some cases, you need to call flush in every loop in a foreach. That might happen if you need to query it. But in your case you just search for a id, which didn't change anyway.
Related
I have multiple loops like :
$bets = $this->em->getRepository('AppBundle:Bet')->getBetsForMatch($match_id);
foreach ($bets as $key => $bet) {
$devices = $this->em->getRepository('AppBundle:Device')->findBy(array('user' => $bets->getUser()));
foreach ($devices as $key => $device) {
//HERE I SEND A PUSH NOTIFICATION
if($this->rms_push->send($message)){
$device->getUser()->setBadge($device->getUser()->getBadge() + 1);
$this->em->flush();
}
}
}
So, I get all bets for a match, for each bet I get all devices saved for the user, and after that I need to update my user with : $device->getUser()->setBadge($device->getUser()->getBadge() + 1);
For now, I flush each time but I think there is a better way, ideas ?
You need only one flush, out of your loop:
foreach ($bets as $key => $bet) {
$devices = $this->em->getRepository('AppBundle:Device')->findBy(array('user' => $bets->getUser()));
foreach ($devices as $key => $device) {
//HERE I SEND A PUSH NOTIFICATION
if($this->rms_push->send($message)){
$device->getUser()->setBadge($device->getUser()->getBadge() + 1);
}
}
}
$this->em->flush();
Calling $this->_em->persist($obj) involves to create a new entry.
If you need to create or update depending on the entry exists or not, look at EntityManager::merge .
To preserve memory usage for large number of entries, look at batch processing.
Note SensioLabs insight (PHP source code quality analysis) raises a warning if your code calls EntityManager::flush inside a loop.
I've been struggling with an annoying issue for a while. I'm trying to create Notification entities associated to InventoryItems that expire.
These InventoryItems are generated automatically for users, but users can edit them and set expiry date on them individually. Upon saving, if an InventoryItem has an expiry date, a Notification entity is generated and associated to it. So these Notification entities are created when an entity is updated and hence onPersist event is not going to work.
Everything seems to work fine and Notifications are generated upon saving InventoryItems as expected. Only problem is, when a Notification is created for the first time, even though it's saved properly, changes to the InventoryItem are not saved. That is a Notification with correct expiry date is created, but the expiry date is not saved on the InventoryItem.
Here's my onFlush code:
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($entity instanceof NotificableInterface) {
if ($entity->generatesNotification()){
$notification = $this->notificationManager->generateNotificationForEntity($entity) ;
if ( $notification ) {
$uow->persist($notification) ;
}
$entity->setNotification($notification) ;
$uow->persist($entity);
$uow->computeChangeSets();
}
}
}
}
The problem only occurs the first time a Notification is associated to an entity, i.e. the first time an expiry date is set on an InventoryItem. In later instances when expiry date is updated, the update is reflected correctly on both the Notification and InventoryItem.
Any ideas or suggestions would be appreciated.
Thanks
You need to call computeChangeset specifically on your newly created or updated entity. Just calling computeChangeSets is not enough.
$metaData = $em->getClassMetadata('Your\NameSpace\Entity\YourNotificationEntity');
$uow->computeChangeSet($metaData, $notification);
Thanks Richard. You pointed me to the right direction. I needed to recompute the change set on the parent entity (InventoryItem) to get things working properly. Additionally I had to call computeChangeSets on the unit of work to get rid of the invalid parameter number error (such as the one explained symfony2 + doctrine: modify a child entity on `onFlush`: "Invalid parameter number: number of bound variables does not match number of tokens")
Note I ended up also removing:
if ( $notification ) {
$uow->persist($notification) ;
}
Which never made sense, since I had set cascade persist on the association in my entity and should have cascaded down automatically.
My final solution is:
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($entity instanceof NotificableInterface) {
if ($entity->generatesNotification()){
$notification = $this->notificationManager->generateNotificationForEntity($entity) ;
$entity->setNotification($notification) ;
// if ( $notification ) {
// $uow->persist($notification) ;
// }
$metaData = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($metaData, $entity);
$uow->computeChangeSets();
}
}
}
}
I am trying to remove galleryhasmedia from gallery.
However gallery entity doesn't have removegalleryhasmedia or something.
so I did a clumsy way, but it doesnt work.
$em = $this->getDoctrine()->getManager();
$firstGhmArray = $gallery->getGalleryHasMedias();
echo count($gallery->getGalleryHasMedias()) // before count
$afterGhmArray = array();
foreach ($firstGhmArray as $ghm){
if ($ghm->getId() == $id){ // id is the target id to delete
//delete
}
else {
array_push($afterGhmArray , $ghm);
}
$gallery->setGalleryHasMedias($afterGhmArray);
}
echo count($gallery->getGalleryHasMedias()) // after count
$em->persist($gallery);
$em->flush();
I think if galleryHasMedias are normal array collection.
I can delete the element with this procedure.
I need to do something more for galleryhasmedia??
You can override the Gallery entity and add this function to it :
public function clearGalleryHasMedias()
{
$this->galleryHasMedias->clear();
}
galleryHasMedias field is an ArrayCollection which can be cleared using the clear method. Its weird tho that setting an empty array doesn't clear work but i guess my solution is worth the shot.
I have a reletivly simple change logging class that stores the date, an integer to indicate the type of change and 2 varchar(50)s that hold the old and new data for the change.
I can create and populate an instance of the class but when I come to flush it I get an "Error: Maximum function nesting level of '200' reached, aborting!" error.
I've read about the Xdebug issue and configured the max nests up to 200 but as you can see this isn't enough. The save process should be very simple and there should be no need for so many nested functions, so increasing it further will just hide the problem, whatever it is. I have far more complicated classes in this app that persisit and flush without a problem.
The issue is always at
NormalizerFormatter ->normalize ()
in app/cache/dev/classes.php at line 4912 -
Having looked at this a bit more I think the issue may be that the change instance is created and saved during the preUpdate event of another class:
public function preUpdate(LifecycleEventArgs $eventArgs)
{
$entity = $eventArgs->getEntity();
if ($entity instanceof Property) {
$entityManager = $eventArgs->getEntityManager();
$changeArray = $eventArgs->getEntityChangeSet();
foreach ($changeArray as $field => $values) {
$eventType = "";
switch ($field) {
case 'price' :
$eventType = PropertyEvent::EVENTTYPE_PRICE;
BREAK;
case 'status' :
$eventType = PropertyEvent::EVENTTYPE_STATUS;
BREAK;
}
if ($eventType != "") {
$event = new PropertyEvent($entity->getID(), $eventType, $values[0], $values[1]);
$entityManager->persist($event);
$entityManager->flush();
}
}
$entity->setUpdatedDate();
}
}
Why would that be an issue?
Doctrine only has one lifecycle event process so regardless of the entity you're using adding a flush within a lifecycle event will send you back round the loop and into your event handler again... and again ... and...
Have a look at this blog post
http://mightyuhu.github.io/blog/2012/03/27/doctrine2-event-listener-persisting-a-new-entity-in-onflush/
Basically the answer is to use
$eventManager -> removeEventListener('preUpdate', $this);
to remove the event. Then create the new entity, persist, flush and finally re-attach the event you are in.
$eventManager -> addEventListener('preUpdate', $this);
I think your issue is the $entity->setUpdatedDate(); call, which probably calls another update event and re-calls your handler.
I have a D7 website where users can make content (obviously...). So every node has it's own author. Every author is a member of an organization. But he can be a member of more then one organization. So far the facts.
I would like to create a view where the content is filtered on Author. Very easy, set the relation of the view on "Content's Author" and select the current user as filter.
But what I would like is to filter on the author's organization. So in fact it's a nested relation. Filter the nodes on the current logged in user (that's easy), but how can I filter on the current logged in user's organization?
Ok, the panels didn't work out, so I wrote my own hook :-)
function mymodule_views_pre_view (&$view, &$display_id, &$args) {
// Only execute this script when the view 'fiches_my_thema' is called
if ('fiches_my_thema' == $view->name) {
// Get users thema
global $user;
$userRoles = $user->roles;
$user_themas = array();
// Filter roles so you end up with the "Thema's".
foreach ($userRoles as $key) {
if(strpos($key,'edacteur')) {
$key = str_replace('Redacteur - ','', $key);
$key = str_replace('Eindredacteur - ','', $key);
$user_themas[] = $key;
}
}
// Resolve tid
$terms = taxonomy_get_tree(5);
$allRoles = array();
$arguments = array();
// Assign the 'tid' to a variable
foreach ($terms as $key) {
$singleRoles = $key->name;
$allRoles[] = $singleRoles;
if(in_array($singleRoles, $user_themas)) {
$arguments[] = $key->tid;
}
}
// Only when the arguments are NOT empty, set the arguments
if(!empty($arguments)) {
$finalArguments = implode("+", $arguments);
$args[] = "$finalArguments";
$view->set_arguments($args);
}
}
}