How to display the results of my request on the cakephp 3 view

Hello I have a problHello I have a problem to display the results of my request in cakephp here is the request:em to display the results of my request in cakephp here is the request:

$connection = ConnectionManager::get(‘default’);
$marches = $connection->execute('SELECT COUNT(*) AS nombre,
num_contrat,
libelle_marche,
date_debut_marche,
date_fin_marche,
SUM(montant_cible),
SUM(montant_commande),
SUM(montant_receptionne)
FROM marches
GROUP BY libelle_marche
')->fetchAll(‘assoc’);

in my controller
public function planning()
{
$marches = $this->Marches->getMarche();
foreach($marches as $marche)
debug($marche->num_contrat);
die(‘hi’);
}

here is the message I have :
null

[ Notice (8)](javascript:void(0);): Trying to get property of non-object [ APP/Controller/MarchesController.php , line 39 ]

how to display the result in my view for example I would like to display the value of num_contrat, nombre …
thanks for your help

What’s line 39 of MarchesController.php? We have no idea from what you’ve shown here.

Ok line 39 represents : debug($marche->num_contrat.
to make it simple in my controller I have a function planning:
public function planning()
{
$connection = ConnectionManager::get(‘default’);
$marches = $connection->execute('SELECT COUNT(*) AS nombre,
num_contrat,
libelle_marche,
date_debut_marche,
date_fin_marche,
SUM(montant_cible),
SUM(montant_commande),
SUM(montant_receptionne)
FROM marches
GROUP BY libelle_marche
')->fetchAll(‘assoc’);
foreach($marches as $marche)
debug($marche);
die(‘hi’);
}

when I debug ($marche), I can see the results I want :
/src/Controller/MarchesController.php (line 39 )

[ ‘nombre’ => ‘10’,
‘num_contrat’ => ‘EC3BAC5010’,
‘libelle_marche’ => ‘AERIEN’,
‘date_debut_marche’ => ‘2015-02-02’,
‘date_fin_marche’ => ‘2020-01-31’,
‘SUM(montant_cible)’ => ‘14728500’,
‘SUM(montant_commande)’ => ‘13347673’,
‘SUM(montant_receptionne)’ => ‘12443733’ ]

but when I want to recover the value of ‘num_contrat’ => ‘ECBAC5010’ by debug($marche-> num_contrat); I have :
null

[ Notice (8)](javascript:void(0);): Trying to get property of non-object [ APP/Controller/MarchesController.php , line 39 ]

How to display the values ​​of my request in a view, thanks

The results show that you have an array of results, not an object. Just like the error message would indicate. As such, you’d need to reference the value you’re looking for as $marche['num_contrat'].

1 Like