{"id":28605,"date":"2018-05-31T12:09:38","date_gmt":"2018-05-31T10:09:38","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=28605"},"modified":"2018-05-31T12:09:38","modified_gmt":"2018-05-31T10:09:38","slug":"when-and-where-to-determine-the-id-of-an-entity","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2018\/05\/31\/when-and-where-to-determine-the-id-of-an-entity\/","title":{"rendered":"When and where to determine the ID of an entity"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>It always amazes me when I randomly come across an article or a blog post precisely on the subject that I&#8217;m mulling over in my head &#8211; all without searching specifically for the solution or even researching the problem domain.\u00a0 It&#8217;s almost like the universe knows what I&#8217;m thinking and sends help my way.<\/p>\n<p>&#8220;<a href=\"https:\/\/matthiasnoback.nl\/2018\/05\/when-and-where-to-determine-the-id-of-an-entity\/\">When and where to determine the ID of an entity<\/a>&#8221; is an example of exactly that.\u00a0 Lately, I&#8217;ve been working with <a href=\"https:\/\/book.cakephp.org\/3.0\/en\/core-libraries\/events.html\">events in CakePHP<\/a> a lot.\u00a0 And for one particular scenario, I was considering the <em>beforeSave()<\/em> event in the model layer, which would trigger some functionality that modifies data in other models.\u00a0 So, having a reference of the current ID would be useful for debugging and logging purposes.\u00a0 But since the current entity hasn&#8217;t been saved it, the ID is not there.\u00a0 \u00a0And that&#8217;s where I started thinking about this whole thing and considering where is the right place to generate the ID.<\/p>\n<p>One thing that kind of bothered me on top of the theoretical discussion, was the practical implementation, especially in different frameworks.\u00a0 If I remember correctly, the earlier version of CakePHP framwork, used the presence or absense of the ID in the entity to differentiate between insert and update operations.\u00a0 It might still be true now, but at least there is a way to work around it, as CakePHP now has <em>isNew()<\/em> method to check if the entity needs to be <a href=\"https:\/\/book.cakephp.org\/3.0\/en\/orm\/saving-data.html#updating-data\">inserted or updated<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>It always amazes me when I randomly come across an article or a blog post precisely on the subject that I&#8217;m mulling over in my head &#8211; all without searching specifically for the solution or even researching the problem domain.\u00a0 It&#8217;s almost like the universe knows what I&#8217;m thinking and sends help my way. &#8220;When &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2018\/05\/31\/when-and-where-to-determine-the-id-of-an-entity\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">When and where to determine the ID of an entity<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"When and where to determine the ID of an entity #WebDev #PHP #CakePHP #databases #OOP #BestPractices","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[3069,1537,1559,3083,38,1330],"keyring_services":[],"class_list":["post-28605","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-best-practices","tag-cakephp","tag-databases","tag-oop","tag-php","tag-web-development"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":13328,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/08\/31\/orm-designer\/","url_meta":{"origin":28605,"position":0},"title":"ORM Designer","author":"Leonid Mamchenkov","date":"August 31, 2010","format":false,"excerpt":"Here is a tool that might help you with your MVC framework, like CakePHP, Symfony, and others - ORM Designer. \u00a0In\u00a0essence, it is a graphical user interface for drawing a visual representation of your project (such as an Entity Relationship Diagram (ERD)) and than converting it into the code. \u00a0You\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2010\/08\/orm_designer-500x268.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":27717,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/06\/22\/phinx-joins-cakephp\/","url_meta":{"origin":28605,"position":1},"title":"Phinx joins CakePHP!","author":"Leonid Mamchenkov","date":"June 22, 2017","format":false,"excerpt":"These are some really good news - Phinx joins CakePHP family! \u00a0If you are from a different technology stack and not familiar with these, Phinx is an excellent database migrations tool, which has been used by CakePHP framework for a while now. \u00a0The two worked great together. \u00a0Now that they\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27851,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/08\/15\/cakephp-events-system\/","url_meta":{"origin":28605,"position":2},"title":"CakePHP Events System","author":"Leonid Mamchenkov","date":"August 15, 2017","format":false,"excerpt":"Events are a great way to separate the business logic of your application and make things simpler and, often, faster. \u00a0CakePHP framework introduced an events system in version 2.1, and since then it got much better. \u00a0The official documentation covers current implementation pretty well. \u00a0But in this post I wanted\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24952,"url":"https:\/\/mamchenkov.net\/wordpress\/2015\/11\/10\/cakephp-3-cheatsheet\/","url_meta":{"origin":28605,"position":3},"title":"CakePHP 3 Cheatsheet","author":"Leonid Mamchenkov","date":"November 10, 2015","format":false,"excerpt":"CakePHP 3 Cheatsheet - a handy collection of code snippets for anyone new to CakePHP 3 framework and all the changes that went into this version.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27405,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/10\/using-requirejs-with-cakephp\/","url_meta":{"origin":28605,"position":4},"title":"Using RequireJS with CakePHP","author":"Leonid Mamchenkov","date":"March 10, 2017","format":false,"excerpt":"https:\/\/www.slideshare.net\/StephenYoung6\/require-js-cakephp","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24147,"url":"https:\/\/mamchenkov.net\/wordpress\/2015\/05\/20\/cakephp-3-orm\/","url_meta":{"origin":28605,"position":5},"title":"CakePHP 3 ORM","author":"Leonid Mamchenkov","date":"May 20, 2015","format":"status","excerpt":"https:\/\/twitter.com\/cakephp\/status\/600970761004851200","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/28605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=28605"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/28605\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=28605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=28605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=28605"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=28605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}