-1

This is my second post today because I'm very new at OOP in PHP, so bear with me if it's a silly question...

I've created a beautiful class that I want to be able to use with my "JOB" objects that includes being able to use this single object to pull data from many tables so the end user sees relevant data. For instance, in my class, I have:

public function showProjectManager() {
    $q = "SELECT m.name FROM " . jJOBS . " AS j JOIN " . jMEI . " AS m ON m.uid = j.mei WHERE id = " . $this->id;
    $r = $this->_dblink->query($q);
    $row = $r->fetch_assoc();
    return $row['name'];
}

because the data stored in the jJOBS table is merely a number that references an id in another table. There are several pieces of data like this stored in the main jJOBS table.

So, in another section of code there is a dropdown box that lets the user select a specific "JOB" and using an AJAX call I want to display the data from that JOB. What I'm trying to do is create a JOB object (which includes the showProjectManager method), read all the properties from a database using mysqli_fetch_object and then call the showProjectManager method, like this:

    $disp = new Job();
    $q = "SELECT * FROM " . jJOBS . " WHERE id = " . $job_id;
    $r = $jobsdb->query($q);
    $disp = $r->fetch_object();
    $zList .= "<li><span class='smLabels l'>Project Manager :</span>\n<span class='smText r'>" . $disp->showProjectManager() . "</span></li>";

But whenever I fetch the object from the DB, the $disp object reverts back to stdClass. How can I maintain the proper class and access the values I need from the DB? Everything I have read on the subject calls for the use of a DataMapper which seems incredibly complex, especially for this scenario.

PeeHaa
  • 66,697
  • 53
  • 182
  • 254
DevlshOne
  • 7,960
  • 1
  • 25
  • 33
  • 2
    Please , learn how to use prepares statements. Just because you use newer API does not make it automagically secure. – tereško Oct 31 '12 at 01:41
  • I'm not sure what your comment has to do with anything here beside ramping up your user stats. However, if I am translating this properly, your telling me to use prepared statements rather than direct queries. Well, sir, not that it is any of your business or anything but we are about to migrate this data over to a SQL server, which means all of my database code is going to change. This is also why I am teaching myself OOP - I am writing a new class for SQL in tandem with the rest of this project. – DevlshOne Oct 31 '12 at 01:57
  • 2
    @DevlshOne Comments don't really "ramp up stats" in any way, shape, or form. You also really should learn prepared statements if you want to join the future. – SomeKittens Oct 31 '12 at 02:16
  • Where does `$job_id` come from? – uınbɐɥs Oct 31 '12 at 02:18
  • @FewPussies, I am fully aware of how to use prepared statements. However, I chose, in this case, to NOT use them since I am re-writing the database class for a SQL server. – DevlshOne Oct 31 '12 at 19:28

2 Answers2

1

Easiest approach is to pass a class name to fetch_object, like so:

$q = "SELECT * FROM " . jJOBS . " WHERE id = " . $job_id;
$r = $jobsdb->query($q);
$disp = $r->fetch_object("Job");

Per the documentation, this will create $disp as an object of type Job with all its fields filled out, then call the Job constructor.

duskwuff -inactive-
  • 171,163
  • 27
  • 219
  • 269
  • Isn't the correct answer always "RTFM!"? LOL! Thanks!!.. I had no idea you could pass a classname to the fetch_object method. – DevlshOne Oct 31 '12 at 01:05
0

You are resetting the $disp variable. You need to set the job in the job object. Pass the id to the constructor like this.

class Job {
    private $job = null;

    function __construct($id = null) {
        if(isset($id)) {
            $q = "SELECT * FROM " . jJOBS . " WHERE id = " . $job_id;
            $r = $jobsdb->query($q);
            $job = $r->fetch_object();
            $this->job = $job;
        }
    }

    function getJob() {
        return $this->job;
    }

    public function showProjectManager() {
        $q = "SELECT m.name FROM " . jJOBS . " AS j JOIN " . jMEI . " AS m ON m.uid = j.mei WHERE id = " . $this->job->id;
        $r = $jobsdb->query($q);
        $row = $r->fetch_object();
        return $row->name;
    }
}

?>

Then

$disp = new Job(123);
$disp->someJobObjectFunction();
shapeshifter
  • 2,889
  • 1
  • 23
  • 36
  • You also might want to add LIMIT 1 to the end of your job select query. – shapeshifter Oct 31 '12 at 00:50
  • This doesn't seem to be doing what I want. This is returning a "job" property from within the "JOB" object that is of stdClass. If you add print_r($disp) to that code, you'll see what I mean. – DevlshOne Oct 31 '12 at 01:02