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

[eluser]cayasso[/eluser]
Thank you m4rw3r!!
So there isn't a method in IgnitedRecord that will return only the arrays of the query results(each row) like
Code:
get_data()
that returns an array of a single row from the query? or like
Code:
result_array()
for CI AR?

I will be waiting for the code you will post ;0

Thank you!

[eluser]Cannyp[/eluser]
Hi there,

I am having some difficulty saving back data to a joining table with habtm.

I have defined my schema and 2 classes as follows.

Code:
create table files (
   id integer,
   name varchar(20)
)

create table deliveries (
   id integer,
   dest varchar(20)
)

create table files (
   id integer,
   file_id integer,
   delivery_id integer
)

class File extends IgnitedRecord {
    var $table = "files";
    var $habtm = array(
        'table' => 'deliveries',
        'fk' => 'file_id',
        'name' => 'files',
        'join_table' => 'file_deliveries',
        'related_foreign_key' => 'delivery_id');
}

class Delivery extends IgnitedRecord {
    var $table = "deliveries";
    var $habtm = array(
        'table' => 'files',
        'fk' => 'delivery_id',
        'name' => 'deliveries',
        'join_table' => 'file_deliveries',
        'related_foreign_key' => 'file_id');
}


and in my controller, I loop over some data in $msg to save the data back to the database like so.

Code:
foreach($msg["files"] as $f)
{
   $fdata["name"] = $f["name"];
   $file_rec = $this->file->new_record($fdata);
   $file_rec->save();
}

foreach($msg["deliveries"] as $d)
{
   $ddata["dest"] = $d["dest"];
   $del_rec = $this->delivery->new_record($ddata);
   $del_rec->save();
}

EDIT: what I forgot to mention was, I have a second array in $d which relates to the file_deliveries. In this array, I have access to a reference to the file records already imported e.g.

Code:
foreach($msg["deliveries"] as $d)
{
   $ddata["dest"] = $d["dest"];
   $del_rec = $this->delivery->new_record($ddata);
   $del_rec->save();
   foreach ($d["filedeliveries"] as $fd)
   {
      print $fd["file_id"];
      // need to save this to the joining table with the delivery_id
   }
}

I can save the records back into the files and deliveries table fne but I cant work out how to insert the joining row without actually accessing the id property of the $del_rec and doing a new_record() inside the 2nd foreach loop - but I dont want to do this as it kind of defeats the purpose.

Can anyone shed any light on how to setup the habtm relationships properly? I have tried playing around with add_relationship() - which I have working in other parts of the code but I can't figure it out for habtm.

any pointers much appreciated.

thanks

[eluser]dxrsm[/eluser]
Hello,
I am testing IR on a predefined mysql schema. That means that I have to define non-default settings. I have users and userprofiles tables defined with innoDB and relations. There is a one-to-one relationship between them.
So I define these derived classes:

Code:
class User extends IgnitedRecord{
    var $table = "users";
    var $name = "user";
    var $id_col = "userID";
    var $has_one = array('table'=>'userprofiles', 'name'=>"userprofile");
}

class Userprofile extends IgnitedRecord{
    var $table = "userprofiles";
    var $name = "userprofile";
    var $id_col = "userProfileID";    
    var $belongs_to = array('table'=>'users', 'name'=>'user', 'fk'=>'userID');
}

In the controller I have a function defined as:

Code:
function index(){
    $this->load->library('ignitedrecord/ignitedrecord');
    $this->load->model('user');
    $this->load->model('userprofile');
        
    $rec =& $this->user->find(1);
      
    echo $rec->username;
        
    $rel =& $rec->related("userprofile");
        
    $r = $rel->get();

    echo $r->email;          
    }

I get the following error:

Quote:A Database Error Occurred

Error Number: 1054

Unknown column 'userprofile.user_id' in 'where clause'

SELECT `userprofile`.* FROM `userprofiles` AS `userprofile` WHERE `userprofile`.`user_id` = '1' LIMIT 1

I have to note that when i print_r the $rec object i get a *RECURSION*.
Also, when I call $rel = $rec->join_related("userprofile"), i get an error that:
Quote:IR_record: Method join_related() is not found.

I am using IgnitedRecord 1.0 pre_release (rev218).
I have included ignitedrecord and ignitedquery.php in libraries folder, and I'm running php5.
Is IgnitedRecord not suited for this kind of db setup?
I am taking a break... :down:
Any help much appreciated.
Thank you very much

[eluser]m4rw3r[/eluser]
@cayasso:

Here is a function for the job:
Code:
/**
* Converts an array of (record) objects or a record object into an array.
*
* @param $record The record or array of records to be converted
* @return array or scalar
*/
function records2array($record)
{
    if(is_object($record))
    {
        $skip = array('__table', '__instance', '__data', '__no_escape_data');
        $ret = array();
        
        foreach(array_diff(array_keys(get_object_vars($record)), $skip) as $key)
        {
            $ret[$key] = records2array($record->$key);
        }
        
        return $ret;
    }
    elseif(is_array($record))
    {
        $ret = array();
        
        foreach(array_keys($record) as $key)
        {
            $ret[$key] = records2array($record[$key]);
        }
        
        return $ret;
    }
    
    return $record;
}
Due to the PHP 4 compatibility, I cannot make a custom result set which has this (and some more neat functions).

@Cannyp:

Your problem is that you don't have record objects with the data.
I'm going to think about how to nicely add a function which will make it possible to establish relations with a list of ids (or a single id).

@dxrsm:

You have to set the fk on both ends of the relation, IR does not share this information, currently.
The join_related() method is on the model, it joins the related objects during the query, thus saving a lot of extra queries.
And replace print_r() with idump() (or $record->dump()), it skips the models.

[eluser]Cannyp[/eluser]
I have gotten around it for now by using the $rec->__id property.

Is this the accepted way to access the id of the newly created object/row? I'm just a bit wary of using this and then your interface changes.

Code:
$rec = $this->ci->file->new_record($f);
$rec->save();
$id = $rec->__id;


Or am I missing something?

[eluser]m4rw3r[/eluser]
You should also be able to use $rec->id directly (because save() assigns all pks in the objet too, at least in rev 233 and up).

[eluser]Cannyp[/eluser]
Thats perfect. I was using an older rev - 218. Got the latest from svn and all is well.

Thanks for a great library.

[eluser]dxrsm[/eluser]
Thank you. Now it works.

[eluser]cayasso[/eluser]
Thank you m4rw3r it works perfectly!!!! great!!!

I have another question, how do I use CI AR instead of IQ so for example this code bellow:

Is this correct??? I tried and gets error saying that db is not recognized. Do I need to instantiate the CI db first in the Class?

Code:
class Content extends IgnitedRecord {
    
    public $belongs_to = 'page';
    
    public $has_many = 'comments';
    
    // --------------------------------------------------------------------
        
    /**
     * Constructor
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        parent::__construct();        
        $this->user_lang = get_language();
    }
        
        // --------------------------------------------------------------------
        
    /**
     * Get latest content
     *
     * @access    public    
     * @return    array
     */
    public function latest()
    {
        
        $this->db->where('isact', true);
        $this->db->where('language', $this->user_lang);

        
        $query = $this->db->get('contents');
        return ( $query->num_rows() > 0 ) ? $query->result_array() : FALSE;
    }
}

Thank you!!!!

[eluser]m4rw3r[/eluser]
The thing is that $this->db is not assigned at all, no libraries are assigned actually (neater print_r, less dependency on other libs, cleaner code).

So to get CI's AR, you have to use get_instance(); as you would have done in any other lib (although you can assign the db property yourself in the constructor).

But the difference between CI's AR and IgnitedQuery is mostly negligible, so you can also use IQ if you want:
Code:
$q = new IgnitedQuery();

$q->where('isact', true);
$q->where('language', $this->user_lang);

$query = $q->get('contents');
return ( $query->num_rows() > 0 ) ? $query->result_array() : FALSE;




Theme © iAndrew 2016 - Forum software by © MyBB