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

How do you fetch specific records by column value?

posted by hcker2000 7 years ago

I am trying to figure out how to do the following query. I figured I would just make a new Article object and then do some thing like $article->find(array('article_type_id' => 3));

select * from articles where article_type_id = 3

Hi hcker2000 you should take a look at the fActiveRecord and fRecordSet documentation http://flourishlib.com/docs/fActiveRecord#CreatingandLoadingRecords

The simplest case of fetching a record by its primary key is very easy

$article = new Article(1);

.. but I guess you already figured that out.

In your example you want to find all articles by type id. You can do that like this:

$articles = fRecordSet::build('Article', array('article_type_id=' => 3));   // note the equal sign in the key

foreach ($articles as $article) {
    // read article

$articles is a fRecordSet object. If you want a regular array you can call the getRecords() method or getRecord(0) to get any specific record.

posted by mblarsen 7 years ago

but if i only want single value?.. do i need to do looping??? i had used getRecord()method but...

posted by isn 7 years ago

Hi isn

In general you have to do looping yes. But if you create a unique index on a column you can fetch using the constructor function.

Say for User you have both user_id and email which are both unique. The normal way to fetch a single unique record. Just like in the

$user = new User(1);

So if you've created a unique index on email you can do like this:

$user = new User(array('email' => $email));
posted by mblarsen 7 years ago

but i think its not a good idea making unique to every field, i mean cant we just get a single data like

select address from db where id=5; (ie only address if we can get its id)


posted by isn 7 years ago

If you just want a specific column of a specific record, you should use fDatabase:

$db  = new fDatabase('mysql', 'my_database', 'username', 'password');
$address = $db->query("SELECT address FROM db WHERE id = 5")->fetchScalar(); 
posted by wbond 7 years ago