提问人:IMANE LOUKILI 提问时间:12/18/2022 最后编辑:Lajos ArpadIMANE LOUKILI 更新时间:12/22/2022 访问量:915
为什么我会收到警告:尝试在 PHP 中读取 bool 上的属性“nomGroupe”?
Why do I receive Warning: Attempt to read property "nomGroupe" on bool in PHP?
问:
当我尝试显示客户端及其组的列表时,我的代码中有问题,我遇到以下错误警告:尝试在布尔上读取属性“nomGroupe”这是我的代码
观点:
<table class="table table-bordered dataTable" id="dataTable" width="100%" cellspacing="0" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
<thead>
<tr role="row">
<th class="sorting sorting_asc" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Name: activate to sort column descending" style="width: 73px;">ID</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Position: activate to sort column ascending" style="width: 95px;">Nom</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Position: activate to sort column ascending" style="width: 95px;">Prenom</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Age: activate to sort column ascending" style="width: 31px;">Email</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Age: activate to sort column ascending" style="width: 31px;">Adresse</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Office: activate to sort column ascending" style="width: 58px;">Telephone</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Start date: activate to sort column ascending" style="width: 71px;">Groupe</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Salary: activate to sort column ascending" style="width: 67px;">Crée par</th>
<th class="sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Age: activate to sort column ascending" style="width: 31px;">Action</th>
</tr>
</thead>
<tbody>
<?php if (isset($rows) && $rows) : ?>
<?php foreach ($rows as $row) : ?>
<tr>
<td> <?= $row->id_client ?></td>
<td> <?= $row->nom ?></td>
<td> <?= $row->prenom ?></td>
<td> <?= $row->email ?></td>
<td> <?= $row->adresse ?></td>
<td> <?= $row->telephone ?></td>
<td> <?= $row->nomGroupe ?></td>
<td> <?= $row->creePar ?></td>
<td>
<a href="<?= ROOT ?>/clients/update/<?= $row->id_groupe ?>">
<button class="btn-sm btn btn-info text-white"><i class="fa fa-edit"></i></button>
</a>
<a href="<?= ROOT ?>/clients/delete/<?= $row->id_client ?>">
<button class="btn-sm btn btn-danger"><i class="fa fa-trash-alt"></i></button>
</a>
</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="6">
<center>aucun client pour le moment</center>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
Controller 类:
<?php
class Clients extends Controller{
private $client ;
private $clients ;
function __construct()
{
$this->client = new Client();
//$this->clients= $this->getClients();
}
function index(){
if(!Auth::logged_in()){
$this->redirect("login");
}
$this->view("clients",['rows'=>$this->getClients()]);
}
public function getClients(){
//get list of clients from database
$this->clients= $this->client->findAll();
//for each clients we will find his group and the user that added thus client to the db
foreach($this->clients as $client){
$groupe_tmp = new Groupe();
$utilisateur_tmp = new Utilisateur();
$val=$client->id_groupe;
$groupe_tmp=$groupe_tmp->first('id_groupe',$val);
$val=$client->id_utilisateur;
$utilisateur_tmp=$utilisateur_tmp->first('id_utilisateur',$val);
$client->nomGroupe=$groupe_tmp->nomGroupe;
$client->creePar=$utilisateur_tmp->nom." ".$utilisateur_tmp->prenom;
}
return $this->clients;
}
}
?>
错误:
答:
0赞
Lajos Arpad
12/22/2022
#1
线
$groupe_tmp=$groupe_tmp->first('id_groupe',$val);
返回一个布尔值(大概是)而不是返回正确的记录,因此,当您的代码到达false
$client->nomGroupe=$groupe_tmp->nomGroupe;
你得到你描述的错误。您可以执行以下操作:
$client->nomGroupe = is_bool($groupe_tmp) ? $groupe_tmp->nomGroupe : '';
此外,值得研究一下,看看它是否正确,以及为什么它会返回布尔值。您可能还会遇到数据库问题。first
function
评论
$client->nomGroupe=$groupe_tmp->nomGroupe;
在 处重新赋值,结果可能为假值。$groupe_tmp
$groupe_tmp=$groupe_tmp->first('id_groupe',$val);