php - Data mapper pattern and automated updates of other objects -


I am creating a PHP application using the Data Mapper pattern to separate my DB from the domain object. I have a Mapper class which gives the site object based on data from DB and accepts it to save the existing site object back in DB.

My problem is that one (and only one) sites in the system have to be marked as the "primary" site, which means that if I set one in primary form then I want to be able to automatically unset from the current primary.

Then, something like this:

$ mapper = new Site_Mapper (); $ Site = $ mapper- & gt; Bring (2); $ Site- & gt; Primary = true; $ Mapper- & gt; ($ Site) save;

Anyhow in the background it will do this automatically:

  $ mapper = new Site_Mapper (); $ Site = $ mapper- & gt; Bring (1); $ Site- & gt; Primary = false; $ Mapper- & gt; ($ Site) save;  

The question is, where should the logic argument go to automatically update the existing primary site? If the DB query fails and no site is left as primary, then the object must be saved after saving the DB.

Cheers, Jack

  Public function save ($ siteObj) {// Save the saved object $ Sql ​​= "Site Set Up primary = 1 where id! =? "; $ Stmt = new PDO_statement ($ sql); $ Stmt- & gt; Execute ($ siteObj-> ID); }  

Obviously you can either create custom save () functions, maybe to do it a bit more smoothly or if you can compare it to make sure that you Really need to run update statement


Comments