本文整理汇总了PHP中Doctrine\ORM\Event\OnFlushEventArgs::getEntityManager方法的典型用法代码示例。如果您正苦于以下问题:PHP OnFlushEventArgs::getEntityManager方法的具体用法?PHP OnFlushEventArgs::getEntityManager怎么用?PHP OnFlushEventArgs::getEntityManager使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\ORM\Event\OnFlushEventArgs
的用法示例。
在下文中一共展示了OnFlushEventArgs::getEntityManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onFlush
/**
* Change cssHash of views when a widget is updated or deleted.
*
* @param OnFlushEventArgs $args
*/
public function onFlush(OnFlushEventArgs $args)
{
$this->em = $args->getEntityManager();
$this->uow = $this->em->getUnitOfWork();
$this->widgetRepo = $this->em->getRepository('Victoire\\Bundle\\WidgetBundle\\Entity\\Widget');
$this->viewRepo = $this->em->getRepository('Victoire\\Bundle\\CoreBundle\\Entity\\View');
$updatedEntities = $this->uow->getScheduledEntityUpdates();
$deletedEntities = $this->uow->getScheduledEntityDeletions();
//Update View's CSS and inheritors of updated and deleted widgets
foreach (array_merge($updatedEntities, $deletedEntities) as $entity) {
if (!$entity instanceof Widget) {
continue;
}
$view = $entity->getView();
$this->updateViewCss($view);
$this->updateTemplateInheritorsCss($view);
}
//Remove CSS of deleted View and update its inheritors
foreach ($deletedEntities as $entity) {
if (!$entity instanceof View) {
continue;
}
$this->viewCssBuilder->removeCssFile($entity->getCssHash());
$this->updateTemplateInheritorsCss($entity);
}
//Update CSS of updated View and its inheritors
foreach ($updatedEntities as $entity) {
if (!$entity instanceof View) {
continue;
}
$this->updateViewCss($entity);
$this->updateTemplateInheritorsCss($entity);
}
}
示例2: onFlush
/**
* Collect activities changes
*
* @param OnFlushEventArgs $args
*/
public function onFlush(OnFlushEventArgs $args)
{
$entitiesToDelete = $args->getEntityManager()->getUnitOfWork()->getScheduledEntityDeletions();
$entitiesToUpdate = $args->getEntityManager()->getUnitOfWork()->getScheduledEntityUpdates();
if (!empty($entitiesToDelete) || !empty($entitiesToUpdate)) {
foreach ($entitiesToDelete as $entity) {
$class = $this->doctrineHelper->getEntityClass($entity);
$id = $this->doctrineHelper->getSingleEntityIdentifier($entity);
$key = $class . '_' . $id;
if ($this->activityContactProvider->isSupportedEntity($class) && !isset($this->deletedEntities[$key])) {
$targets = $entity->getActivityTargetEntities();
$targetsInfo = [];
foreach ($targets as $target) {
$targetsInfo[] = ['class' => $this->doctrineHelper->getEntityClass($target), 'id' => $this->doctrineHelper->getSingleEntityIdentifier($target), 'direction' => $this->activityContactProvider->getActivityDirection($entity, $target)];
}
$this->deletedEntities[$key] = ['class' => $class, 'id' => $id, 'contactDate' => $this->activityContactProvider->getActivityDate($entity), 'targets' => $targetsInfo];
}
}
foreach ($entitiesToUpdate as $entity) {
$class = $this->doctrineHelper->getEntityClass($entity);
$id = $this->doctrineHelper->getSingleEntityIdentifier($entity);
$key = $class . '_' . $id;
if ($this->activityContactProvider->isSupportedEntity($class) && !isset($this->updatedEntities[$key])) {
$changes = $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($entity);
$isDirectionChanged = $this->activityContactProvider->getActivityDirectionProvider($entity)->isDirectionChanged($changes);
$targets = $entity->getActivityTargetEntities();
$targetsInfo = [];
foreach ($targets as $target) {
$targetsInfo[] = ['class' => $this->doctrineHelper->getEntityClass($target), 'id' => $this->doctrineHelper->getSingleEntityIdentifier($target), 'direction' => $this->activityContactProvider->getActivityDirection($entity, $target), 'is_direction_changed' => $isDirectionChanged];
}
$this->updatedEntities[$key] = ['class' => $class, 'id' => $id, 'contactDate' => $this->activityContactProvider->getActivityDate($entity), 'targets' => $targetsInfo];
}
}
}
}
示例3: onFlush
public function onFlush(OnFlushEventArgs $args)
{
$unitOfWork = $args->getEntityManager()->getUnitOfWork();
foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {
if (is_callable(array($entity, 'setDeleted'))) {
$entity->setDeleted(true);
$unitOfWork->propertyChanged($entity, 'deleted', false, true);
$unitOfWork->scheduleExtraUpdate($entity, array('deleted' => array(false, true)));
$args->getEntityManager()->persist($entity);
}
}
}
示例4: onFlush
public function onFlush(OnFlushEventArgs $eventArgs)
{
/* @var $entity SoftDeletable */
$entityManager = $eventArgs->getEntityManager();
$unitOfWork = $eventArgs->getEntityManager()->getUnitOfWork();
foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {
if ($entity instanceof SoftDeletable) {
$meta = $entityManager->getClassMetadata(get_class($entity));
$entity->onDelete();
$unitOfWork->computeChangeSet($meta, $entity);
$entityManager->persist($entity);
$unitOfWork->scheduleExtraUpdate($entity, $unitOfWork->getEntityChangeSet($entity));
}
}
}
示例5: onFlush
/**
* @link http://docs.doctrine-project.org/en/latest/reference/events.html
*/
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$primitivelyChangedEntities = new SplObjectStorage();
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if (property_exists($entity, 'created_by')) {
$entity->created_by = $this->identifier;
$primitivelyChangedEntities->attach($entity);
}
if (property_exists($entity, 'updated_by')) {
$entity->updated_by = $this->identifier;
$primitivelyChangedEntities->attach($entity);
}
}
foreach ($uow->getScheduledEntityUpdates() as $entity) {
if (property_exists($entity, 'updated_by')) {
$entity->updated_by = $this->identifier;
$primitivelyChangedEntities->attach($entity);
}
}
foreach ($primitivelyChangedEntities as $entity) {
$meta = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($meta, $entity);
}
}
示例6: onFlush
/**
* Upload the files on new and updated entities
*
* @param OnFlushEventArgs $eventArgs
*/
public function onFlush(OnFlushEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
$entities = array_merge($uow->getScheduledEntityInsertions(), $uow->getScheduledEntityUpdates());
foreach ($entities as $entity) {
$classMetadata = $em->getClassMetadata(get_class($entity));
if (!$this->isEntitySupported($classMetadata)) {
continue;
}
if (0 === count($entity->getUploadableFields())) {
continue;
}
// If there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
foreach ($entity->getUploadableFields() as $field => $uploadDir) {
// If a file has been uploaded
if (null !== $entity->getUploadField($field)) {
// Generate the filename
$filename = $entity->generateFilename($entity->getUploadField($field), $field);
// Set the filename
$entity->setUploadPath($field, $filename);
$entity->getUploadField($field)->move($entity->getUploadRootDir($field), $entity->getUploadPath($field));
// Remove the previous file if necessary
$entity->removeUpload($field, true);
$entity->unsetUploadField($field);
}
}
$em->persist($entity);
$uow->recomputeSingleEntityChangeSet($classMetadata, $entity);
}
}
示例7: onFlush
public function onFlush(OnFlushEventArgs $args)
{
if ($this->_isFlushing) {
return;
}
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$entities = array_merge($uow->getScheduledEntityInsertions(), $uow->getScheduledEntityUpdates(), $uow->getScheduledEntityDeletions());
foreach ($entities as $entity) {
$structures = [];
if ($entity instanceof Structure) {
$structures[] = $entity;
} else {
if ($entity instanceof Node) {
$structures[] = $entity->structure;
} else {
if ($entity instanceof Content) {
foreach ($entity->nodes as $node) {
$structures[] = $node->structure;
}
} else {
continue;
}
}
}
foreach ($structures as $structure) {
if (!in_array($structure, $this->_structures, true)) {
$this->_structures[] = $structure;
}
}
}
}
示例8: onFlush
/**
* @param OnFlushEventArgs $event
*/
public function onFlush(OnFlushEventArgs $event)
{
$em = $event->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if ($entity instanceof User) {
$userSettings = new UserSettings();
$userSettings->setUser($entity);
$em->persist($userSettings);
$uow->computeChangeSet($em->getClassMetadata(UserSettings::clazz()), $userSettings);
}
if ($entity instanceof Group) {
$groupSettings = new GroupSettings();
$groupSettings->setGroup($entity);
$em->persist($groupSettings);
$uow->computeChangeSet($em->getClassMetadata(GroupSettings::clazz()), $groupSettings);
}
}
foreach ($uow->getScheduledEntityDeletions() as $entity) {
if ($entity instanceof User) {
$query = $em->createQuery(sprintf('DELETE FROM %s us WHERE us.user = ?0', UserSettings::clazz()));
$query->execute(array($entity));
}
if ($entity instanceof Group) {
$query = $em->createQuery(sprintf('DELETE FROM %s us WHERE us.group = ?0', GroupSettings::clazz()));
$query->execute(array($entity));
}
}
}
示例9: onFlush
public function onFlush(OnFlushEventArgs $args)
{
//echo "---preFlush".PHP_EOL;
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if ($entity instanceof CmsUser) {
// Adds a phonenumber to every newly persisted CmsUser ...
$phone = new CmsPhonenumber();
$phone->phonenumber = 12345;
// Update object model
$entity->addPhonenumber($phone);
// Invoke regular persist call
$em->persist($phone);
// Explicitly calculate the changeset since onFlush is raised
// after changeset calculation!
$uow->computeChangeSet($em->getClassMetadata(get_class($phone)), $phone);
// Take a snapshot because the UoW wont do this for us, because
// the UoW did not visit this collection.
// Alternatively we could provide an ->addVisitedCollection() method
// on the UoW.
$entity->getPhonenumbers()->takeSnapshot();
}
/*foreach ($uow->getEntityChangeSet($entity) as $field => $change) {
list ($old, $new) = $change;
var_dump($old);
}*/
}
}
示例10: onFlush
/**
* @param OnFlushEventArgs $args
*/
public function onFlush(OnFlushEventArgs $args)
{
$uow = $args->getEntityManager()->getUnitOfWork();
foreach ($uow->getScheduledEntityDeletions() as $entity) {
$this->addPendingUpdates($entity);
}
}
示例11: onFlush
public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$entities = array_merge($uow->getScheduledEntityInsertions(), $uow->getScheduledEntityUpdates());
foreach ($entities as $entity) {
if (!$entity instanceof Searchable) {
continue;
}
$this->_updates[] = $entity;
}
$entities = array_merge($uow->getScheduledEntityDeletions());
foreach ($entities as $entity) {
if (!$entity instanceof Searchable) {
continue;
}
$this->_deletions[] = $entity;
}
if (count($this->_deletions)) {
$indexes = $em->getRepository('Chalk\\Core\\Index')->entities($this->_deletions);
foreach ($indexes as $index) {
$em->remove($index);
}
$this->_deletions = [];
}
}
示例12: onFlush
/**
* Update event's exclude dates and recurrenceIds when start datetime changes.
*
* @param OnFlushEventArgs $eventArgs
*/
public function onFlush(OnFlushEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
$entities = array_merge($uow->getScheduledEntityUpdates(), $uow->getScheduledEntityInsertions());
$eventUtil = new EventUtil($em);
foreach ($entities as $entity) {
if (!$entity instanceof Event) {
continue;
}
/** @var $entity \Xima\ICalBundle\Entity\Component\Event */
$eventUtil->cleanUpEvent($entity);
$uow->recomputeSingleEntityChangeSet($em->getClassMetadata(get_class($entity)), $entity);
// apply changed $dtStart values to child events $recurrenceIds if any
$changeSet = $uow->getEntityChangeSet($entity);
if (isset($changeSet['dtStart']) && isset($changeSet['dtStart'][0])) {
$interval = $changeSet['dtStart'][0]->diff($entity->getDtStart());
$q = $em->createQuery("select e from Xima\\ICalBundle\\Entity\\Component\\Event e where e.uniqueId = '" . $entity->getUniqueId() . "' AND e.recurrenceId IS NOT NULL");
$detachedEvents = $q->getResult();
foreach ($detachedEvents as $detachedEvent) {
/** @var $detachedEvent \Xima\ICalBundle\Entity\Component\Event */
$recurrenceId = $detachedEvent->getRecurrenceId();
$recurrenceId->getDatetime()->add($interval);
// make Doctrine accept a DateTime as changed
$recurrenceId->setDatetime(clone $recurrenceId->getDatetime());
$uow->recomputeSingleEntityChangeSet($em->getClassMetadata(get_class($recurrenceId)), $recurrenceId);
}
}
}
}
示例13: onFlush
/**
* Logs the entity changes
*
* @param \Doctrine\ORM\Event\OnFlushEventArgs $eventArgs
*/
public function onFlush(OnFlushEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityInsertions() as $entity) {
$this->debug('Inserting entity ' . get_class($entity) . '. Fields: ' . json_encode($uow->getEntityChangeSet($entity)));
}
foreach ($uow->getScheduledEntityUpdates() as $entity) {
$add = '';
if (method_exists($entity, '__toString')) {
$add = ' ' . $entity->__toString();
} elseif (method_exists($entity, 'getId')) {
$add = ' with id ' . $entity->getId();
}
$this->debug('Updating entity ' . get_class($entity) . $add . '. Data: ' . json_encode($uow->getEntityChangeSet($entity)));
}
foreach ($uow->getScheduledEntityDeletions() as $entity) {
$add = '';
if (method_exists($entity, '__toString')) {
$add = ' ' . $entity->__toString();
} elseif (method_exists($entity, 'getId')) {
$add = ' with id ' . $entity->getId();
}
$this->debug('Deleting entity ' . get_class($entity) . $add . '.');
}
}
示例14: onFlush
public function onFlush(OnFlushEventArgs $args)
{
if (!$this->container->has('orocrm_contact.contact.manager')) {
return;
}
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$insertedEntities = $uow->getScheduledEntityInsertions();
$updatedEntities = $uow->getScheduledEntityUpdates();
$entities = array_merge($insertedEntities, $updatedEntities);
foreach ($entities as $entity) {
if (!$entity instanceof DiamanteUser) {
continue;
}
$contactManager = $this->container->get('orocrm_contact.contact.manager');
$contact = $contactManager->getRepository()->findOneBy(['email' => $entity->getEmail()]);
if (empty($contact)) {
continue;
}
if ($entity->getFirstName() == null) {
$entity->setFirstName($contact->getFirstName());
}
if ($entity->getLastName() == null) {
$entity->setLastName($contact->getLastName());
}
try {
$em->persist($entity);
$md = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($md, $entity);
} catch (\Exception $e) {
$this->container->get('monolog.logger.diamante')->addWarning(sprintf('Error saving Contact Information for Diamante User with email: %s', $entity->getEmail()));
}
}
}
示例15: handleOnFlush
/**
* Handle onFlush event
*
* @param OnFlushEventArgs $event
*/
public function handleOnFlush(OnFlushEventArgs $event)
{
$em = $event->getEntityManager();
$uow = $em->getUnitOfWork();
$newEntities = $uow->getScheduledEntityInsertions();
foreach ($newEntities as $entity) {
if ($entity instanceof Call) {
$hasChanges = $this->activityManager->addActivityTarget($entity, $entity->getOwner());
// recompute change set if needed
if ($hasChanges) {
$uow->computeChangeSet($em->getClassMetadata(ClassUtils::getClass($entity)), $entity);
}
}
}
$changedEntities = $uow->getScheduledEntityUpdates();
foreach ($changedEntities as $entity) {
if ($entity instanceof Call) {
$hasChanges = false;
$changeSet = $uow->getEntityChangeSet($entity);
foreach ($changeSet as $field => $values) {
if ($field === 'owner') {
list($oldValue, $newValue) = $values;
if ($oldValue !== $newValue) {
$hasChanges |= $this->activityManager->replaceActivityTarget($entity, $oldValue, $newValue);
}
break;
}
}
// recompute change set if needed
if ($hasChanges) {
$uow->computeChangeSet($em->getClassMetadata(ClassUtils::getClass($entity)), $entity);
}
}
}
}