Mahmoud.Eskandari
Mahmoud.Eskandari

Reputation: 1478

Best way to search in all columns in Phalcon ORM?

Always I use this following code in mysql:

    SELECT * FROM myobject 
WHERE MATCH(`name`, `foo`, `bar`) AGAINST ('$search') IN BOOLEAN MODE

In Phalcon ORM:

 myobject::find([
            "columns” => "id, name”,
            "conditions" => "name LIKE ?1 OR foo LIKE ?1 OR bar LIKE ?1 ",
            "bind"       => [1 =>$search]
            ]);

How to find through all columns?

Is this possible in Phalcon ORM in a quick way?

Upvotes: 2

Views: 981

Answers (1)

Timothy
Timothy

Reputation: 2003

As mentioned above by @niki_mihaylov, there is no ready solution available in Phalcon. You can however just grab all your model's columns and add a where clause for each column.

Depending on how many columns your table has and what kind of thing you are searching for, there might be a drop in performance.

[controller]

$result = myobject::searchColumns($search);

[model]

public static function searchColumns($search)
{
    $query = self::query();

    foreach (self::columnMap() as $column) {
        $query->orWhere($column . ' LIKE :search:');
    }

    $query->bind(['search' => '%' . $search . '%']);
    return $query->execute();
 }

The self::columnMap() refers to the columnMap defined in your model. A better solution might be to define a custom array with all the columns you would like to search through. Hereby limiting the amount of searchable columns and possibly an increase in performance.

Upvotes: 1

Related Questions