{"id":5719,"date":"2014-04-06T10:29:16","date_gmt":"2014-04-06T10:29:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/preupdate-event-listener-crashes-the-site-collection-of-common-programming-errors\/"},"modified":"2014-04-06T10:29:16","modified_gmt":"2014-04-06T10:29:16","slug":"preupdate-event-listener-crashes-the-site-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/06\/preupdate-event-listener-crashes-the-site-collection-of-common-programming-errors\/","title":{"rendered":"PreUpdate event listener crashes the site-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to build a preUpdate, I was doing it with a postUpdate but I didn&#8217;t contemplate the fact that I was changing whenever an edit of that entity was being made, no matter the field&#8230; so I read I should use preUpdate instead&#8230;<\/p>\n<p>This is my preUpdate Listener:<\/p>\n<pre><code>namespace Prizes\\PrizesBundle\\EventListener;\n\nuse Doctrine\\ORM\\Event\\LifecycleEventArgs;\nuse Prizes\\PrizesBundle\\Entity\\Prize;\nuse Prizes\\CatalogBundle\\Entity\\CatalogHasPrize as CHP;\n\n\/**\n * Description of DeactivatePrizesInCatalog\n *\n * @author Victoria Noguera\n * Desactiva premios en catalogos cuando se desactiven en el prize central.\n *\/\n\/**\n * @ORM\\Entity\n * @ORM\\HasLifecycleCallbacks\n *\/\nclass DeactivatePrizesInCatalog {\n\/**\n     * @ORM\\PrePersist()\n     * @ORM\\PreUpdate()\n     *\/\n    public function preUpdate(LifecycleEventArgs $args) {\n       $entity = $args-&gt;getEntity();\n        $entityManager = $args-&gt;getEntity\n\nManager();\n    if ($entity instanceof Prize) {\n        if ($args-&gt;hasChangedField('status')) {\n            $status_new = $args-&gt;getNewValue('status');\n            if ($status_new-&gt;getName() == \"Inactive\") {\n                $statusinactive = $entityManager-&gt;getRepository('AppStatusBundle:Status')-&gt;find(8);\n                $q = $entityManager-&gt;createQuery(\"UPDATE CatalogBundle:CatalogHasPrize c SET c.status = :statusid WHERE c.status = 7 AND c.prize = :prizeid\")\n                        -&gt;setParameters(array('statusid' =&gt; $statusinactive, 'prizeid' =&gt; $entity-&gt;getId()));\n                $rs = $q-&gt;getResult();\n            } else if ($status_new-&gt;getName() == \"Active\") {\n                $statusinactive = $entityManager-&gt;getRepository('AppStatusBundle:Status')-&gt;find(7);\n                $q = $entityManager-&gt;createQuery(\"UPDATE CatalogBundle:CatalogHasPrize c SET c.status = :statusid WHERE c.status = 8 AND c.prize = :prizeid\")\n                        -&gt;setParameters(array('statusid' =&gt; $statusinactive, 'prizeid' =&gt; $entity-&gt;getId()));\n                $rs = $q-&gt;getResult();\n            }\n        }\n    }\n}\n\n}\n<\/code><\/pre>\n<p>and this is the way i&#8217;m doing it on the config.yml file (I just read it goes in a services.yml, but i&#8217;ll change it later)<\/p>\n<pre><code>services:\n    preupdate.listener:\n        class: Prizes\\PrizesBundle\\EventListener\\DeactivatePrizesInCatalog\n        tags: \n             - { name: doctrine.event_listener , event: preUpdate }\n<\/code><\/pre>\n<p>however, when I do this, my project goes from fully functional to 503 service unavailable. Even if I just write <code>public function preUpdate(LifecycleEventArgs $args) { }<\/code> it crashes, I&#8217;m assuming a configuration issue, but I have no idea what I&#8217;m doing wrong.<\/p>\n<p>I&#8217;d appreciate any help offered. Thank you<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to build a preUpdate, I was doing it with a postUpdate but I didn&#8217;t contemplate the fact that I was changing whenever an edit of that entity was being made, no matter the field&#8230; so I read I should use preUpdate instead&#8230; This is my preUpdate Listener: namespace Prizes\\PrizesBundle\\EventListener; use Doctrine\\ORM\\Event\\LifecycleEventArgs; use Prizes\\PrizesBundle\\Entity\\Prize; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5719","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5719"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5719\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}