Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord 1.0 pre-release

[eluser]ardinotow[/eluser]
This is my libraries/controller.php.
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the modules library */
require_once 'Modules.php';

class Controller
{    
    /** Constructor **/
    public function __construct()
    {    
        /* set this controller name */
        $class = strtolower(get_class($this));
        
        /* create a new loader */
        $this->load = (class_exists('MX_Loader')) ? new MX_Loader($class) : new Loader($class);
        
        log_message('debug', ucfirst($class)." Controller Initialized");
        
        /* assign core libraries */
        $this->_assign_libraries();    
        
        /* register this controller */
        modules::$registry[$class] = $this;
        
        /* autoload module items */
        $this->load->autoload();
        
        /* activate output profiler from config */
        if ($this->config->item('enable_profiler')) $this->output->enable_profiler();
    }

    /** PHP compatibility **/
    public function Controller()
    {
        self::__construct();
    }
    
    /** Return protected $autoload array **/
    public function autoload()
    {
        return (isset($this->autoload)) ? $this->autoload : FALSE;
    }

    /** Assign core libraries **/
    private function _assign_libraries()
    {
        if ($core = end(modules::$registry))
        {            
            foreach (get_object_vars($core) as $key => $object)
            {
                if (is_object($object) AND ! isset($this->$key))                
                    $this->$key = $object;    
            }
        }
        else /* executes only for the first controller */
        {
            /* the CI core classes */
            $classes = array(
                'config'    => 'Config',
                'input'        => 'Input',
                'benchmark'    => 'Benchmark',
                'uri'        => 'URI',
                'output'    => 'Output',
                'lang'        => 'Language',
                'router'    => 'Router',
            );
            
            /* assign the classes */
            foreach ($classes as $key => $class)
            {
                $this->$key = load_class($class);    
            }
            
            /* initialize CI_Base */
            CI_Base::__construct();
        }
    }
    //////////////////////////////////
    //
    //    patch for ignitedrecord
    //
    //////////////////////////////////
    function ORM($model = false, $name = FALSE, $db_conn = FALSE)
    {
        if(!class_exists('IgnitedRecord'))
        {
            // change this line if the IgnitedRecord file is stored elsewhere
            require_once(APPPATH.'models/ignitedrecord/Ignitedrecord.php');
        }
        
        if($model != false)
        {
            $this->model($model,$name,$db_conn);
        }
    }    
}

class Loader
{    
    public $_ci_classes;
    protected $_class, $_module;
    protected static $models, $autoload, $loader;
    
    public function __construct($class)
    {
        /* the class name */
        $this->_class = $class;
        
        /* the module name */
        $this->_module = modules::path();
        
        /* the CI_Loader is a singleton */
        (isset(self::$loader)) OR self::$loader = load_class('Loader');    
        
        /* get a reference to CI_Loader classes */
        $this->_ci_classes =& self::$loader->_ci_classes;
    }
    
    /** Load a module config file **/
    public function config($file = '', $use_sections = FALSE)
    {
        $ci = modules::$registry[$this->_class];
        
        ($file == '') AND $file = 'config';

        if (in_array($file, $ci->config->is_loaded, TRUE))
            return $ci->config->item($file);

        list($path, $file) = modules::find($file, $this->_module, 'config/');
        
        if ($path === FALSE)
        {
            self::$loader->config($file, TRUE);                    
            return $ci->config->item($file);
        }
        
        if ($config = modules::load_file($file, $path, 'config'))
        {
            /* reference to the config object */
            $current_config =& $ci->config->config;

            if ($use_sections === TRUE)
            {
                if (isset($current_config[$file]))
                {
                    $current_config[$file] = array_merge($current_config[$file], $config);
                }
                else
                {
                    $current_config[$file] = $config;
                }
            }
            else
            {
                $current_config = array_merge($current_config, $config);
            }

            $ci->config->is_loaded[] = $file;
            unset($config);
            
            return $ci->config->item($file);
        }
    }

    /** Load the database drivers **/
    public function database($params = '', $return = FALSE, $active_record = FALSE)
    {
        if (class_exists('CI_DB', FALSE) AND $return == FALSE AND $active_record == FALSE)
            return;

        require_once BASEPATH.'database/DB'.EXT;

        if ($return === TRUE)
            return DB($params, $active_record);
            
        $ci = modules::$registry[$this->_class];

        $ci->db = get_instance()->db = DB($params, $active_record);
        $this->_ci_assign_to_models();
        
        return $ci->db;
    }

    /** Load dbforge **/
    public function dbforge()
    {
        $ci = modules::$registry[$this->_class];
        
        self::$loader->dbforge();
        $ci->dbforge = get_instance()->dbforge;
        
        return $ci->dbforge;
    }
Continued...

[eluser]ardinotow[/eluser]
Code:
/** Load dbutil **/
    public function dbutil()
    {
        $ci = modules::$registry[$this->_class];
        
        self::$loader->dbutil();
        $ci->dbutil = get_instance()->dbutil;    
        $ci->dbforge = get_instance()->dbforge;
        
        return $ci->dbutil;
    }

    /** Load files **/
    public function file($path, $return = FALSE)
    {
        return self::$loader->file($path, $return);
    }

    /** Load a module helper **/
    public function helper($helper)
    {
        if (is_array($helper))
            return self::helpers($helper);
        
        if (isset(self::$loader->_ci_helpers[$helper]))    
            return;

        list($path, $_helper) = modules::find($helper.'_helper', $this->_module, 'helpers/');

        if ($path === FALSE)
            return self::$loader->helper($helper);

        modules::load_file($_helper, $path);
        self::$loader->_ci_helpers[$_helper] = TRUE;
    }

    /** Load an array of helpers **/
    public function helpers($helpers)
    {
        foreach ($helpers as $_helper) self::helper($_helper);    
    }

    /** Load a module language file **/
    public function language($langfile, $lang = '')
    {
        $ci = modules::$registry[$this->_class];
        
        $deft_lang = $ci->config->item('language');
        $idiom = ($lang == '') ? $deft_lang : $lang;
    
        if (in_array($langfile.'_lang'.EXT, $ci->lang->is_loaded, TRUE))
            return $ci->lang;
        
        list($path, $_langfile) = modules::find($langfile.'_lang', $this->_module, 'language/', $idiom);

        if ($path === FALSE)
        {
            self::$loader->language($langfile, $lang);
        }
        else
        {
            if($lang = modules::load_file($_langfile, $path, 'lang'))
            {
                $ci->lang->language = array_merge($ci->lang->language, $lang);
                $ci->lang->is_loaded[] = $langfile.'_lang'.EXT;
                unset($lang);
            }
        }
        
        return $ci->lang;
    }
    /** Load a module library **/
    public function library($library, $params = NULL, $object_name = NULL)
    {
        $ci = modules::$registry[$this->_class];
        
        $class = strtolower(end(explode('/', $library)));
        
        if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
            return $ci->$_alias;
            
        ($_alias = $object_name) OR $_alias = $class;
    
        list($path, $_library) = modules::find($library, $this->_module, 'libraries/');
        
        if ($path === FALSE)
        {
            self::$loader->_ci_load_class($library, $params, $object_name);
            $_alias = $this->_ci_classes[$class];
            $ci->$_alias = get_instance()->$_alias;
         }
        else
        {    
            /* load module config file as params */
            if ($params == NULL)
            {
                list($path2, $file) = modules::find($_alias, $this->_module, 'config/');    
                ($path2) AND $params = modules::load_file($file, $path2, 'config');
            }            
            
            modules::load_file($_library, $path);
            
            $library = ucfirst($_library);
            $ci->$_alias = new $library($params);
            $this->_ci_classes[$class] = $_alias;
        }

        $ci->$_alias->CI = $ci;
        $this->_ci_assign_to_models();
        
        return $ci->$_alias;
    }

    /** Load a module model **/
    public function model($model, $object_name = NULL, $connect = FALSE)
    {
        $ci = modules::$registry[$this->_class];

        ($_alias = $object_name) OR $_alias = strtolower(end(explode('/', $model)));
        
        if (isset(self::$models[$_alias]))
            return $ci->$_alias;
        
        list($path, $model) = modules::find($model, $this->_module, 'models/');

        (class_exists('Model', FALSE)) OR load_class('Model', FALSE);

        if ($connect !== FALSE)
        {
            if ($connect === TRUE) $connect = '';
            $this->database($connect, FALSE, TRUE);
        }

        modules::load_file($model, $path);

        $model = ucfirst($model);
        $ci->$_alias = new $model();
        self::$models[$_alias] = $ci->$_alias;
        
        $ci->$_alias->_assign_libraries();
        
    //////////////////////////////////
    //
    //    patch for ignitedrecord
    //
    //////////////////////////////////

        $CI =& get_instance();
        if( ! isset($CI->$_alias))
        {
            $CI->$_alias = $this->$_alias;
        }    
    
    
        return $ci->$_alias;
    }

    /** Load a module controller **/
    public function module($module)
    {
        if (is_array($module))
            return self::modules($module);
        
        $ci = modules::$registry[$this->_class];

        /* get the controller name */
        $controller = strtolower(end(explode('/', $module)));

        (isset($ci->$controller)) OR $ci->$controller = modules::load($module);
            
        return $ci->$controller;
    }

    /** Load an array of controllers **/
    public function modules($modules)
    {
        foreach ($modules as $_module) self::module($_module);    
    }

    /** Load a module plugin **/
    public function plugin($plugin)
    {
        if (isset(self::$loader->_ci_plugins[$plugin]))    
            return;

        list($path, $_plugin) = modules::find($plugin.'_pi', $this->_module, 'plugins/');    
        
        if ($path === FALSE)
            return self::$loader->plugin($plugin);

        modules::load_file($_plugin, $path);
        self::$loader->_ci_plugins[$plugin] = TRUE;
    }
continued...

[eluser]ardinotow[/eluser]
Now, this is the last one.
Code:
/** Load scripts **/
    public function script($scripts = array())
    {
        return self::$loader->script($scripts);
    }

    /** Load variables into output buffer **/
    public function vars($vars = array())
    {
        return self::$loader->vars($vars);
    }

    /** Load a module view **/
    public function view($view, $vars = array(), $return = FALSE)
    {
        list($path, $view) = modules::find($view, $this->_module, 'views/');
        
        self::$loader->_ci_view_path = $path;
        
        return self::$loader->_ci_load(array('_ci_view' => $view, '_ci_vars' => self::$loader->_ci_object_to_array($vars), '_ci_return' => $return));
    }

    /** Assign libraries to models **/
    public function _ci_assign_to_models()
    {
        if (is_array(self::$models))
        {
            foreach (self::$models as $model)
            {
                $model->_assign_libraries();
            }
        }
    }

    /** Autload items **/
    public function autoload()
    {
        $ci = modules::$registry[$this->_class];
        
        /* process application autoload */
        if ( ! isset(self::$autoload) AND self::$autoload = TRUE)
            self::$loader->_ci_autoloader();
        
        /* controller autoload array */
        ($autoload = $ci->autoload()) OR $autoload = array();
        
        list($path, $file) = modules::find('autoload', $this->_module, 'config/');
        
        /* module autoload file */
        if ($path != FALSE)                
            $autoload = array_merge(modules::load_file($file, $path, 'autoload'), $autoload);
        
        /* nothing to do? */
        if (count($autoload) == 0) return;
        
        /* autoload database & libraries */
        if (isset($autoload['libraries']))
        {
            if (in_array('database', $autoload['libraries']))
            {
                /* autoload database */
                if ( ! $db = $ci->config->item('database'))
                {
                    $db['params'] = 'default';
                    $db['active_record'] = TRUE;
                }

                $ci->db = $this->database($db['params'], TRUE, $db['active_record']);
                $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
            }

            /* autoload libraries */
            foreach ($autoload['libraries'] as $library)
            {
                $this->library($library);
            }
        }        
                
        /* autoload config */
        if (isset($autoload['config']))
        {
            foreach ($autoload['config'] as $key => $val)
            {
                $this->config($val, TRUE);
            }
        }

        /* autoload helpers, plugins, scripts, languages */
        foreach (array('helper', 'plugin', 'script', 'language') as $type)
        {
            if (isset($autoload[$type]))
            {
                foreach ($autoload[$type] as $item)
                {
                    $this->$type($item);
                }
            }
        }

        /* autoload base classes */
        if (isset($autoload['class']))
        {
            foreach ($autoload['class'] as $class)
            {
                modules::autoload($class);
            }
        }        

        /* autoload models */
        if (isset($autoload['model']))
        {
            foreach ($autoload['model'] as $model => $alias)
            {
                (is_numeric($model)) ? $this->model($alias) : $this->model($model, $alias);
            }
        }

        /* autoload module controllers */
        if (isset($autoload['modules']) AND ! in_array($this->_class, $autoload['modules']))
        {
            foreach ($autoload['modules'] as $module)
            {
                $this->module($module);
            }
        }
    }
}
Please correct if I'm wrong

[eluser]m4rw3r[/eluser]
Try moving the ORM() method to the Loader class, because it asks for a Loader::orm() method (this is probably because the instructions was for ME - HMVC version 5.0).

I'm going to recheck ME - HMVC integration for IR 1.0 (because the instructions are also for an older version of IR).

[eluser]Muser[/eluser]
I'm using ME 5.1.40 with IR and for now seems to work all ok

I can't paste all the libraries/Controller.php code here because of character limit in posts.

So I've posted it here:

http://pastebin.com/m740f1f5e

As m4rw3r said, you need to move ORM method inside Loader class

[eluser]ardinotow[/eluser]
wow great, it works!...m4rw3r & muser thanks for the help.
@m4rw3r: I think the documentation lack in some function explanation, e.g. related() and validation reference.

[eluser]julien.1486[/eluser]
Hi there !

Thank you very much for your ORM and for your work, m4rw3r (and all others, of course)!

I don't know if it is the right place (if it's not you can hit me) but I have a problem in deleting a row.

I don't have any error messages, but the row isn't deleted.

My function's controller :
Code:
function del_news($news_id){
            $this->load->orm('news');
            $news = $this->news->find($news_id);
            
            if($news!=false){
                $news->delete();
                $this->session->set_flashdata('status', 'La news sélectionnée a été supprimée.');
            }
            else{
                $this->session->set_flashdata('status', 'La news sélectionnée n\'exite pas. Veuillez contacter le webmaster.');
            }
            
            redirect('fiche/index/'.$this->session->userdata('id'), 'refresh');                
        }

I know that $news isn't false because my "flashdata" is displayed next, and because a echo $news->title is working.
I have enabled the profiler in order to show queries and there isn't one containing 'Delete From ...etc...'.

The 'news' model look like this :
Code:
<?php
    Class news extends IgnitedRecord{
        var $model = 'news';
        var $table = 'news';

        var $belongs_to = 'user';
    }
?>

Is it something wrong in my code ?

Thanks a lot for your answers, and I'm sorry to bother you.

[eluser]m4rw3r[/eluser]
Can you supply me with a idump() of the $news object and also any IR related record log messages (via PM)?

Also check the return value from the $q->delete() at the last line of IgnitedRecord::delete() (I know that there has been some problems when the id is 0, going to correct them).

EDIT: Seems like a problem with the revision 218, I recommend updating to a later revision

[eluser]Cannyp[/eluser]
<?
Hi there,

I have some related tables like so.

Code:
class Order extends IgnitedRecord {
    var $table = "orders";
    var $has_many = array(
        array('table'=>'files','fk'=>'order_id','model'=>'file'),
        array('table'=>'deliveries','fk'=>'order_id','model'=>'delivery')
        );
}

class File extends IgnitedRecord {
    var $table = "files";
    var $belongs_to = array('table'=>'orders','fk'=>'order_id','model'=>'order');
    var $has_many = array('name' => 'orderf', 'table'=>'file_deliveries','fk'=>'file_id','model'=>'filedelivery');
}

class DeliveryPoint extends IgnitedRecord {
    var $table = "delivery_points";
    var $has_many = array('name' => 'fd', 'table'=>'file_deliveries','fk'=>'delivery_point_id','model'=>'filedelivery');
}

class EdoFileDelivery extends IgnitedRecord {
    var $table = "file_deliveries";
    var $belongs_to = array(
        array('name' => 'dp', 'table'=>'delivery_points','fk'=>'delivery_point_id','model'=>'deliverypoint'),
        array('name' => 'fi', 'table'=>'files','fk'=>'file_id','model'=>'file')
        )
    ;
}

So, an order has many files and many delivery points and file deliveries is a many to many between files and deliver_points.

Code:
$order = $this->order->find($id);
$files = $order->related('files')->where('files.deleted', '0')->get(); // works fine
$deliveries = $order->related('delivery_points')->where('delivery_points.deleted', '0')->get(); //works fine

When I come to load all the file_deliveries, I want to join back onto the files table to get the filename. I have tried various combinations of related() and join_related() but can't seem to figure it out. Can anyone shed any light on what Im trying to do?

Code:
foreach ($deliveries as $d)
{
    // get the file deliveries and join back to files table to get the filename
    $file_deliveries = $d->related('dp');
}

any help much appreciated.

[eluser]porky207[/eluser]
Hi, I'm getting an error when trying to make IR return record objects as a custom class extending IR_record. Considering how simple the child class is, I think I'm doing something wrong somewhere else. Do you have any idea? Thank you in advance! /p

Quote:A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for IR_record::IR_record(), called in C:\xampplite\htdocs\ci\system\libraries\Loader.php on line 184 and defined
Filename: ignitedrecord/record.php
Line Number: 97
(+ several subsequent errors, will post if needed.)

My code looks like this:

models/Map.php
Code:
<?php
class Map extends IgnitedRecord{
    var    $table        =    'map';
    var $child_class    =    'map_record';
?>

models/Map_record.php
Code:
<?php
class Map_record extends IR_record{
(nothing here yet--first I'm trying to make it work)
}
?>

[quote author="m4rw3r" date="1226536928"]@Sander Versluys:

IgnitedRecord uses the DataMapper pattern (not to be confused with stensi's DataMapper ORM), and the model and data objects are separate classes.
So to add a method to the model, just add it to the model class.

But if you would like to add a method to a record, you have to create a descendant class of IR_record, assign your methods to that class, then set $child_class in the model to the class name:
Code:
class User_model extends IgnitedRecord{
    var $child_class = 'user_record'; // the name of the class to create

    // define some more things here, just as usual
}

class user_record extends IR_record{
    function my_func()
    {
        echo $this->name;
    }
}
This means you can make generic record objects (but more specialized than IR_record), and reuse them with different models.
http://www.assembla.com/spaces/IgnitedRecord/documents[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB