Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
model ::find_by() method giving error
  • Whenever I use the find_by() method with the third parameter set to 'like', it gives me a
     Fuel\Core\PhpErrorException [ Warning ]:
    array_key_exists() expects parameter 2 to be array, integer given.

     This is the line in question:

    $result = Model_Tmuser::find_by('username', $search_target, 'like', 5, 10); 

    Note: it works perfect when omitting the third parameter.
  • HarroHarro
    Accepted Answer
    I don't know where you got the idea from that it would work like this.

    A find_by() always does a value lookup, you can not use any other operator. If you want that, code a normal find() with a where clause. And what are the 5 and 10 arguments? Not sure what you intend to do there.

    If you pass a third argument, it has to be the options array, like you pass as the second argument on a normal find.

    Model_Tmuser::find_by('username', $search_target);

    is an shortcut for

    Model_Tmuser::find('all', array('where' => array('username', '=', $search_target)));
  • I copied it from the examples at

    Namely, 
    // SELECT * FROM `users` WHERE `email` LIKE "%@example.com" LIMIT 5 OFFSET 10
    $users = Model_User::find_by('email', '%@example.com', 'like', 5, 10);

    I need to use LIKE in this search query... I'll try with the example in the last like you wrote, and use 'like' instead of '='

    Thanks again
  • I think you are mixing \Model\Crud and \Orm\Model which have different implementations of find_by(). The OP seems to be using a class deriving from \Orm\Model, however, the doc you linked mentions \Model\Crud which obviously are different classes.

    So check the implementation of the model in question and see what it ultimately extends - \Model\Crud or \Orm\Model - then you know what to look inside the docs for.
  • atabakatabak
    Accepted Answer
    $result = Model_User::query()
    ->select('field1', 'field2',....)
    ->where('
    email', 'LIKE', '%'.$search.'%')
    ->get();

Howdy, Stranger!

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

In this Discussion