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

[eluser]ben_co[/eluser]
You're right! It's exactly the type of structure I use. And indeed, there are problems when it comes to relations because you must set up the array of keys that are involved in the process. It was why I asked that question :-). By the way, it's a great library though ;-)
#32

[eluser]Jeffrey04[/eluser]
Is there any way to know whether the result is being saved successfully? Does save() returns any value indicating whether the insertion/update is done successfully?

when I create a new record using new_record(array(...)), do i have to include the id element even though I don't have it? eg.
Code:
$this->model->new_record(array('gid' => '', 'field' => 'field_value');
$this->model->save();

i am getting an error if I do something like this (without specifying the id)
Code:
$this->model->new_record(array('field' => 'field_value'));
$this->model->save();
My Model
Code:
<?php
    class Group_Model extends IgnitedRecord {
        public $__table;
        public $__id_col;

        public function __construct() {
            parent::IgnitedRecord();
            
            // set the table relationship
            $this->__table = 'group';
            $this->__id_col = 'gid';
        }
    }

Quote:Message: Undefined index: gid
#33

[eluser]m4rw3r[/eluser]
1. No, not for the moment (I'll implement it)

2. No, you do not need to include the id element (or any other column which is not marked as NOT NULL in the db).

About the new_record() error:
It is an error in the constructor of the IgnitedRecord_record object (completely forgot that the array does not always contain the id :red: ).

Quick fix:
Code:
// at line 144 change:
$this->__id = $data[$instance->__id_col];
// to:
$this->__id = isset($data[$instance->__id_col]) ? $data[$instance->__id_col] : null;

Does anyone have any suggestions for version 0.1?

The list and revision behaviour will not be in 0.1, as I have not got enough time (and I haven't come up with a nice idea for the revision behaviour).
Also, the JOINS will come later (if they can, I have some problems with their flexibility, so I don't know (will make some benchmarks and see if the flexibility loss (from the JOIN methods) is justified)).
#34

[eluser]Jeffrey04[/eluser]
Thanks, may I know as well whether is it possible to put ignitedrecord into the libraries folder instead of models?
#35

[eluser]m4rw3r[/eluser]
It should be working wherever you like to place it, there are one condition though: The Model class must be loaded before IgnitedRecord.
So it's a bit more code if you want to put it in the libraries folder:
Code:
// of course, this is not the only way
class example extends Controller{
    function example(){
        if(!class_exists('Model'))
            load_class('Model', FALSE);
        require_once(APPPATH.'libraries/ignitedrecord.php');
        // load your extended model here
        // factory example:
        $this->pages = IgnitedRecord::factory('pages',
            array('__table' => 'tree_table',
                  '__belongs_to' => array('table' => 'authors',
                                          'col' => 'writer')
                 ));
    }
}
I've made a custom loader for IgnitedRecord, which will be available in 0.1
the usage:
Code:
class example extends Controller
    function example(){
        $this->load->ORM();
        $this->load->model('modelname');
        // or:
        $this->load->ORM('modelname','anothername'); // it also works as a wrapper for the model() function
    }
}
Of course this loader is easily configurable (ok, it's only one setting), so you can put ignitedrecord.php anywhere you want.
#36

[eluser]Jeffrey04[/eluser]
Thanks for the responsive reply :red:

one more question, does the model call other related models if I run a save() method that save record into two databases?
#37

[eluser]m4rw3r[/eluser]
No, I'm afraid not.

But if you attach a newly created record (not saved) to a related one, both will be saved (they will be saved anyway, with the new data).
Example:
Code:
$rec1 = $this->page->new_record();
$rec1->title = 'A page';

$rec2 = $this->author->new_record();
$rec2->name = 'An author';

$rec1->add('author',$rec2); // both $rec1 and $rec2 will be saved here
#38

[eluser]Jeffrey04[/eluser]
* deleted *
#39

[eluser]m4rw3r[/eluser]
I made a benchmark of the JOIN and "normal" way of fetching relations, and I came up with a very surprising result:
Code:
Has Many (1 linked in table) 1000 repetitions:
Normal    3.1683
JOINs     4.6899
So according to the results, the normal way is both faster and more customizable than the JOINs.

The tests were made with both the server and database on the same computer, so they may differ (tell me if they differ a lot on your system, or something).

I guess I could have messed up somewhere in my code, so I post it here:
Code:
// controller:
$this->benchmark->mark('normal_start');
for($i = 0; $i < 1000; $i++){
    $obj = $this->thread->find(1);
    $obj->load_rel();
}
$this->benchmark->mark('normal_end');
$this->benchmark->mark('join_start');
for($i = 0; $i < 1000; $i++){
    $obj = $this->thread->findj(1);
}
$this->benchmark->mark('join_end');

// model
class thread extends IgnitedRecord{
    var $__has_many = 'posts';
    function page(){
        parent::Ignited();
    }
    function findj($id){
        $tables = array_merge(array($this->__table),array_keys($this->__belongs_to),array_keys($this->__has_many),array_keys($this->__has_one));
        $str = '';
        foreach($tables as $table){
            foreach($this->db->list_fields($table) as $col){
                $str .= $table.'.'.$col.' AS '.$table.'_'.$col.',';
            }
        }
        $this->db->select($str);
        $this->db->from($this->__table);
        $this->db->where("$this->__table.$this->__id_col",$id);
        foreach($this->__belongs_to as $table => $opts){
            $this->db->join($table, "$table.id = $this->__table.{$opts['col']}",'left');
        }
        foreach($this->__has_many as $table => $opts){
            $this->db->join($table, "$table.{$opts['col']} = $this->__table.$this->__id_col", 'left');
        }
        foreach($this->__has_one as $table => $opts){
            $this->db->join($table, "$table.{$opts['col']} = $this->__table.$this->__id_col", 'left');
        }
        $this->db->limit(1);
        $result = $this->db->get();
        array_shift($tables); // to remove "this" table
        $obj = $this->split_join($result->row_array(),$tables);
        return $obj;
    }
    function split_join($data,$tables){
        $CI =& get_instance();
        $res = array();
        $d = array();
        foreach($this->db->list_fields($this->__table) as $col){
            $d[$col] =& $data[$this->__table.'_'.$col];
        }
        $obj = $this->_dbobj2ORM($d);
        foreach($tables as $table){
            $d = array();
            foreach($this->db->list_fields($table) as $col){
                $d[$col] =& $data[$table.'_'.$col];
            }
            $modelname = $this->_get_modelname($table);
            $rel_name = isset($this->__related_tables[$table]) ? $this->__related_tables[$table] : $table;
            $obj->__relations[$rel_name] =& $CI->{$modelname}->_dbobj2ORM($d);
            if(!isset($obj->$rel_name) && $obj->__relations[$rel_name] != false){
                $obj->$rel_name =& $obj->__relations[$rel_name];
                $obj->__relation_properties[] = $rel_name;
            }
        }
        return $obj;
    }
}

BTW. I'll probably release 0.1 soon :-)
#40

[eluser]m4rw3r[/eluser]
IgnitedRecord 0.1.0 is out! :-)

I have now created a project space at assembla: IgnitedRecord
There are not very much there yet... But it will come with time.

Here are the changes:

- Limited support for multiple primary keys have been implemented
- save() now returns true if the INSERT/UPDATE query succeeded
- Added set_belongs_to(), set_has_many(), set_has_one() and set_habtm() (Has And Belongs To Many)
- factory() does not need the double underscores in the property names, double underscores are automatically added
- save() now only saves the record if it has been edited
- Because of the new save(), a $force option has been added
- The IgnitedRecord_tree have got get_descendants()
- A custom loader has been implemented.
- $__habtm can be used exactly like $__has_and_belongs_to_many, they are merged in the constructor
Bugfixes:
- id column does not exist when creating a new record
- Misspellings and typos in the manual

Feel free to comment and make more suggestions!




Theme © iAndrew 2016 - Forum software by © MyBB