Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Invalid param count using parameter in where
  • Hi,

    Just wanted to check whether it is possible and allowed to use parameters in the WHERE clause of a query. I was attempting the following (using ORM):

      $songs = Model_Song::query()
                ->where(\DB::expr('MATCH(artist, title) AGAINST(:query IN BOOLEAN MODE)'))
                ->bind(':query', Input::post('q'))
                ->get();

    but I am getting "Invalid param count for where condition." at PKGPATH/orm/classes/query.php @ line 481.

    I thought I would "solve" the problem by DB::quote()ing the input string and plugging it directly into the DB::expr, but that gives me the same error.

    [Edit] Found this topic - http://fuelphp.com/forums/discussion/11587 - I am using version 1.7.2.

    P.S. I put this under General because I think it is related to the DB classes and not specifically to the ORM. If not, feel free to move it.

  • Afaik where() expects 2 or 3 arguments, either "field" and "value", or "field", "operator" and "value". The error you get here is because only 1 argument is passed (hence the invalid param count).

    1.8/dev has more support for DB::expr() in where() clauses, but I'm not sure this scenario is supported, you'll have to test that.

    If you're using git, simply checkout 1.8/develop. if you're using composer update it with --prefer-source, and if you've used the zip, make a copy of your fuel installation folder, and swap fuel/packages/orm with the contents of https://github.com/fuel/orm/archive/1.8/develop.zip.
  • Just checked, 1.8/develop also requires at least 2 arguments.

    It's probably better to run a normal DB query (not sure it has this limitation), and use as_object() if you want ORM objects as a result.
  • Thanks for your quick reply Harro.

    I am a bit confused now, the topic I linked to (http://fuelphp.com/forums/discussion/11587) seems to imply that as of version 1.7.1, where supports passing a DB::expr() as the only argument. Is this not the same interface that calling query() on an ORM model uses?

    I will try with as_object when I get home.
  • The SQL builder seems to understand it from the looks of it, but the where() method bails out directly if only 1 argument is passed, so it doesn't even get to building the SQL.

    You could test by disabling the "Invalid param count for where condition." exception in the _where() method, and see what the result is.

    It could be a

            if ($condition instanceof \Fuel\Core\Database_Expression)

    needs to be added.
  • I pushed a fix for this (to 1.8/develop): https://github.com/fuel/orm/commit/de31432d652ca355d76ae42cda4a94ec6943cbf5

    But there are more issues: ORM does not support bind(), and ORM uses generated table aliases which you have to use to prefix "article" and "title", but you don't know what is generated.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion