Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Leave it up to the programmer to select required fields in an fRecordSet::buildFromSQL() call

posted by vena 9 years ago

I get the reasoning behind asking that SQL statements passed to buildFromSQL look like a SELECT * FROM statement, but this is irritatingly limiting. Why not throw an exception after the statement is run if the required fields are not returned rather than enforcing a rigid SQL scheme?

For instance, what if I needed to use a control flow statement to decide between selecting a column or using a sub-query to get that value elsewhere if the column is null (a simple IF() in MySQL)? As far as I can see, that would mean I'd have to give up all the benefits of fRecordSet/fActiveRecord because it'd mean straying from SELECT * FROM (I tried it, Flourish throws an exception complaining about SELECT * FROM).

Once again, maybe I'm just not seeing the bigger picture :) Seems like a small increase in trust for a huge increase in potential, though.

Well, all of the fActiveRecord functionality is designed around the active record pattern, that is, an object that represents a row/record in a database table. In such situations there isn't a need for anything other than SELECT * FROM.

It sounds to me like you are using it very much like an active record, but you are pulling data from a different table. In your situation, I would probably recommend simply extending fRecordSet:

class CustomRecordSet extends fRecordSet
{
	static public function buildFromSQL($class, $sql, $non_limited_count_sql=NULL)
	{
		fActiveRecord::validateClass($class);
		fActiveRecord::forceConfigure($class);
		return new fRecordSet(
			$class,
			fORMDatabase::retrieve($class, 'read')->translatedQuery($sql),
			$non_limited_count_sql
		);
	}
}

Then you can call:

$rs = CustomRecordSet::buildFromSQL($sql);

I think leaving the check will help more people than remove the check will.

posted by wbond 9 years ago

Ah interesting, that should do the trick. Thanks once again!

posted by vena 9 years ago

yeah you know, this is just pure laziness on my part. this all came from my wanting to pass data to views with that convenient get/prepare/encode method prefix structure fActiveRecord provides. i really should get off my butt and just write a simple data container class that provides that, huh? save the overhead and the possibility someone else does something silly like manipulate the database from the view or something.

that way i can tune my SELECTs to my little heart's content. :)

posted by vena 9 years ago