Page 1 of 1

Edit object properties

Posted: Mon Apr 06, 2009 12:06 am
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.

Re: Edit object properties

Posted: Mon Apr 06, 2009 4:04 pm
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 )

Re: Edit object properties

Posted: Wed Apr 15, 2009 5:14 am
by Jenk

Code: Select all

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