Edit object properties

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
networkguy
Forum Newbie
Posts: 19
Joined: Tue Mar 31, 2009 2:26 am

Edit object properties

Post by networkguy »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi, I have a question about editing object properties and subsequent database values.

If you create and save an objects properties into the database, how would you refer back to that object if in case you wanted to edit one or more of its properties. For example, lets say I instantiated a new Page() (from the Page class) object called $PageOne, set its properties of id, title and text. All I wanted to do was edit the text field, how would I go about doing that? I'm thinking about calling the setText() method and then use mysql UPDATE to update text to database.

Code: Select all

$PageOne = &new Page();
$PageOne->setText("Some text to display...");
$PageOne->setTitle("My first page!");
$PageOne->save(); //saves to database and returns saved id
 
$PageOne->setText("Some new text to display....");
$PageOne>update(); //update text on page of id (using getId()) for given object
Should this be the right way of doing it?

Any advice would be appreciated, thanks in advance!

The other thing I did not mention for simplicity is that I'm going to want to do this from another class (lets call it Books) that will create an instance of a Page class. I want to be able to edit Page objects through the Books class, so the Books class is an interface to the Page class. I'll be using composition for this. If someone has done this type of thing before I was wondering whether you could pass the object name through the class, since the Book object would contain an array of Page objects, I'd check the name of the Page object in the Pages array of objects, if the object exists then I only want to "Update" it, else I want to instantiate a new Page object. Anyways, this part is just IF anyone has done this type of thing before. If you have any idea about this I double thank you in advance for any pointers or opinions.


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Edit object properties

Post by josh »

- Each method call could directly execute an SQL command to update the value
- the save method could figure out wether to insert or update all the fields ( active record pattern )
- some external mechanism could create this object and save it, by "pulling" values out of it and saving them, so no database code is present in the object ( data mapper pattern )
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Edit object properties

Post by Jenk »

Code: Select all

INSERT INTO `table` VALUES (`foo`, `blah`) ON DUPLICATE KEY UPDATE
Post Reply