Welcome Guest, Not a member yet? Register   Sign In
Jamie Rumbelow's Flare O/R Mapper for CI - anybody playing around with it?
#1

[eluser]peterphp[/eluser]
Hi,

I spent quite a while now playing around with Jamie Rumbelow's Flare O/R Mapper for CI (yesterday's version which is still labeled as version 0.1.0, but it now comes with some helpful code comments).

After managing to get the example to work I started exploring into the more advanced features.

Here are my results:

1) I managed to overload $primary_key with the column name of my table which is different to the default 'id'.

I did it this way in my controller:

Code:
Customer::$primary_key = 'cid';

with

table name: customers
controller: customers
model: customer_model

Can anybody maybe give me a hint if this is the proper way to do the overloading?

$primary_key is declared 'static' in the Flare lib:

Code:
/**
* The name of the primary key. Defaults to
* ID but can be overloaded if needs be.
*
* @var string
*/
static $primary_key = 'id';

I am asking because I am not very 'fluent' in OO.


2) After that success I expected it to be easy to also change the table that is queried as the doc comments state:

Code:
/**
* The table name, automatically generated
* based on the class name (pluralised). Overload
* if you have funky table names.
*
* @var string
*/
static $table;

Unfortunately

Code:
Customer::$table = 'my_table_name';

did not work, there must be more to this which I could not figure out.


3) After being able to successfully retrieve all properties (columns) with

Code:
$this->data['post'] = Customer::find($id);

I was pretty sure that

Code:
$this->data['post'] = Customer::find($id, array('select' => 'login_name'));

would only return the 'login_name' property/column, but this didn't work either.


Overall I am very impressed what is possible with Flare, I just need to get these things sorted out to start really working with it.

So, maybe there are a few more advanced users here who can give me a little bit of help, that would be really nice and highly appreciated.
#2

[eluser]peterphp[/eluser]
Hi,

I just wanted to update my post with my last findings which are are workaround for my point 2) and a solution for 3).

Solution for 3):

Code:
$options = array(
             'select'     => 'column3, column5, column7'
             'from'       => 'myTableName',
             'conditions' => array('column3' => 'mySearchTerm'),
             );

$this->data['post'] = Customer::find('all', $options);

// alternative short form
$this->data['post'] = Customer::all($options);

with:
model: customer_model (that's what "Customer::" refers to).

"conditions" within the options-array creates a where statement in the resulting sql.


Workaround for 2):

Well, I now just use the "from"-option above.


Hope that's helpful for someone.

Again, IMHO Jamie delivered a *brilliant* piece of code with Flare, congratulations to that!
#3

[eluser]sophistry[/eluser]
i hadn't noticed that bit of code until now. thanks for the link.

two things i did notice about the code:

pluralize and singularize are not optimized - in fact, it's the opposite! the code could be easily refigured to test for the most common matches first and then return. instead, it searches the uncommon words first and only at the very last foreach loop does it get to the basic english pluralization of "add an s or es".

also, using CI's last_row() function is a very bad idea (this bug has been confirmed, yet not fixed in CI since April 15, 2009): http://codeigniter.com/bug_tracker/bug/7389/

otherwise, this looks good for PHP5 users!

thanks.
#4

[eluser]Jamie Rumbelow[/eluser]
I was wondering when a Flare topic would pop up here Smile The first thing I'd like to say is that Flare is in extremely early beginnings. It will be a fully featured, working, documented ORM with some really sexy stuff going on like relationships, single table inheritance and other goodies. It's not quite at that level of maturity yet, as it was mostly written during one night when my insomnia was especially bad, so I'm expecting to find bugs popping up all over the place.

Having said that, it's really helpful having people reporting bugs to me, so please keep going - I really appreciate it and it's really helpful for the system's development.

[quote author="peterphp" date="1261061152"]
Solution for 3):

Code:
$options = array(
             'select'     => 'column3, column5, column7'
             'from'       => 'myTableName',
             'conditions' => array('column3' => 'mySearchTerm'),
             );

$this->data['post'] = Customer::find('all', $options);

// alternative short form
$this->data['post'] = Customer::all($options);

with:
model: customer_model (that's what "Customer::" refers to).

"conditions" within the options-array creates a where statement in the resulting sql.
[/quote]

I think there's a bug - I'll investigate with the select issue you were having and see what I can find.

Quote:Workaround for 2):

Well, I now just use the "from"-option above.

There's an inherent issue in PHP and static stuff that means overloading static properties doesn't work correctly. It's pissing annoying. I'll have to go to the drawing board and figure out what the best solution to this problem is, until then, just use the "from" options parameter.

Quote:Again, IMHO Jamie delivered a *brilliant* piece of code with Flare, congratulations to that!

Thanks very much! Smile

[quote author="sophistry" date="1261081758"]i hadn't noticed that bit of code until now. thanks for the link.

two things i did notice about the code:

pluralize and singularize are not optimized - in fact, it's the opposite! the code could be easily refigured to test for the most common matches first and then return. instead, it searches the uncommon words first and only at the very last foreach loop does it get to the basic english pluralization of "add an s or es".

also, using CI's last_row() function is a very bad idea (this bug has been confirmed, yet not fixed in CI since April 15, 2009): http://codeigniter.com/bug_tracker/bug/7389/

otherwise, this looks good for PHP5 users!

thanks.[/quote]

Thanks for the comments.

As far as the inflectors go, they work. And I'm happy with that Smile if you're running a huge web app where every millisecond counts (despite the fact you shouldn't be using CI's ActiveRecord, let alone an ORM) then have a go at rewriting them. Until then I'm not going to worry about it because it does it's job perfectly well.

I'm sure EllisLab will fix last_row(), and then it won't be a problem, will it? Smile The only other way to do this would be to return a result array then return the last element of that array, and that's just back to square one.

Cheers.

Jamie
#5

[eluser]sophistry[/eluser]
[quote author="Jamie Rumbelow" date="1261093475"]I'm sure EllisLab will fix last_row()[/quote]

i am glad that you are confident; i am not. bugs like this hang around for ages. seeing your wicked code in play and your obvious talents, i am surprised that you don't just switch the code and avoid the last_row() bug altogether. yes, it's an easy fix...

WRT inflectors... it's just a pet peeve of mine and so easily optimized by reordering the arrays and the foreach loops that it would be a shame to leave this the way it is. it's kind of like that annoying rattle in your car... it doesn't really affect the performance, but it sure is nice when it's gone!

anyway, your contributions are wonderful... don't let me get you down with my picayune essaying.

cheers.
#6

[eluser]peterphp[/eluser]
Hey Jamie,

first things first: Happy New Year :-)

I am now looking into the 'save' function of Flare and can't really get it to work. Here is what I am doing:

Code:
$this->load->library('flare');
$this->load->model('c_gmail_model');  // name of db table is 'c_gmails'

$gmail = new C_gmail();

$gmail->id  = '[email protected]';
$gmail->pwd = 'mysecretpwd';

$gmail->save();

This does not work at all.

I can improve it by adding these two lines to the Flare save() function:

Code:
get_instance()->load->database();
self::$db =& get_instance()->db;

With these two lines the inserting (of new ids) into db works, but in case the id already exists the system does NOT automatically switch over to 'update' as it should.

Maybe I am missing out just a simple step in my code, but I looked through all the Flare code and could not find any function that I might have to call before calling the save() function.

Thanks in advance for any hint.
#7

[eluser]Jamie Rumbelow[/eluser]
Hey Peter,

Thanks for contacting me.

The first problem you mentioned is fixed in the latest update I pushed to the repository. The second I'm not so sure about - I think it's because 'id' is expected to be your auto incremented primary key. If you want to change it to something else (again, remember to update to the latest version), create a method in your model called "primary_key" that returns the name of the column. You'll need that column to exist for it to work.

If it still doesn't work, make the code available online and I'll take a look.

Cheers,

Jamie
#8

[eluser]peterphp[/eluser]
Hi Jamie,

thanks for the quick reply, I will have a look into the newest code then.

Regarding the second problem: I found out that I need a slightly different behavior from what Flare offers so I created my own 'my_save' function.

Please let me know what you think about it so that I can modify it if necessary. I'm still fairly new to OO...

Cheers again,

Peter




Theme © iAndrew 2016 - Forum software by © MyBB