Welcome Guest, Not a member yet? Register   Sign In
Entities error on soft delete
#1

Hi , sorry for english , i have a  db table with a foreign key (id_categorie)  this is the entities :


Code:
<?php
namespace App\Models\Entities;

use CodeIgniter\Entity;
use App\Models\CategorieModel;

class Esami extends Entity
{
    function categorie()
    {
      $model= new CategorieModel();
      return $model->find($this->id_categorie);
   
    }

}

If in table categorie the field deleted_at is set i have error in list in this row :


Code:
<?php foreach ($esami as $row): ?>

                    <td> <?=/*$row->id_categorie*/ $row->categorie()->nome?></td>

<?php endforeach; ?>


i want show both non deleted (to delete ) and delete (to reactivate) . How can i do this ?
Reply
#2

(12-25-2020, 02:45 AM)pippuccio76 Wrote: Hi , sorry for english , i have a  db table with a foreign key (id_categorie)  this is the entities :


Code:
<?php
namespace App\Models\Entities;

use CodeIgniter\Entity;
use App\Models\CategorieModel;

class Esami extends Entity
{
    function categorie()
    {
      $model= new CategorieModel();
      return $model->find($this->id_categorie);
   
    }

}

If in table categorie the field deleted_at is set i have error in list in this row :


Code:
<?php foreach ($esami as $row): ?>

                    <td> <?=/*$row->id_categorie*/ $row->categorie()->nome?></td>

<?php endforeach; ?>


i want show both non deleted (to delete ) and delete (to reactivate) . How can i do this ?

Can someone help me?
Reply
#3

1. Try calling withDeleted() method before calling find()
PHP Code:
return $model->withDeleted()->find($this->id_categorie); 

2. You need to rethink your approach to database queries. I mean queries in a loop.

I think it might be worth changing the query to get the Esami array and add JOIN to get the category.
For example:

PHP Code:
// Esami model class 

public function withCategorie()
{
    return $this->builder()
        ->select("{$this->table}.*, categorie.nome as categoriaNome"
        ->join('categorie''categorie.id = esami.id_categorie')
        ->get()
        ->getResult($this->tempReturnType);
}

$esami = (new EsamiModel())->withCategorie();


// in template 

<?php foreach ($esami as $row): ?>

    <td> <?=/*$row->id_categorie*/ $row->categoriaNome?></td>

<?php endforeach; ?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB