phproad / phpr-framework

[PHPR Framework] PHPRoad system files
http://phproad.com
MIT License
12 stars 2 forks source link

Db_ActiveRecord #10

Closed snetty closed 11 years ago

snetty commented 11 years ago

Haven't actually checked this, but earlier issue reminded me of a feature I really want.

Can you please add order, limits and possibly extra joins to relations, my Hosting module has to jump through some ridiculously expensive hoops to get the relations I need.

daftspunk commented 11 years ago

@snetty can you give me some code examples?

snetty commented 11 years ago

I was actually struggling with this in LS yesterday. If you dig around in their AR you'll find that they have an unimplemented 'finder_sql' option on a relation definition which allows you to just pass it the SQL to use to create the join. Problem with that is that it won't give you the ability to add models to the relation, it would be read only.

So in summary, think it would be better to allow calculated columns to return multiple rows. So for example, you could set up a calculated column on the Shop_Customer model that shows all the products that customer has bought (obviously via the Order and Order Item tables)

daftspunk commented 11 years ago

I'm almost certain this exists already. Has many relationships will show a string separated with commas. Is this what you mean?

Maybe submit a pull request so I can see what you mean. Seeing code helps.

snetty commented 11 years ago

would want to be able to access the model rather than just 1 value though, so something like this

$calculated_columns = array(
'purchased_products' => array(
'sql' =>'SOME SQL QUERY THAT RETURNS ALL THE PRODUCTS TAHT A CUSTOMER HAS BOUGHT', 
'type' => 'Shop_Product')
);

then usage

$purchased_products = $customer->purchased_products;
foreach($purchased_products as $purchased_product){
echo $purchased_product->price;
}
daftspunk commented 11 years ago

Oh I get you now. I will see if this is possible currently. I'm pretty sure you could do it using a custom column with an eval prefix function. Have you tried that?

snetty commented 11 years ago

/me stares blankly, erm yeah that'd work.. not embarrassed at all

daftspunk commented 11 years ago

All good then! :100: