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

[eluser]Paul Apostol[/eluser]
Hi,
I'm tying to figure how this library works.
For the moment I have only one question:
IR supports table and joining table prefixes? How?
Thank you,
Paul

[eluser]amrnt[/eluser]
hi guys...
can any body help me ... the library works fine @ all ... but :

the model
Code:
class Post extends IgnitedRecord {

    var $act_as = 'timestamped';

    function __construct ()
    {
        parent::IgnitedRecord ();
    }
}

the controller
Code:
class Posts extends Controller {

    function Posts() {

        parent::Controller();

        $this->load->orm();
        $this->load->model('post', '', TRUE);

    }

function create()
{
$to_add = $this->post->new_record(array('text'=>$_POST['text'])    );
$to_add->save();
}
}

when i add a post ,it'll be added good...
but the 'created_at' field has no changes ... why?
is there something missed? please help me

thanks in advance

[eluser]m4rw3r[/eluser]
@paul Apostol:

The prefixing is done in IgnitedQuery, which is the query builder IR uses.
IgnitedQuery is meant to be a replacement for AR, and thus it fetches the db prefix from the database configuration.
(I haven't tested the prefixing enough, but the code seems to be good - tell me if you find any errors)

[eluser]Paul Apostol[/eluser]
Thanks m4rw3r,
Do you know where I can find an "how to" for the way I can set the prefixes?
I have to use the private methods? Can be set from the model?
Thank you

[eluser]m4rw3r[/eluser]
The prefixes you talk about is a global prefix which is in the whole CI.
So if you set the prefix to eg. "foo_" then the users table would be foo_users and the users_tags would become foo_users_tags.

Also on setting it: Ci Database configuration (dbprefix)

@amrtamimi:

Waiting for flojon, because he wrote the timestamped behaviour (but I'll take a look too)

[eluser]Paul Apostol[/eluser]
OK, I understood.
Really doesn't help me for my application/style of using tables.
I want to have prefixes for sets of tables and not for whole application, depending on the area for which I'm using the tables.
I'll try to see if I can make a little modification to the library.

[eluser]Paul Apostol[/eluser]
0.
Anybody used an autoload for the IR models?
1.
I get this error
PHP Fatal error: Call to undefined method Loader::orm()
2.
To have table prefixes independent from CI prefix I made some changes
add in database config
Code:
$db['default']['irprefix'] = Array("app_"=>Array('levels'));
In Ignitedquery
- constructor change: $this->q_prefix = $CI->db->irprefix;
function replace
Code:
function _prefix($str)
    {
        
        if(empty($this->q_prefix))
        {
            return $str;
        }
        elseif(strpos($str, '.')) // cannot start with a dot, just ignore false and zero
        {
            foreach($this->q_to_prefix as $table)
            {
                if(strpos($str, $table) !== false)
                {
                    if(is_array($this->q_prefix))
                    {
                        foreach($this->q_prefix as $k=>$v)
                            if(in_array($table, $v))
                                $str = preg_replace('@(?<=^|\s|\.|`|")'.$table.'(?=$|\s|\.|`|")@i', $k . $table, $str);
                    }
                    else
                        $str = preg_replace('@(?<=^|\s|\.|`|")'.$table.'(?=$|\s|\.|`|")@i', $this->q_prefix . $table, $str);
                }
            }
        }
        
        return $str;
    }
    
    function _add_prefix($str)
    {
        $this->q_to_prefix[] = $str;
        if(is_array($this->q_prefix))
        {    
            foreach($this->q_prefix as $k=>$v)
                if(in_array($str, $v))
                    return $k.$str;
        }
        else
            return $this->q_prefix . $str;
    }

- insert function replace:
$sql = $CI->db->_insert($this->_protect_identifiers($this->q_prefix.$table), array_map(array(&$this, '_protect_identifiers'), $keys), $set);
with
$sql = $CI->db->_insert($this->_protect_identifiers($this->_add_prefix($table)), array_map(array(&$this, '_protect_identifiers'), $keys), $set);
- update function replace
$sql = $CI->db->_update($this->_protect_identifiers($this->q_prefix.$table), $set, array($this->_build_where($this->q_where)), $this->q_order_by, $this->q_limit);
with
$sql = $CI->db->_update($this->_protect_identifiers($this->_add_prefix($table)), $set, array($this->_build_where($this->q_where)), $this->q_order_by, $this->q_limit);

Good Luck

[eluser]sofbas[/eluser]
@amrtamimi:
How does you're data model look like?

The behavior works fine for me, I did have an issue when the created_at column was a different name, but you can set that.

[eluser]m4rw3r[/eluser]
@paul Apostol:

That shouldn't pose any problems, you just need to autoload the 'database' and 'ingitedrecord/ignitedrecord' libs too.
This should work because the models are loaded after the libraries in CI's autoload.

The error you have there is because your loader does not have an orm() method, have you copied this to the loader? or are you using PHP 4?

[eluser]Paul Apostol[/eluser]
Hello,
I'm using php 5.2.6.
The autoload contains database, session and ignitedrecord/ignitedrecord
MY_Loader is in app/library folder.
$this->load->model() works.
As an extra I'm using HMVC 5.1. Could be the problem?


EDIT:
Autoloading done by ading the next code in IgnitedQuery class
Code:
static function autoload($class)
    {
        // Don't attempt to autoload CI_ or MY_ prefixed classes
        if (in_array(substr($class, 0, 3), array('CI_', 'MY_'))) return;
        // Prepare class
        $class = strtolower($class);
        // Prepare path
        $path = APPPATH . 'modules';
        // Prepare file
        $file = $path . '/' . $class . EXT;
        // Check if file exists, require_once if it does
        if (file_exists($file)) require_once($file);
        else
            // Do a recursive search of the path for the class
            IgnitedQuery::recursive_require_once($class, $path);
    }
    static function recursive_require_once($class, $path)
    {
        if ($handle = opendir($path))
        {
            while (FALSE !== ($dir = readdir($handle)))
            {
                // If dir does not contain a dot
                if (strpos($dir, '.') === FALSE)
                {
                    // Prepare recursive path
                    $recursive_path = $path . '/' . $dir;
                    // Prepare file
                    $file = $recursive_path . '/' . $class . EXT;
                    // Check if file exists, require_once if it does
                    if (file_exists($file))
                    {
                        require_once($file); break;
                    }
                    else if (is_dir($recursive_path))
                        // Do a recursive search of the path for the class
                        IgnitedQuery::recursive_require_once($class, $recursive_path);
                }
            }
            closedir($handle);
        }
    }
DM inspiration Wink

and just before the class:
Code:
spl_autoload_register('IgnitedQuery::autoload');

Just be carefully to replace in autoload function in case you don't use HMVC the word 'modules' with 'models'




Theme © iAndrew 2016 - Forum software by © MyBB