Welcome Guest, Not a member yet? Register   Sign In
ORM
#1

[eluser]Clauddiu[/eluser]
Hey guys,

I want to make a small orm library and I run into some issues. I will also want to use this library outside codeigniter when I need.

This is call method:
Code:
$results = orm::related()->with(
array(
   "admin" => array(
       "relate" => null      
   ),
   "posts" => array(
       "relate" => "admin_id",      
   ),
   "comments" => array(
       "relate" => "posts_id",      
   ),
)
)->all();

The related method is just an instance of the orm object, with method is like this:
Code:
public function with()
{
    $args = func_get_args();

    if (!empty($args) && is_array($args))
    {
        foreach ($args as $table)
        {
            $this->_with[] = $table;
        }
    }

    return $this;
}

all method, for now I don't look up for any relations between the tables, i just want to be able to insert the new records into the previous rows
Code:
public function all()
    {
        if (isset($this->_with) && is_array($this->_with))
        {
            $rows = array();
            $last_table = null;
            foreach ($this->_with[0] as $table => $params)
            {
                if (!$last_table)
                {
                    $rows[$table] = $this->query("SELECT * FROM `{$table}`");
                    $last_table = $table;                    
                }
                else
                {                                      
                    $rows = $this->seek($last_table, $rows, $table, $this->query("SELECT * FROM `{$table}`"));
                    $last_table = $table;                  
                    
                }
                
            }
            
        }      
      
       return $rows;
    }

Here I think is the problem at the seek method:
Code:
protected function seek($key, $seek_data, $table, $data)
{
    $return = array();
    if (is_array($seek_data))
    {
        #print_r($seek_data); die;
        foreach ($seek_data as $k => $v)
        {                
            if (is_array($v))
            {                    
                $this->seek($key, $v, $table, $data);                    
            }
            else
            {
                if ($k == $key)
                {
                    $v[$table] = $data;
                }
                $return[$key] = $v;
            }

        }
    }      

    return $return;
}


I want to be able when all is done to do something like:
Code:
$results = orm::related()->with(
array(
   "admin" => array(
       "relate" => null      
   ),
   "posts" => array(
       "relate" => "admin_id",      
   ),
   "comments" => array(
       "relate" => "posts_id",      
   ),
)
)->all();

and then use it like this:
Code:
foreach($results as $admin)
{
    print $admin->name . "<br />";
    
    foreach ($admin->posts as $post)
    {
        print $post->title . "<br />";
        
        foreach ($post->comments as $comment)
        {
            print $comment->body . "<br >";
        }
        
    }
}

I'll really appreciate any help or suggestions.

Thank you,
Claudiu




Theme © iAndrew 2016 - Forum software by © MyBB