Troubleshooting ORM returning null

Sometimes you may get no data back from an ORM query. You'll see an error like this one below stating that there was no data returned.


/vendor/twig/twig/src/Extension/DebugExtension.php:59:
array (size=0)
  empty

So how do we resolve this?

Step 1

Check the terminal for errors. You should see the query as in the output below.


src/templates/import/matchimport.twig
[Thu Mar 25 16:25:19 2021] INFO:Adding DBA
[Thu Mar 25 16:25:19 2021] DEBUG:SQL:
select  *
from    match t

Step 2

If your ORM query returns no data try running the query using a database client like DB-Beaver or PhpMyAdmin.

“select * from match t”

Step 3

Retrace your steps.

To get data from an ORM object you need the following files:

Define an ORM Class (Model)


// ./src/orm/Match.php
<?php
class Match extends \Tina4\ORM
{
    public $tableName="match";
    public $primaryKey="match_key"; //set for primary key
}

a Template file (View)


<!-- ./src/templates/match.twig -->
  {% set match = Tina4.call("MatchHelper","getMatch",[request.id]) %}
    {{ dump(match) }}

Controller


<?php

class LegacyHelper extends \Tina4\Data 
{
  public function getMatch($id) //no data yet
    {
        $match=(new OldMatch())->select("*",1,0)
                               ->where("match_key = {$id}")
                               ->asArray();                        
    return $match;
    }
}

Route (optional) a Route is preferable but Tina4 will read your template file by name in the absence of a route matching your url query.

http://localhost:8383/match.php should reach your template file if it's located in the templates directory (not a subdirectory of it).

Step 4

Consider reserved words. In the case above “match” is a reseved word in MySQL

Avoid Reserved words. If you inherit a database with tables that have reserved words for names you should change the names of those tables.

As a short term workaround

You can use tildes around the table name using the fetch method:


$match=$this->DBA->fetch("select * from `match`")
                         ->asArray();

Powered by ComboStrap