Jump to content

MelodyMaker

Members
  • Posts

    23
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

MelodyMaker's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I think you're right for the method that changes the caption. But for adding and removing photos?
  2. So the operations such as: -add -remove -move (by "moving", I mean that the photo number 7 can be moved to position 1 or 2 and the other photos (their numbers) must be ordered consequently. -getPhotoByNumber (which returns a Photo instance, by passing it his number) Should be left out of the user class? But then, should the user class store an array of photos or not? Since Photos are associated to User by a idUser property, they can be left out. Or am I wrong? From a logical point of view it seems also correct to me to store them in the user object since a foto belongs to a user...
  3. I'm sorry, I don't want to bother you with my questions but a User has: -a name -an email - a country ID ... as private properties : Class User{ private name; private email; private countryID; ... } so, why it should not contain an array of photos and addresses as properties as well? Is it because they are arrays?
  4. Thanks keeb, for your help. The fact is that I see Photos, Description and Addresses as aggregates of a user. I mean, I think they're related each other, or I 'm wrong?
  5. Hi, I would like to know if there's a way to avoid bad repetition like this one: I have a User class: class User{ private $conf; private $photos = Array(); private $addresses = Array(); private $descriptions = Array(); ... public function __construct($userData){ $this->conf = Configuration::getInstance(); ... } public function addPhoto(Photo $photo){ $photoNumber = $this->getPhotosAmount(); if( $photoNumber < $this->conf->userMaxPhotos){ $photo->setIdUser($this->u_idUser); $photo->setNumber(++$photoNumber); $this->photos[] = $photo; return true; } return false; } public function addDescription(Description $description){ $descNumber = $this->getDescriptionsAmount(); if( $descNumber < $this->conf->userMaxDescriptions){ $description->setIdUser($this->u_idUser); $description->setNumber(++$descNumber); $this->descriptions[] = $description; return true; } return false; } public function addAddress(Address $address){ $addressNumber = $this->getAddressesAmount(); if( $addressNumber < $this->conf->userMaxAddresses){ $address->setIdUser($this->u_idUser); $address->setNumber(++$addressNumber); $this->addresses[] = $address; return true; } return false; } ... } Of course, there are also three "remove" functions. It looks like very bad, to me... As you can see, the methods are very similar and this is very annoying. So, I decided to create and abstract class called "profileItem" which is a superclass of Photo, Description, and Address Class. The abstract ProfileItem Class: abstract class AbsProfileItem{ private $number = 1; private $idUser = 0; public function getIdUser(){ return $this->idUser; } public function setIdUser($idUser){ $this->idUser = $idUser; } public function getNumber(){ return $this->number; } public function setNumber($number){ if($number > 0){ $this->number = $number; } } } The Photo class: class Photo extends AbsProfileItem { private $url = ""; private $caption = ""; public function __construct($url,$caption){ $this->url = $url; $this->caption = $caption; } public function getUrl(){ return $this->url; } public function setUrl($url){ $this->url = $url; } public function getCaption(){ return $this->caption; } public function setCaption($caption){ $this->caption = $number; } } Address and Descriptions are similar... The problem is that I don't know how to integrate them with the user class...I mean, I need, for example an addItem method which can be used for Description, Photo, and Address. But at some point, there will be a conditional statement, something like: if ($item instance of "Address") then "the address should be added to $this->addresses if ($item instance of "Photo") then "the photo should be added to $this->photos if ($item instance of "Description") then "the description should be added to $this->descriptions ... Is there a clean, elegant way to do this (maybe with the help of a pattern...don't know)? Thank you very much in advance! Davide
  6. I'll do it for sure. Thank you very much to all of you!
  7. Oh, and someone told me to also introduce into the system a UserManager that creates user instances by passing a $idUser to his method: $userManager = UserManager::getInstance(); $user = userManager->getUserById($idUser); I know that this is much cleaner, but is that the only reason to introduce all those "managers"? Thank you very much in advance. Davide
  8. Thank you very much for your suggestions! What I maybe still don't understand (and I hope this is due to the fact that I'm a beginner) is to introduce the Gallery (or PhotoManager object). I mean, why? Loading, adding photos, and so on, cannot be simply methods of the user class? Or this violates the encapsulation principle? Is it for that reason?
  9. ...but the AddPhoto and DeletePhoto are still in user class? And your method: public function getAllPhotos(User $user) { $userId = $user->getId(); $query = "SELECT * FROM userphotos WHERE userId = '$userId'"; $result = mysql_query($query); return $result; } This should build and return Photo instances, right?
  10. Yes, some people told me to go that way as well. So, we should have three classes involved: a Photo Class a PhotoRepository Class a User Class. Photo Class which models a photo: Class Photo{ function __construct($url,$number,$caption){ ... } } a PhotoRepository Class which has (or stores) a User reference: Class PhotoRepository{ public function __construct(User $user) //<- is it correct to pass it as a constructor parameter? {...} public function getAllPhotos(){ //Here the query to the DB $QUERY = "SELECT * FROM userphoto WHERE idUser='.$this->user->getIdUser()'."; ... $arr = Array(); while($res = ...){ $arr[] = new Photo($res[url], $res['caption'],$res['number'] } return $arr; } //What about deleting a photo??? (from the repository AND FROM THE DB?) } and the class User: class User{ function __construct($id){ ... } //The following methods should call a PhotoRepository method? public function Addphoto() { ... } public function removePhoto() { } }
  11. Thanks for your answer! The getPhotos method you wrote works only if first a user adds a photo. But sometimes, I have to load all the photos from the database, without using the method add. This is this point my question came from: function getPhotos(){ SELECT * from userphoto where idUser = ... //at this point, I have to build the object photo, but how? while ($res = $result->fetch_array()){ //here I can get the IDPhoto, since i retrieve it from the DB (the link (url) to the foto is in the DB and the file has uploaded) $photo = new Photo ($res['IDPhoto'] $res, $res['caption']) // } BUT, for example, when a user, uploads a photo (if the upload has success) the photo can't have a IDPhoto, because it gets (or should get) that ID ONLY after a successful update AND after an INSERT into a DB. So, a constructor like this: new Photo ($IDPhoto,$url, $caption) in the case of an upload does not work, I think. It is this that is making me crazy... }
  12. Thanks again, and I'm sorry if I ask you another question, but this is very very important for me. There's still something I don't understand. You wrote the photo Class: class Photo { private $url; private $caption; public function __construct($url, $caption) { $this->url = $url; $this->caption = $caption; } public function upload(User $user) { $userId = $user->getId(); $query = "INSERT INTO photos (url, caption) VALUES ({$this->url}, {$this->caption}) WHERE user_id = '$userId'"; $result = mysql_query($query); if($result) { //success! } else { //failure } } } The constructor gets a url and a caption as arguments. That's ok. What is making me mad, now, is that I would like to have a method that gives me all the photo for a specific user and also a metod to delete a photo. The first problem is: where should I place those methods? Something tells me that they should be in the user Class: class User{ ... public function getAllPhotos(){ $QUERY = "SELECT * FROM userphoto WHERE idUser=..." $result = mysql_query($QUERY); while($res = $result->fetch_array()){ $photo = new Photo($res['url'],$res['caption']) $this->photos[] = $photo } } } Is this correct? Now the problem: The delete method. It should not only remove a photo from the "photos" array of the user class, but also delete it from database (and maybe even delete it from disc). For this reason, I think that it would be nice to have also a IDPhoto value somewhere (which is infact the primary key value of the table userphoto). Otherwise, the SQL DELETE statement would be really bad: DELETE FROM userphoto WHERE idUser= and url=... best would be: DELETE FROM userphoto WHERE IDPhoto = $IDPhoto. I don't know how to insert IDPhoto, since we construct the class only with url and caption. And last, I will certainly use also a "number" field to number the photos, a user has. I don't know if this could be use to delete the photos, instead of using a IDPhoto.. Thanks in advance, I know I'm asking you a lot.Sorry if I bother you... Davide
  13. Just a quick question: passing a User instance as argument to the upload photo method, is it correct from a formal point of view?
  14. Thank you! The fact is that I really can't figure out, how the two classes are associated, I mean: let's suppose that I give the user (it is so) the ability to add (upload) a photo and delete a photo. Then, according to what you say, these methods should be part of the Photo Class. which parameters should I pass to the AddPhoto? A reference to the user that owns that photo. So, first I have to instantiate User class, then the foto class (what about the parameters in its constructor?) and then, somewhat pass the userId to the photo Class? Please not that my photo has only four attributes: a url, a caption, an idUser and a number (plus the methods to upload and delete, if I put them here). Sorry, I know that I'm terribly confused...
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.