Sqlbuilder + Codeigniter v.4.1.1

Asked

Viewed 22 times

0

I have a model called Usuariomodel that only works the find and findAll option, but in my test step a Where so the method does not connect at the base: below follows the function call and the result:

Model:

class UsuarioModel extends Model
{
    protected $table = 'usuarios';
    protected $primaryKey = 'id';
    protected $allowedFields = ['nome', 'login', 'senha', 'tipoUsuario'];
    
    protected $returnType     = 'array';
    protected $useSoftDeletes = true;
    
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';
    
        
    public function get($id = null)
    {
        if($id <> null)
        {
            return $this->find($id);                    
        }           
        $exibe = $this->find();
        var_dump($exibe);
        exit();     
        
    }   
    
    public function pesquisar($busca = null)
    {
        
        $exibe = $this->where('id', 1);         
        var_dump($exibe);
        exit();
    }       
}

Controller:

class Usuario extends Controller
{

    public function __construct()
    {
        $this->model = new UsuarioModel();
    }   
    
    public function index()
    {   
        $busca = $this->request->getVar('busca');       
        if(!empty($busca))
        {           
            $this->data =   [
                'table' => $this->model->pesquisar($busca)
            ];
        }
        else
        {
            $this->data = [
                'table' => $this->model->get()
            ];
        }

        
        echo view('template/header');
        echo view('usuario/index', $this->data);
        echo view('template/footer');
    }
}
  • you speak the method pesquisar?

  • That’s right. This is the result of the search method var_dump: https://uploaddeimagens.com.br/imagens/XypRnfs

  • And this is the result of the get: https://uploaddeimagens.com.br/imagens/Bd90u-4method

  • it connects yes at the base

  • So what can it be? It does not bring the result of Where. Actually in this test I put Where passing an id directly, but the method will do a like in the Name field. But none of the functions work, nor directly pass the controller.

  • You are not asking for the result to be executed and mounted because of this need to use find or findAll at the end

  • It worked out the way you said, I really appreciate your help.

  • @Lucas Rondon. I recommend the doc. I gave a quick read and found very good https://codeigniter.com/user_guide/models/model.html#finding-data

Show 3 more comments

1 answer

0


You don’t have to do what you want to do in the model can directly use the methods that the model has that is much more logical and easier for maintenance, although it lacked to call the method at the end of the builder within your custom method, example:

//dentro do método
public function pesquisar($id)
{
    return $this->where('id', $id)->first();
}
$blog = new Blog();
$result = $blog->pesquisar(1);

or

//fora do método
public function pesquisar($id)
{
    return $this->where('id', $id);
}
$blog = new Blog();
$result = $blog->pesquisar(1)->first();

the same thing is done like this:

$blog = new Blog();
$result = $blog->where('id', 2)->first();

if the search is done directly on your primary key, then:

$blog = new Blog();
$result = $blog->find(2);
  • Great, thank you so much for the tip, besides working out now I’ll be able to make a code cleaner, thank you very much.

  • @Marcosxavier did not understand the recommendation?

  • 1

    @novic thousand pardons. It was to comment on the question, I traveled. I will put in the right place.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.