CodeIgniter Forums
IgnitedRecord 1.0 pre-release - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: IgnitedRecord 1.0 pre-release (/showthread.php?tid=7996)



IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]Andre83[/eluser]
When I try to establish a new relationship between two models I get this error:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: IgnitedRecord_record::$instance
Filename: models/ignitedrecord.php
Line Number: 1180

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ignitedrecord.php
Line Number: 1180

This what I've done:
Code:
$this->load->model("post");
$this->load->model("tag");
        
$rec = $this->post->new_record();
$rec->title = 'A new post';
$rec->slug    = 'a-new-post';
$rec->save();
        
$tag = $this->tag->new_record();
$tag->name = 'Blah';
$tag->save();
          
$post = $this->post->find(2);
$tag = $this->tag->find_by('name', 'Blah');
        
$post->add_relationship($tag);

What am I doing wrong?


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]m4rw3r[/eluser]
Whoops, I forgot the double underscore before the instance property (had started to move some methods from MPTtree ORM to IgnitedRecord, so I guess I forgot them (MPTtree uses only instance, without underscores)).

Updated the errata.

Thanks!


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]Andre83[/eluser]
Thanks for the fix, m4rw3r; for some reason the following is not working though.

Code:
$post->add_relationship($tag);

I would expect it to create a new record in the posts_tags table, but it doesn't.


Edit: sorry, my fault, all's working wonderfully now Big Grin


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]m4rw3r[/eluser]
Have you defined a has and belongs to many relationship between the two models (it is needed at least for the object where you are calling add_relationship())?
Because it is working fine for me...

Code:
// here is the relationship needed, because you establish a relation from a child record of this class
class post extends IgnitedRecord{
    var $__has_and_belongs_to_many = 'tags';
    function post(){
        parent::IgnitedRecord();
    }
}
// not needed here, but if you want to call $tag_rec->add_relationship($post_rec); you have to define the relation in here
class tag extends IgnitedRecord{
    var $__has_and_belongs_to_many = 'posts';
    function tag(){
        parent::IgnitedRecord();
    }
}
Or have you forgotten the double underscores prefixing the properties?

EDIT: Didn't see that you cleared that up


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]Andre83[/eluser]
Okay I've got another problem for you to solve Tongue

I'm trying to delete a record from the database, and see if also the corresponding relationships are deleted too, so I write this:

Code:
$post = $this->post->find(2);
$post->delete();

I get this error:

Message: Invalid argument supplied for foreach()
Filename: models/ignitedrecord.php
Line Number: 933


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]m4rw3r[/eluser]
Could you post your derived class?
Because everything works fine for me (the delete() method loops all relations and deletes them, so it is something with the relation properties).


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]Andre83[/eluser]
Code:
class Post extends IgnitedRecord {
    
    var $__has_and_belongs_to_many = 'tags';

    function Post()
    {

        parent::IgnitedRecord();

    }

}

Nothing more than this...


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]m4rw3r[/eluser]
Ok, this problem arises when a record which hasn't loaded any relations is being deleted.
The __relation_properties in the IgnitedRecord_record class must be set to array() in the declaration.

Updating the errata.

Anyway, please give me more feedback (not only error reports, but error reports are fine I guess (then I have something to do, and I know that I've not wasted my time Tongue))!


IgnitedRecord 1.0 pre-release - El Forum - 05-02-2008

[eluser]Chris Newton[/eluser]
Just FYI, I've had to move on from attempting to use this at the moment, so I won't be posting any additional comments at least for the near future. It shows promise, so I'll probably take a look again in a few weeks. Thanks for the efforts thusfar.


IgnitedRecord 1.0 pre-release - El Forum - 05-03-2008

[eluser]m4rw3r[/eluser]
IgnitedRecord have now been updated to 0.1.0 RC 4

I have now made some changes on how the more "advanced" relations (not using all defaults) are defined:
previous:
Code:
var $has_one = array('tablename' => array('name' => 'relationname', 'col' => 'foreign_key'),
                     'tablename2' => array('name' => 'rel2')
                    );
the new way:
Code:
var $has_one = array(array('table' => 'tablename', 'name' => 'relationname', 'col' => 'foreign_key'),
                     array('table' => 'tablename', 'name' => 'another_to_same')
                    );
Personally I think the new version is more descriptive, and it adds a new feature: Now you can relate to the same table with different relationnames using the same relation type.
Example:
Code:
class thread extends IgnitedRecord{
    var $has_many = array(array('table' => 'posts', 'col' => 'thread_id'),
                          array('table' => 'posts', 'col' => 'thread_id_comment', 'name' => 'comments')
                         );
}
In the model above, we have both comments and posts stored in the same table. If a post is only a comment, it has only the thread_id_comment set, if it is a post it has thread_id set, and if it is both it has both set.

Some bugs have been corrected and loading of relations now logs a warning if no relation with that name was found.
I've also added calls to show_error() in the __call() methods if no method is found.

I'm currently playing around with JOINs and aliases, and if somebody wants to help I'd be grateful. It seems like IgnitedRecord would loose a lot of flexibility when fetching objects if I implement the JOINs like my current experimentations suggests, so I will probably make join variants of most of the existing methods for fetching data.
If anyone have a suggestion on how to implement JOINs, please share.

Thanks for your feedback!