Welcome Guest, Not a member yet? Register   Sign In
$builder->getCompiledSelect not work
#1

(This post was last modified: 06-17-2022, 05:10 AM by MikiStoni.)

Hi Devs

I used Composer to update my codeigniter to 4.21. Now I get an error message when calling getCompiledSelect. Is there another bug?

Greetings, Mike

Error Message

CodeIgniter\Exceptions\ModelException
You cannot use `getCompiledSelect()` in `App\Models\PreferencesFirmModel`. This is a method of the `Query Builder` class.

Controller

Code:
    $builder = new PreferencesFirmModel();
    $builder->select('id');
    $builder->where('team', '0' . $show_team);
    $builder->where('active', '1');
    $builder->where('deleted_at', NULL);
    $sql = $builder->getCompiledSelect(false);
    $builder->query($sql);

Model

Code:
<?php namespace App\Models;

use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;

class PreferencesFirmModel extends Model {
  protected $table      = 'data_company';
  protected $primaryKey = 'id';

  protected $returnType = 'array';
  protected $useSoftDeletes = true;

  // this happens first, model removes all other fields from input data
  protected $allowedFields = [
    'active', ..........
  ];

  protected $useTimestamps      = true;
  protected $createdField      = 'created_at';
  protected $updatedField      = 'updated_at';
  protected $deletedField      = 'deleted_at';

  protected $validationRules    = [];
  protected $validationMessages = [];
  protected $skipValidation    = false;

  // we need different rules for registration, account update, etc
  protected $dynamicRules = [
    'newFirma' => [
      'firma'                => 'required|min_length[3]|max_length[100]',
.........
    ],
    'updateFirm' => [
      'firma'                => 'required|min_length[3]|max_length[100]',
..........
    ],
    'updateSort_ci' => [
      'sortable_ci'          => 'required',
    ],
    'updateSort_ii' => [
      'sortable_ii'          => 'required',
    ],
    'delSort' => [
      'id'                    => 'required',
    ]
  ];


    //--------------------------------------------------------------------
    /**
    * Changes validation rules dynamically
    */
  public function setValidationRules($rules) {
    $this->validationRules = $this->dynamicRules[$rules];
  }


}
Reply
#2

This and similar methods are not allowed to be called through the model.
No need to use the model in this way.
Reply
#3

(06-17-2022, 05:09 AM)MikiStoni Wrote: Hi Devs

I used Composer to update my codeigniter to 4.21. Now I get an error message when calling getCompiledSelect. Is there another bug?

Greetings, Mike

Error Message

CodeIgniter\Exceptions\ModelException
You cannot use `getCompiledSelect()` in `App\Models\PreferencesFirmModel`. This is a method of the `Query Builder` class.

Controller

Code:
    $builder = new PreferencesFirmModel();
    $builder->select('id');
    $builder->where('team', '0' . $show_team);
    $builder->where('active', '1');
    $builder->where('deleted_at', NULL);
    $sql = $builder->getCompiledSelect(false);
    $builder->query($sql);

Model

Code:
<?php namespace App\Models;

use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;

class PreferencesFirmModel extends Model {
  protected $table      = 'data_company';
  protected $primaryKey = 'id';

  protected $returnType = 'array';
  protected $useSoftDeletes = true;

  // this happens first, model removes all other fields from input data
  protected $allowedFields = [
    'active', ..........
  ];

  protected $useTimestamps      = true;
  protected $createdField      = 'created_at';
  protected $updatedField      = 'updated_at';
  protected $deletedField      = 'deleted_at';

  protected $validationRules    = [];
  protected $validationMessages = [];
  protected $skipValidation    = false;

  // we need different rules for registration, account update, etc
  protected $dynamicRules = [
    'newFirma' => [
      'firma'                => 'required|min_length[3]|max_length[100]',
.........
    ],
    'updateFirm' => [
      'firma'                => 'required|min_length[3]|max_length[100]',
..........
    ],
    'updateSort_ci' => [
      'sortable_ci'          => 'required',
    ],
    'updateSort_ii' => [
      'sortable_ii'          => 'required',
    ],
    'delSort' => [
      'id'                    => 'required',
    ]
  ];


    //--------------------------------------------------------------------
    /**
    * Changes validation rules dynamically
    */
  public function setValidationRules($rules) {
    $this->validationRules = $this->dynamicRules[$rules];
  }


}
try this:

$PreferencesFirmBuilder = new PreferencesFirmModel(); 
$sql =$PreferencesFirmBuilder->select('id')->where('team', '0' . $show_team)->where('active', '1')->where('deleted_at', NULL)->builder->getCompiledSelect(false);
Reply
#4

Wow thank you very much. Long time passed. I looked at the problem again. Was exactly the right hint. Thanks a lot cerverg!!!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB