Welcome Guest, Not a member yet? Register   Sign In
Need Help w/ ORM Library
#1

[eluser]Michael Wales[/eluser]
I'm working on my own ORM Library and I can't seem to figure this part out.

First off, here's the library (obviously, not complete):

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class Orm extends Model {

    var $insert_id;

    function Orm($owners = array()) {
        parent::Model();
    }
    
    // Attempts to save an object (INSERT if no ID is provided, UPDATE if one is)
    function save() {
        // Create an array to save, of only thos values defined in the models setter
        $this->load->helper('date');
        foreach ($this->set as $setter) {
            $save[$setter] = $this->$setter;
        }
        
        // Perform the INSERT/UPDATE as required
        $now = date('Y-m-d h:i:s', now());
        if (!isset($save['id'])) {
            $save['created_on'] = $now;
            $save['updated_on'] = $now;
            $query = $this->db->insert($this->table, $save);
            $this->insert_id = $this->db->insert_id();
        } else {
            $save['updated_on'] = $now;
            $query = $this->db->update($this->table, $save, array('id' => $save['id']));
            $this->insert_id = $this->id;
        }
        
        // Return a useful result to the user
        if ($this->db->affected_rows() == 1) {
            return TRUE;
        }
        return FALSE;
    }
    
    function get_related() {
        if ($this->has_many) {
            $id = substr($this->table, 0, -1) . '_id';
            $var = $this->has_many
            $this->$var = $this->_get_related($this->has_many, array($id => $this->insert_id));
        }
    }
    
    function _get_related($table, $search) {
        $query = $this->db->get_where($table, $search);
        if ($query->num_rows() > 0) {
            return $query->result();
        }
        return FALSE;
    }

}

The issue I am having is within the get_related() method, particularly this line:
Code:
$var = $this->has_many
            $this->$var = $this->_get_related($this->has_many, array($id => $this->insert_id));

Let's take a look at my model - to see exactly what we're trying to accomplish here:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');

class Account extends Orm {

    var $table = 'accounts';
    var $has_many = 'users';
    
    var $id;
    var $full_name;
    var $email;
    var $type;
    var $created_on;
    var $updated_on;
    var $set = array('id', 'full_name', 'email', 'type');
    
    function Account() {
        parent::Orm();
    }
}

So, Accounts has_many Users - and the get_related method, should equates to the following (in theory):
Code:
// All in all: $this->db->get_where('users', array('account_id'=>1));
function get_related() {
    if ($this->has_many) {
        $id = substr('accounts', 0, -1) . '_id'; // account_id
        $var = 'users'
        $this->$var = $this->_get_related('users', array('account_id' => $this->insert_id));
        }
    }

If I hardcode the $this->$var portion to $this->users, it works perfectly fine. Obviously, this is not the best solution because it won't work for every possible scenario.

Here's the Controller so you can see the "end developer" code and how I would like this to function.

Code:
function index() {
        $this->load->model('account');
        $a = new Account();
        $a->full_name = 'Michael Wales';
        $a->email = '[email protected]';
        $a->type = 'gold';
        if ($a->save()) {
            $this->load->model('user');
            $u = new User();
            $u->account_id = 1;
            $u->email = '[email protected]';
            $u->password = 'wizard';
            $u->salt = 'aks9di';
            $u->save();
            
            $u = new User();
            $u->account_id = 1;
            $u->email = '[email protected]';
            $u->password = 'wizard';
            $u->salt = 'asksd';
            $u->save();
            
            $a->get_related();
            foreach ($a->users as $user) {
                echo 'Email: ' . $user->email;
            }
        } else {
            echo 'Account not saved.';
        }
    }

Any ideas? The error I get is:
Quote:Parse error: syntax error, unexpected T_VARIABLE in E:\xampplite\htdocs\tiara\libraries\MY_Model.php on line 43
#2

[eluser]Michael Wales[/eluser]
Holy smokes... I'm retarded. This one piece of code has been giving me such a hard time - I've been trying everything.

This line was a last minute "attempt":
Code:
$var = $this->has_many
$this->$var = $this->_get_related($this->has_many, array($id => $this->insert_id));

In my frustration, I failed to notice the missing semicolon on the first line. Add that in - and everything works as expected.

Leaving this post up so everyone can look at the new hotness I'll be posting on my blog when it's completed. Big Grin
#3

[eluser]Rick Jolly[/eluser]
Michael, that might just justify an editor upgrade.
#4

[eluser]stuffradio[/eluser]
At least you figured it out Smile
#5

[eluser]Daniel Moore[/eluser]
Every time I get that error (not as much as I used to) the missing semicolon is the first thing I always look for. Anytime there's an unexpected T_anything, in most cases it's because of a missing semicolon, parenthesis, bracket or brace.

Looking forward to seeing the complete stuff on the blog.




Theme © iAndrew 2016 - Forum software by © MyBB