Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to check if record was found
  • Hi guys,
    I'm trying to work with ORM, so I'm creating an ajax based application.
    I have a problem when I try to find a record in my table. This is how I retrive my record:
    $o_link = Model_Link::find($name);
    

    $name can be an empty string or a string value. Documentation explain that if record doesn't exist, $o_link will be null. But if I try to print it I receive this:
    Orm\Query Object
    (
        [model:protected] => Model_Link
        [connection:protected] => 
        [alias:protected] => t0
        [relations:protected] => Array
            (
            )
    
        [joins:protected] => Array
            (
            )
    
        [select:protected] => Array
            (
            )
    
        [limit:protected] => 
        [offset:protected] => 
        [where:protected] => Array
            (
            )
    
        [order_by:protected] => Array
            (
            )
    
        [group_by:protected] => Array
            (
            )
    
        [values:protected] => Array
            (
            )
    
    )
    

    Am I did it in a wrong way? Thank you!
  • An empty string is not a special parameter for find(). If $name = '', it will translate to SELECT * FROM link WHERE id = ''; Whether or not this returns null, depends on if this query will return records or not.
  • The problem is most likely when the input for find() is null, in that case it's just an alias for query() and expects you to go method chaining. The find() method has 3 uses: find by id (with just 1 input), find first/last/all using the 2nd param array input and first param null to continue with method chaining to build the query. In all these cases the 2nd param will also be used when given as an array of the first conditions set on the query.
  • It was my fault, I don't want to use "ID" because I have another primary key. I read quickly the guide, so I thought that it would search for the primary key, not only for ID.
    Thank you!
  • I'm sorry, I misspoke it is "find by pk" not just id, the rest of my explanation stands: the problem is that it got null as input.
  • Still find this odd. TS mentioned "$name can be an empty string or a string value". In case of an empty string, find() will return query()->get_one(). Which should either return a model object containing the first record found, or null if nothing was found. It should not return an empty query object. In case of null, it does indeed return a query object you can use to chain. Model::find(null) is the same as Model::find(). So many we have a misunderstanding to what the definition of "empty string" is? Because null is something completely different than an empty string...
  • You can check it, I can't on my phone (or I can but am too lazy). But he clearly gets a Query object returned which I think can only happen when you input null.
  • Interesting discussion, I'm going out tonight, when I'll find 5 minutes I'll try to answer myself to the question.
    Thank you!

Howdy, Stranger!

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

In this Discussion