1

I've got a couple of model classes handling a db layer, and I've got a logic error in how to hand the db connection to the query in an OO way. Class1 is a db_connect which I'm calling inside the constructor of class2 query methods:

class db {
    public $host = 'localhost';
    public $username = 'root';
    public $password = '';
    public $database = 'molecule';
    public $mysqli = '';

   function __construct() {
        $mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
            if (mysqli_connect_errno()) {
                printf("Database Connection failed: %s\n", mysqli_connect_error());
                exit();
            }
        return $mysqli;
    }   
}

class nodeModel {
    public $node;
    public $node_name;
    public $node_link;
    public $node_comment;
    public $mysqli;

    function __construct() {
        $mysqli = new db;
        return $mysqli;
    }

    public $insert;
    public $insert_id;
    function insertNode($node_name, $node_link, $node_comment) {
            $this->insert = $insert;
            $this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");
            $this->insert->bind_param("sss", $this->node_name, $this->node_link, $this->node_comment);
            if($this->insert->execute()) {
                $this->insert_id = $mysqli->insert_id;
            } 
            return $this->insert_id;
            print_r($this->insert_id);
            $this->insert->close();
        }}

But I'm getting an undefined variable error with $insert.

I am calling that layer with the following:

include 'node_model.php';
$dbTest = new nodeModel;
$node_name = 'My Node Title';
$node_link = 'My Node Link';
$node_comment = 'My Node Comment. This one should be longer so I will write more stuff';
$dbTest->insertNode($node_name, $node_link, $node_comment);

The way I'm connecting with the constructor of the query class seems to be working but not sure which variable I need in scope to attach to the prepare/bind/execute statements?

And I do know that I should be using PDO or another ORMy type tool. Which I will, but this is more about some basic OO get/set/retrieve best practice ... thanks for your help.

shotdsherrif
  • 433
  • 2
  • 10
  • 20
  • PDO is not "ORMy type tool". And I would [disagree](http://stackoverflow.com/a/5864000/727208) with your understanding of what Model is. – tereško Jun 13 '12 at 10:06

2 Answers2

1

There are some major problems with Your code...

1. Make Your model class properties private and create setter and getter methods to set or retrieve a value to/from property. Also Your method insertNode is wrong.

class nodeModel {
    private $node;
    private $nodeName;
    private $nodeLink;
    private $nodeComment;
    private $mysqli;

    function __construct() {
        $this->mysqli = new db;
    }

    public function getNodeName() {
        return $this->nodeName;
    }

    public function getNodeLink() {
        return $this->nodeLink;
    }

    public function getNodeComment() {
        return $this->nodeComment;
    }

    public function setNodeName($value) {
        $this->nodeName = $value;
    }

    public function setNodeLink($value) {
        $this->nodeLink = $value;
    }

    public function setNodeComment($value) {
        $this->nodeComment = $value;
    }

    function insertNode() {
        $this->insert = $this->mysqli->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->insert->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->insert->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->insert->close();

        print_r($this->insert_id);

        return $this->insert_id;
    }}


}

2. In class db when You create a connection store it into $this->mysqli, not just $mysqli.

function __construct() {
    $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
        if (mysqli_connect_errno()) {
            printf("Database Connection failed: %s\n", mysqli_connect_error());
            exit();
        }
    return $this->mysqli;
}

3. In Your third code, you create only a variables that You pass to the method insertNode but within this method You do nothing with the passed variables - You expect that the class properties are set but they are not... Therefore do this:

include 'node_model.php';
$dbTest = new nodeModel;
$dbTest->setNodeName('My Node Title');
$dbTest->setNodeLink('My Node Link');
$dbTest->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
$dbTest->insertNode();

Hope this helps...

shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • Thanks for your thorough answer. You can see my confusion was with get/set conventions in OOP and the extra work that it creates when you've done things in procedural. – shotdsherrif Jun 13 '12 at 22:20
  • What I find interesting is that you have to assign the variables with functions and then they're in scope/ i.e. part of the class object, so the insert method doesn't need to have them passed in as arguments. – shotdsherrif Jun 13 '12 at 22:23
  • Yes. This is one approach. A little easier is to have all the properties public, then You do not have to have getter/setter methods and directly assign values as `$node->title = 'My Title';` and then again call `$node->insertNode();` to save it as new... – shadyyx Jun 14 '12 at 08:26
-1

But I'm getting an undefined variable error with $insert.

That's because the first lines of your function are

$this->insert = $insert;
$this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");

$insert is not in your arguments list, so the first line causes an error. And then in your second line, you overwrite the variable anyway.

You also have 2 lines of code after the return statement, which of course never execute.

Perhaps it's time to get some sleep? :)

Felix Weelix
  • 352
  • 3
  • 7
  • The problem is sure not because of the first lines of the method `insertNode`... -1 – shadyyx Jun 13 '12 at 08:52
  • Sorry, that's the only problem I can see that you're asking about. – Felix Weelix Jun 13 '12 at 08:58
  • Ah, sorry you're not the OP. That is however the only error he reported. – Felix Weelix Jun 13 '12 at 08:59
  • You sure that his code is totaly functional and will do what the OP wants to do after repairing that first two lines? I don't think so... If the SE was only about answering to questions, then anybody can come and answer a question... But we are supposed to help, to solve the problems, let the OPs to understand what is wrong... – shadyyx Jun 13 '12 at 09:16
  • I just thought you were a little harsh marking a newcomer's answer down as "unhelpful" - just because the OP clearly has some misconceptions about variable scope in PHP it doesn't mean that dealing problem A (the one he reported) is "unhelpful" when the root of the problem is the same – Felix Weelix Jun 13 '12 at 09:34
  • Thx to both of you. I was aware my code was off because I had tried a couple of different things which generated different errors and I posted all of it - i.e. the overwritten assignment. My question was really about basic get and set in an OO environment - which requires a couple of extra steps as opposed to its procedural counterpart - and I'm just getting use to that. So yeah, the first response didn't really address my question, but I might have articulated it better also, no worries. – shotdsherrif Jun 13 '12 at 22:08