I’m trying to convert my SQL result into a class using PDO::FETCH_CLASS
.
The code was working fine, until I encountered a query that generates 83
rows. The query returns two columns, one of type int
and one of type text
.
This is the error message:
Fatal error: Uncaught TypeError: AppModelsBibliaLectio::add():
Argument #1 ($bibliaTextus) must be of type AppModelsBibliaTextus,
array given, called in
/MYROUTE/src/App/Domain/Infrastructure/Mapper/Biblia/PdoBibliaMapper.php on line 60
and defined in /MYROUTE/src/App/Models/BibliaLectio.php:10
The code I’m using is as follows:
PdoBibliaMapper
//..
$bibliaLectio= new AppModelsBibliaLectio();
$stmt=$this->pdo->run($sql,$params);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'AppModelsBibliaTextus');
while ($row = $stmt->fetch())
{
$bibliaLectio->add($row); #Line 60
}
BibliaTextus
namespace AppModels;
class BibliaTextus
{
private int $v;
private string $t;
public function toHtml()
{
return sprintf('<span class="versiculo">%s</span> %s',$this->v,$this->t);
}
}
BibliaLectio
namespace AppModels;
class BibliaLectio
{
private array $list;
public function add(BibliaTextus $bibliaTextus) #Line 10
{
$this->list[]=$bibliaTextus;
}
public function toHtml()
{
$s='<h1>Texto Bíblico</h1><div class="textus-title"><p>';
foreach ($this->list as $t)
{
$s.=$t->toHtml();
}
$s.='</p></div>';
$s.='<p class="bibliaTextus"><a href="https://example.com/biblia" target="_blank" rel="nofollow">Some Text</p>';
return $s;
}
}
Why does the code fail with a result of 83 rows and with smaller results it works fine and how could I solve this problem?