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

Deleting many records

posted by ralph 8 years ago

Hi there I have been working with flourish 5 days ago. I'm creating a User Module for the Admin where I have one table with same number of users and checkboxes. My goal its to create a bulk action in order to delete several number of users. All works fine but I want to increase the performance of my code.

foreach( $checkboxes as $id ) {
    $db->execute("Delete from users WHERE user_id = %i", $id);
}

Theer is a query generator in flourish?

If you want to delete a bunch of records, I would recommend using one of the following:

// fDatabase placeholders can handle arrays of value when not using prepared statements
$db->execute("DELETE FROM users WHERE user_id IN (%i)", $checkboxes);

or

// Here we use a prepared statement to reduce the amount of work done for each call
$statement = $db->prepare("DELETE FROM users WHERE user_id = %i");
foreach ($checkboxes as $id) {
    $db->execute($statement, $id);
}

I'm not sure which would be faster, but using $db->enableDebugging(TRUE); it would be pretty easy to tell.


There is no query generator for Flourish. If you use the ORM there is fRecordSet, which provides some simplified querying of rows, but it relies on you using the ORM, which provides knowledge of the structure of your database.

Many of the cross-database libraries implement a query builder since different databases use slightly different constructs. Flourish instead provides the ability to run homogenized SQL that is then translated to the SQL dialect of the database you have connected to. You can see FlourishSql for the features that are supported.

Here's an example from Yii:

$user = Yii::app()->db->createCommand()
    ->select('id, username, profile')
    ->from('tbl_user u')
    ->join('tbl_profile p', 'u.id=p.user_id')
    ->where('id=:id', array(':id'=>$id))
    ->queryRow();

As compared to Flourish:

$user = $db->translatedQuery(
    "SELECT
        id, username, profile
    FROM
        tbl_user u INNER JOIN
        tbl_profile p ON u.id = p.user_id
    WHERE
        id = %i",
    $id
)->fetchRow();

As you can see, they are pretty similar. I think the real maintainability wins come from then you can automatically infer the FROM clause based on the database structure. - this is what fRecordSet::build() does. You can reference columns in related tables without having to do any JOINs, which is especially useful when you WHERE conditions are built via if/else statements.

posted by wbond 8 years ago

This is maybe one of the best things about Flourish you always respond our questions and you explain it very well.

Yes I think this feature its like kill bugs with a cannon.

posted by anonymous 8 years ago