public function retrieveUsers($date) { $users = new \Doctrine\Common\Collections\ArrayCollection(); $articleRepo = $this->getManager()->getRepository('AppBundle:Article'); $articles = $articleRepo->findWithNotifications($date); foreach ($articles as $article) { if ($article->getAnswers()->count()) { foreach ($article->getAnswers() as $answer) { if ($answer->getCreated() < $date && !$answer->getEmailSent()) { if (!$users->contains($answer->getUser())) { $users->add($answer->getUser()); } $answer->setEmailSent(true); $this->getEntityManager()->persist($answer); $this->getEntityManager()->flush($answer); } } } if ($article->getRatings()->count()) { foreach ($article->getRatings() as $rating) { if ($rating->getCreated() < $date && $rating->getEmailSent()) { if (!$users->contains($rating->getUser())) { $users->add($rating->getUser()); } $rating->setEmailSent(true); $this->getEntityManager()->persist($rating); $this->getEntityManager()->flush($rating); } } } } return $users; }
/** * Takes a pile of commits, then puts together all the repositories that are affected by * at least one of the given commits. * * @param \TYPO3\Flow\Persistence\QueryResultInterface the pile of commits * @return \Doctrine\Common\Collections\ArrayCollection the resulting stack of repositories */ public function extractTheRepositoriesFromAStackOfCommits(\TYPO3\Flow\Persistence\QueryResultInterface $commits) { $result = new \Doctrine\Common\Collections\ArrayCollection(); if ($commits->count()) { foreach ($commits as $commit) { if (!$result->contains($commit->getRepository())) { $result->add($commit->getRepository()); } } } return $result; }
/** * Helper function to set the association data of a ORM\OneToMany association of doctrine. * <br><br> * The <b>$data</b> parameter contains the data for the collection property. It can contains an array of * models or data arrays. If the $data parameter is set to null the associated collection will cleared. * <br><br> * The <b>$model</b> parameter expects the full name of the associated model. * For example: * <ul> * <li>We are in the Customer model in the setOrders() function.</li> * <li>Here we want to set the Order objects over the "setOneToMany" function</li> * <li>So we passed as $model parameter: <b>"\Shopware\Models\Order\Order"</b></li> * </ul> * <br> * The <b>$property</b> parameter expect the name of the association property. * For example: * <ul> * <li>In the setOrders() function of the customer model we would expects <b>"orders"</b>.</li> * </ul> * <br> * The <b>$reference</b> property expect the name of the property on the other side of the association. * For example: * <ul> * <li>In the setOrders() function we want to fill the orders data.</li> * <li>To set the reference between customer and orders we set in the orders object the "customer"</li> * <li>To set the customer we use the "$order->setCustomer()" function.</li> * <li>So the parameter expect <b>"customer"</b></li> * </ul> * * @param array|null $data Model data, example: an array of \Shopware\Models\Order\Order * @param string $model Full namespace of the association model, example: '\Shopware\Models\Order\Order' * @param string $property Name of the association property, example: 'orders' * @param string $reference Name of the reference property, example: 'customer' * @return \Shopware\Components\Model\ModelEntity */ public function setOneToMany($data, $model, $property, $reference = null) { $getterFunction = "get" . ucfirst($property); $setterFunction = null; if ($reference !== null) { $setterFunction = "set" . ucfirst($reference); } //to remove the whole one to many association, u can pass null as parameter. if ($data === null) { $this->{$getterFunction}()->clear(); return $this; } //if no array passed or if false passed, return if (!is_array($data)) { return $this; } //create a new collection to collect all updated and created models. $updated = new \Doctrine\Common\Collections\ArrayCollection(); //iterate all passed items foreach ($data as $item) { //to get the right collection item use the internal helper function if (is_array($item) && isset($item['id']) && $item['id'] !== null) { $attribute = $this->getArrayCollectionElementById($this->{$getterFunction}(), $item['id']); if (!$attribute instanceof $model) { $attribute = new $model(); } //if the item is an array without an id, create a new model. } elseif (is_array($item)) { $attribute = new $model(); //if the item is no array, it could be an instance of the expected object. } else { $attribute = $item; } //check if the object correctly initialed. If this is not the case continue. if (!$attribute instanceof $model) { continue; } //if the current item is an array, use the from array function to set the data. if (is_array($item)) { $attribute->fromArray($item); } //after the attribute filled with data, set the association reference and add the model to the internal collection. if ($setterFunction !== null) { $attribute->{$setterFunction}($this); } if (!$this->{$getterFunction}()->contains($attribute)) { $this->{$getterFunction}()->add($attribute); } //add the model to the updated collection to have an flag which models updated. $updated->add($attribute); } //after all passed data items added to the internal collection, we have to iterate the items //to remove all old items which are not updated. foreach ($this->{$getterFunction}() as $attr) { //the updated collection contains all updated and created models. if (!$updated->contains($attr)) { $this->{$getterFunction}()->removeElement($attr); } } return $this; }
public function addParticipantMeta(\Application\Entity\ParticipantMetaEntity $participantMeta) { if (!$this->participantMetas->contains($participantMeta)) { $participantMeta->setParticipant($this); $this->participantMetas->add($participantMeta); } return $this; }
public function addEntryMeta(\Application\Entity\EntryMetaEntity $entryMeta) { if (!$this->entryMetas->contains($entryMeta)) { $entryMeta->setEntry($this); $this->entryMetas->add($entryMeta); } return $this; }
/** * Edits an existing Tag entity. * * @Route("/{id}", name="tag_update") * @Method("PUT") * @Template("PaxyknoxNewsBundle:Tag:edit.html.twig") */ public function updateAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $entity = $em->getRepository('PaxyknoxNewsBundle:Tag')->find($id); if (!$entity) { throw $this->createNotFoundException('Unable to find Tag entity.'); } // before Tag change // get list of all Articles with this Tag // $articles = $entity->getArticles(); $oldArticlesIdsList = new \Doctrine\Common\Collections\ArrayCollection(); foreach ($articles as $article) { $oldArticlesIdsList[] = $article->getId(); } $editForm = $this->createEditForm($entity); $editForm->handleRequest($request); if ($editForm->isValid()) { try { // Tag entity $em->flush(); // For each selected Article on edit form get its id // // Check if it is in $oldArticlesIdsList // - true remove it from the list // - false find Article and set Tag on it // // Articles that will remain on the list no longer need Tag set on it // $data = $request->request->all(); foreach ($data as $dataParameter => $dataParameterValue) { if (isset($dataParameterValue["articles"])) { foreach ($dataParameterValue["articles"] as $articleIdString) { $articleId = (int) $articleIdString; if ($oldArticlesIdsList->contains($articleId)) { $oldArticlesIdsList->removeElement($articleId); } else { $article = $em->getRepository('PaxyknoxNewsBundle:Article')->find($articleId); $article->addTag($entity); } } } } if (!$oldArticlesIdsList->isEmpty()) { foreach ($oldArticlesIdsList as $articleId) { $article = $em->getRepository('PaxyknoxNewsBundle:Article')->find($articleId); $article->removeTag($entity); $oldArticlesIdsList->removeElement($articleId); } } // Article Entity $em->flush(); $flashMessage = $this->get('translator')->trans('flash.tag.update_success', array('%s%' => $entity->getName())); $this->get('session')->getFlashBag()->add('success', $flashMessage); } catch (\Doctrine\DBAL\DBALException $e) { $flashMessage = $this->get('translator')->trans('flash.tag.update_warning'); $this->get('session')->getFlashBag()->add('warning', $flashMessage); return $this->redirect($this->generateUrl('tag_edit', array('id' => $id))); } return $this->redirect($this->generateUrl('tag_show', array('id' => $id))); } return array('entity' => $entity, 'edit_form' => $editForm->createView()); }
public function addVoteMeta(\Application\Entity\VoteMetaEntity $voteMeta) { if (!$this->voteMetas->contains($voteMeta)) { $voteMeta->setVote($this); $this->voteMetas->add($voteMeta); } return $this; }