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 - 10-01-2008

[eluser]m4rw3r[/eluser]
What version are you using?

And if it is the latest from SVN, please describe what you've done when join_related() does not work.

The habtm file needs to be changed to utilize a join, to be able to get data from the join table.

EDIT: I'll make the changes to a join soon in the habtm file.


IgnitedRecord 1.0 pre-release - El Forum - 10-01-2008

[eluser]MarcoITA[/eluser]
Yes, I was using the latest from svn. Just redone the test with current svn (160):

I was going from a given page (name='home') through join table (called 'blocks') to get the articles ('contents') of that page. But...

Dump of $this->page->find_by('nome','home')->related('blocks')->get()

Code:
Array (
    [0] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "1"
        [zona] => string(8) "SINISTRA"
        [posizione] => string(2) "10"
    )
    [1] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "2"
        [zona] => string(8) "SINISTRA"
        [posizione] => string(2) "20"
    )
    [2] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "3"
        [zona] => string(6) "DESTRA"
        [posizione] => string(2) "30"
    )
    [3] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "4"
        [zona] => string(6) "DESTRA"
        [posizione] => string(2) "40"
    )
    [4] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "5"
        [zona] => string(6) "DESTRA"
        [posizione] => string(2) "50"
    )
)

exactly the same as $this->page->find_by('nome','home')->related('blocks')->join_related('articles')->get():

Code:
Array (
    [0] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "1"
        [zona] => string(8) "SINISTRA"
        [posizione] => string(2) "10"
    )
    [1] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "2"
        [zona] => string(8) "SINISTRA"
        [posizione] => string(2) "20"
    )
    [2] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "3"
        [zona] => string(6) "DESTRA"
        [posizione] => string(2) "30"
    )
    [3] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "4"
        [zona] => string(6) "DESTRA"
        [posizione] => string(2) "40"
    )
    [4] => IR_record Object (
        [__table] => string(6) "blocks"
        [__id] => NULL
        [__no_escape_data] => Array (
        )
        [nome] => NULL
        [page_id] => string(1) "1"
        [content_id] => string(1) "5"
        [zona] => string(6) "DESTRA"
        [posizione] => string(2) "50"
    )
)

both are just the correct rows from the 'blocks' (associations) table. join_related('articles') was supposed to add fields from 'articles' (actual article: title, body, author, date, ecc.)...


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

[eluser]Heli[/eluser]
I again have a problem with the fact that the IR is not take the parameters from the model, and is trying to calculate them self. This time the problem with habtm.

Previous similar problem: http://ellislab.com/forums/viewreply/460165/
I have version from 154 revision.
here is my model:
Code:
class Lot_model extends IgnitedRecord {
        var $table = 'lots';
        var $belongs_to = array(
                                        array('table'=>'products'),
                                        array('table'=>'users')
                                    );
        
        var $habtm = array(
                                array('table'=>'products_param_val',
                                        'fk'=>'lot_id',
                                        'r_fk'=>'products_param_val_id',
                                        'join_table'=>'lot_to_product',
                                        'name'=>'ltp'),
                                array('table'=>'condition_param_val',
                                        'fk'=>'lot_id',
                                        'r_fk'=>'condition_param_val_id',
                                        'join_table'=>'lot_to_condition',
                                        'name'=>'ltc')
                                );
        
        
    }



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

[eluser]Heli[/eluser]
and one more:

Code:
$user_act = &$this->user_activity->select('session_id') // I'm need only session_id
->find_by(array('session_id','active'),array($session_id,1));

But IR make such request:
Code:
SELECT `session_id`, `users_activity`.*
FROM `users_activity`
WHERE `session_id` = 'e5f48589f52f7d6b051ab6135bf48b45' AND `active` = 1
LIMIT 1
IR select 'session_id' from users_activity and all from users_activity.


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

[eluser]m4rw3r[/eluser]
@heli
Defaults:
What default value is used instead of the specified one?

Selects:
The find_by() (and all other getters on IgnitedRecord) cannot determine if a select has been done, because join_related() adds selects too, and hence it is not possible to deduce if IR has already selected the data or not.

But I can add an optional parameter to get(), which can adjust what columns to retrieve.

btw, you can also use an associative array in find_by() now:
Code:
find_by(array('name' => 'foo'));

@MarcoITA:

The default name for Belongs To relationships is singular, so try with a singular relation name.
I've added an error message for relation names which weren't found.

And I've also added an experimental feature for the HABTM: relation attributes (your suggestion)

EDIT:
@heli:
Normally an ORM library would refuse to load nothing but the whole record (Ruby's DataMapper uses lazily loading of TEXT fields, though), because at least the ID is required.
So in your case above with the sessions, I would have used CI's AR or IgnitedQuery to fetch it.


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

[eluser]MarcoITA[/eluser]
Downloaded the update form svn, I see the $attr property and method in relproperty_habtm.php.

But I'm sure I'm doing something very wrong: looks like I'm not able to configure the relationship:

Model:
Code:
class Page extends IgnitedRecord {
      var $habtm = array(
            'name'          =>'contents',
            'attr'          => array('zona','ordine'),
            'join_table'    => 'blocks');
    }

controller:

Code:
$this->load->model('page');
        $contents = $this->page->find(1)->related('contents')->get();
        u_print_r(
            $contents
        );

result:

Quote:A Database Error Occurred

Error Number: 1146

Table 'lirzvirtual.portal_contents_pages' doesn't exist

SELECT portal_contents.* FROM (`portal_contents`) JOIN `portal_contents_pages` ON portal_contents_pages.content_id = portal_contents.id WHERE portal_contents_pages.page_id = '1'



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

[eluser]Heli[/eluser]
SQL issues such errors:
Code:
A Database Error Occurred
Error Number: 1146
Table 'trade.lots_products_param_val' doesn't exist
SELECT * FROM (`lots_products_param_val`) WHERE `lot_id` = 6 AND `product_param_val_id` = '1'
and after creating this table I got another SQL error:
Code:
A Database Error Occurred
Error Number: 1146
Table 'trade.condition_param_val_lots' doesn't exist
SELECT * FROM (`condition_param_val_lots`) WHERE `lot_id` = 7 AND `condition_param_val_id` = '1'
After creating these tables all begin work, but when I try to get related records again, I've got error:
Code:
A Database Error Occurred
Error Number: 1054
Unknown column 'products_param_val_id' in 'field list'
SELECT `products_param_val`.* FROM `products_param_val` WHERE `products_param_val`.`id` IN (SELECT `products_param_val_id` FROM `lots_products_param_val` WHERE `lot_id` = '5')
All fk and join_tables are set in model.


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

[eluser]m4rw3r[/eluser]
You two had the same error, and I should really blame myself for not spotting it (I have followed my conventions, so I didn't have that problem with HABTM relations).

The problem was that the class name of IR_RelProperty_habtm had earlier been IR_RelProp, and I did forget to rename the constructor (really stupid of me) :red: .

I hope I won't make such mistakes again (updated SVN with a fix).


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

[eluser]MarcoITA[/eluser]
Wow!!! It now works like a charm!

With this models:

Code:
class Page extends IgnitedRecord {

    var $habtm = array(
        'contents' => array(
            'name'          => 'contents',
            'join_table'    => 'blocks',
            'attr'          => array('zona','posizione'),
        )

    );
}

Code:
class Content extends IgnitedRecord {

    var $belongs_to = 'user';

    var $habtm = array(
        'pages' => array(
            'name'          => 'pages',
            'join_table'    => 'blocks',
            'attr'          => array('zona','posizione'),
        )

    );

}

Just one line of chained methods:

Code:
$this->data->pagina = $this->page->find_by('nome',$page)->related('contents')->order_by('zona, posizione')->join_related('user')->get()

Gives the result:

Code:
Array (
    [0] => IR_record Object (
        [__table] => string(8) "contents"
        [__id] => string(1) "3"
        [__no_escape_data] => Array (
        )
        [zona] => string(6) "DESTRA"
        [posizione] => string(2) "30"
        [user_id] => string(1) "1"
        [user_login] => string(5) "marco"
        [user_password] => string(32) "--not shown--"
        [user_nome] => string(13) "Marco Manieri"
        [user_email] => string(0) ""
        [id] => string(1) "3"
        [titolo] => string(5) "Staff"
        [corpo] => string(195) "--not shown--"
        [data] => string(10) "1222472943"
    )

Just a little additional suggestion:

as join_related adds the table prefix (user_ in the above dump), the ->related might add the join table name to the additional fields fetched from the join table. In the example above we might have: blocks_zona and blocks_posizione.

But.... I have another issue now... sorry for bothering you!

I'm trying to save fields from a form:

Code:
$rec=$this->content->find($this->input->post('id'));
$rec->load_postdata(array('id','titolo','corpo'));
if (!$rec->save()) {
    $this->show_error('Impossibile salvare i dati!');
};

I get this error:

A Database Error Occurred

Error Number: 1406

Data too long for column 'titolo' at row 1

Code:
UPDATE `portal_contents` SET `titolo` = 'Cos\'รจ LIRZ-Virtual.net?', `corpo` = '--cut--' WHERE `id` = '1'

Please note that 'corpo' post field came from a WYSIWYG js editor (FCKeditor) that generates an already "htmlentitied" string.

The only way to make it work is to add, after the load_postdata() method, the following line:

Code:
$rec->titolo = htmlentities($this->input->post('titolo'));

Looks like the load_postdata() or save() methods strangely hand the string from the input() library prior to build the SQL?!

Also note that if I copy the SQL from the error page and paste it into MySQL Query Browser.... it works!!!!


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

[eluser]m4rw3r[/eluser]
The database error you are experiencing is a MySQL character encoding error, not something which is wrong with the SQL or IgnitedRecord.
Most likely it is some kind of mismatch between the PHP defined charset and the MySQL one, and the "data too long" error is somehow triggered when MySQL cannot recognize the letter.

A workaround it to not store the accented characters in the DB, which, as you already stated, can be done with htmlentities().