Edit object properties
Posted: Mon Apr 06, 2009 6:08 am
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.
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.
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
1. $PageOne = &new Page();
2. $PageOne->setText("Some text to display...");
3. $PageOne->setTitle("My first page!");
4. $PageOne->save(); //saves to database and returns saved id
5.
6. $PageOne->setText("Some new text to display....");
7. $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.