Jump to content

OOP and MYSQL


ckrudelux

Recommended Posts

I've been thinking about how to populate my object how I does it now is each object fetch it's data from with in it self this results in many SQL requests which is bad. I've been thinking of doing some SQL query holding object which each get their data from. My question is what are my options to make less SQL requests and still get the data to the objects?

Link to comment
Share on other sites

if you use pdo there is option to fetch data as object setting fetching mode using PDO::FETCH_CLASS or PDO::FETCH_OBJhttp://php.net/pdo.fetch <=PDOstatement::fetch()

Link to comment
Share on other sites

if you use pdo there is option to fetch data as object setting fetching mode using PDO::FETCH_CLASS or PDO::FETCH_OBJhttp://php.net/pdo.fetch <=PDOstatement::fetch()
I can't find anything about private properties, also I can't be sure if the data is the correct one this would be similar to sending an array or stdobj to the constructor. I would still need to send a object into my constructor so I would know that the data given is the correct one.
Link to comment
Share on other sites

have you yet check the manual about it? sorry, this the correct link.http://au.php.net/manual/en/pdostatement.fetch.php

I can't find anything about private properties
what properties are you referring?
I would still need to send a object into my constructor so I would know that the data given is the correct one.
can you elaborate more? what are you trying to do actually?
Link to comment
Share on other sites

have you yet check the manual about it? sorry, this the correct link.http://au.php.net/ma...ement.fetch.php what properties are you referring? can you elaborate more? what are you trying to do actually?
I looked in the manual and yes PDO::FETCH_CLASS is a good method but I can't see if it can populate private properties.Also I want some kind of filter so you can't populate wrong data by mistake. This is one method of doing this in one SQL query instead of many:
<?phpclass Person {   private $fname;  private $lname;  private $id;   /**   *   * An empty constructor will create a new person.   *   */  public function __construct(CollectPersons $person=null){     if($person){	$this->fname = $person->getData()['fname'];	$this->lname = $person->getData()['lname'];	$this->id = $person->getData()['id'];   }	  }   public function getFname(){     return $this->fname;    }   public function getLname(){     return $this->lname;    }   public function getID(){     return $this->id;    }   public function setFname($fname){     $this->fname = $fname;    }   public function setLname($lname){     $this->lname = $lname;    } } class CollectPersons {   private $persons = array();  private $result;   public function getPersons(){     if(empty($this->persons)){	if($query = mysql_query("SELECT * FROM `person`")){		 while($row = mysql_fetch_assoc($query)){		  $this->result = $row;	  $this->persons[] = new Person($this);		 }		}else{		 return false;		}   }     return $this->persons;    }   public function getData(){     return $this->result;    } }

But as I stated out before this is the best solution I could come up with based on my design pattern I don't know if this approach is good at all or if there are better once regarding OOP.

Link to comment
Share on other sites

I can't see if it can populate private properties.
Yes it can.it will assign the properties by the column names of database. So any memeber is previously declared as private it's visibility will be still same. if it does not exist new member will be created with public visibility.
Also I want some kind of filter so you can't populate wrong data by mistake.
What do you mean? the data is already coming from your database rather than getting it in array by looping it. you can use fetchAll() which will return an array of of the object of the specified class.
Link to comment
Share on other sites

What do you mean? the data is already coming from your database rather than getting it in array by looping it. you can use fetchAll() which will return an array of of the object of the specified class.
I want to be sure that the data getting in to the object is from the right table. This is why this row in this example is so special:
public function __construct(CollectPersons $person=null){

It will only accept objects which are of the CollectPersons class as an argument, so I can be sure the data given is from the right table and not something that could look like the right data.

Link to comment
Share on other sites

I'm not clear on what your problems are. It's easy to get data from your database into an object, what else are you trying to do that's causing a problem? It sounds like you have an issue with verification.

Link to comment
Share on other sites

I'm not clear on what your problems are. It's easy to get data from your database into an object, what else are you trying to do that's causing a problem? It sounds like you have an issue with verification.
My issue is I don't really know any good ways to populate my objects I came up with one but did feel like it was the best way of doing it. The way of PDO is one way but feels like it doesn't scope with the idea I read about the OOP. Feels like I take out the idea of using classname/interface with the argument in a method. Yes I could use a validation on the data but could I be sure I got the right data like I can be then asking for specific classname/interface. I can't find any good articles on the web of this topics either. Just haven't seen many example on doing this. :/
Link to comment
Share on other sites

Yes I could use a validation on the data but could I be sure I got the right data like I can be then asking for specific classname/interface.
This is what I'm confused about. How are you ever sure you got the right data? You just sort of trust the database that when you ask for a row with a specific ID, for example, that it returns the correct data. I'm not sure what your issue is with making sure you got the right data, how do you make sure you got the right data now? If you're looking for a way to auto-populate a custom object from a database, you're not going to find that. What you can do is pass the result set into the constructor, or an array of data from the result, and the constructor will populate the instance like it needs to. The constructor can validate to make sure all of the correct fields are there.
Link to comment
Share on other sites

This is what I'm confused about. How are you ever sure you got the right data? You just sort of trust the database that when you ask for a row with a specific ID, for example, that it returns the correct data. I'm not sure what your issue is with making sure you got the right data, how do you make sure you got the right data now? If you're looking for a way to auto-populate a custom object from a database, you're not going to find that. What you can do is pass the result set into the constructor, or an array of data from the result, and the constructor will populate the instance like it needs to. The constructor can validate to make sure all of the correct fields are there.
Yes that is kind of what I do now but I fetch the array from the object collecting the result so I know that I made the query for it. I would still need to validate the data (Just haven't thought about it yet). But at least I could be sure the data is from table a and not b or isn't made up. I know it still could be over written but then it would be on purpose and not by sending the wrong array into the mix. I just feel is something else I missed something simpler I haven't thought of.
Link to comment
Share on other sites

The problem is you are not asking the right question.

The way of PDO is one way but feels like it doesn't scope with the idea I read about the OOP
if you specify the class name the object will be the instance of that class. so if you pass 'collectPerson' with PDO::FETCH_CLASS it will be object of collectPerson. As per view of the design of the classes your collectPerson class is doing nothing about but the person object. how about storing those objects as array of person object? that is where array meant to be used. It is issue with your class design. it would be better if you put the getPerson() in person class and use it to fetch person data. I dont know what else to stay. i cant even show you some example or work toward further with PDO as you have not provided any codes with PDO yet.
Link to comment
Share on other sites

The problem is you are not asking the right question. if you specify the class name the object will be the instance of that class. so if you pass 'collectPerson' with PDO::FETCH_CLASS it will be object of collectPerson. As per view of the design of the classes your collectPerson class is doing nothing about storing the person object. how about storing those objects as array of person object? that is where array meant to be used. It is issue with your class design. it would be better if you put the getPerson() in person class and use it to fetch person data. I dont know what else to stay. i cant even show you some example or work toward further with PDO as you have not provided any codes with PDO yet.
CollectPerson is storing the Person object just look in the while loop and you will see the assigning of the object to the persons property. I haven't used PDO cause I never used it before I don't really know why I should either. I understand that I could use any database I want with the same function but that isn't really needed for me at this point. Auto escaping sure it's a good thing but I could simply write my own instead of loading the hole PDO class.
Link to comment
Share on other sites

yes that is why i was telling you to check the PDO and more specificaly fetchAll() function. fetchAll() will return the array of person object. you dont even need to loop through or manage another class for doing it. collectPerson is not a different entity it is just array of person object. it is not more complicated than that. and mysql api is already obsolete now. you should switch back to mysqli or PDO. PDO loads from the dll so it will not make any effect on your performance.

Link to comment
Share on other sites

Might have done some over thinking on how I wanted things to look like while still wanting a new point of view. I had a look at the PDO again and yes it might suite the need I'm looking for. I just couldn't place it in my thoughts and it felt wrong. Thanks for the answers I got :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...