Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'd like to display the page author's name on the single post page. My approach:

  •  add a creatorName attribute and a corresponding getCreatorName method to the Page class
  • in page.php, change the the query to
    $q = "SELECT id, creatorId, title, content, DATE_FORMAT(dateAdded, '%e %M %Y') AS dateAdded, u.username AS creatorName FROM pages AS p INNER JOIN users AS u ON u.userId=p.creatorId WHERE p.id= $id";
    
  • in page.html - use $page->getCreatorName(); to display the name

This works nicely and I like the way the code structure enables me to slot in a new attribute easily. I guess this is the advantage of using OOP.

 

My question - I have the page creator's name as an attribute in both classes, is this approach the best way? I was considering changing the FETCH_MODE to be able to access the username without having to add it as an attribute to class.  Is one way better than the other or is it a matter of preference.

 

I like the first way because I have instantiated the page object and have access to all its methods and attributes if I decide I want to do more processing with it.

Link to comment
Share on other sites

I think that is correct having a creator id in the page class and then have a user class with the actual creaters user id. So having two is fine, and then have your getCreatorMethod in the page class is also %100. This is actually how Yii does it in its model classes it stores the attributes which relate to a database model in the class. And you should put related model methods or calls within the related class.

Link to comment
Share on other sites

Thanks for the reply Edward. I think you may have misunderstood my question or I'm not completely understanding your response.

 

The User and Page classes were originally set up with Id and creatorId respectively. In order to display the creatorName which already exists as username in the user class I have added the creatorName attribute (and getCreatorName method) to the Page class. creatorName is not a field in the pages table.

 

Is it redundant to have name in both classes (username and creatorName in User class and Page class)? Its just 2 attributes currently, but if I decided the Page class wanted to manipulate a lot more data about the creator there would be alot of overlap between the 2 classes? Does that matter in classes?

Link to comment
Share on other sites

I don't have the book in front of me (and have a bad memory), but isn't an instance of the User class stored in the Page class? If not, you could write it that way. Then you could access any of the author information through that. 

 

That being said, I think your solution is fine.

Link to comment
Share on other sites

I know there's an example in the book somewhere on type restricting method arguments based upon class. You'd use that as part of your code basis. But you would need to either run two database queries, fetching each into a different object (one page, one user) or run one database query and then create the objects from the fetched results (creating constructors that take all the values). 

 

Let me know if any of that is unclear or if you need more help.

Link to comment
Share on other sites

 Share

×
×
  • Create New...