CI 를 오랫만에 다시 접하고 CI4 를 composer 를 이용하여 설치하였습니다.
단순 텍스트를 찍는 컨트롤러는 정상 동작하고 있습니다.
DB 관련 설정을 하고 모델을 만들어서 실행을 시켰는데
Call to undefined method CodeIgniter\Database\MySQLi\Connection::select() 에러를 만나고 어찌할 바를 모르겠습니다.
에러를 내는 라인의 코드는 모델 쪽의 $this->db->select('ct_id'); 입니다.
DB 연결이 제대로 되지 않아 생긴 문제라고 생각하는데, 확인을 어떻게 해야 하는 지 예제는 있는 지
제가 무지한 탓에 해결을 못하고 있네요.
죄송하지만 답변을 부탁드립니다.
.env 에 DB 관련 설정은 다음과 같습니다. ( 로컬에서 RDS 는 workbench 로 정상접속 가능합니다. )
---------------------------------- .env ----------------------------------------
database.default.hostname = RDS 엔드포인트
database.default.database = 디비명
database.default.username = 계정
database.default.password = 비번
database.default.DBDriver = MySQLi
database.default.DBPrefix =
database.default.port = 3306
----------------------------------------------------------------------------------
컨트롤러는 다음과 같습니다.
---------------------------------- CartRyu Controller ----------------------------------------
class CartRyu extends BaseController {
public function initController(
RequestInterface $request,
ResponseInterface $response,
LoggerInterface $logger
) {
parent::initController($request, $response, $logger);
}
public function getData() {
// 모델 로드
$cartRyuModel = new CartRyu_model();
$cData = $cartRyuModel->getCartRyu();
echo json_encode( $cData );
}
}
-----------------------------------------------------------------------------------------
모델은 다음과 같습니다.
---------------------------------- CartRyu Controller ----------------------------------------
class CartRyu_model extends Model
{
protected $table = 'cart_ryu'; // 테이블 명
public function __construct()
{
parent::__construct();
$db = \Config\Database::connect('default');
}
public function getCartRyu() {
$this->db->select('ct_id');
$this->db->where('ct_id <', 1000);
$this->db->order_by('ct_id desc');
$qry = $this->db->get($this->table);
$result = $qry->result_array();
return $result;
}
}
------------------------------------------------------------------------------------------
개인적으로 헷깔려서 첨언 합니다.
public function getCartRyu() {
$qry = $this->db->table($this->table)
->select('ct_id')
->where('ct_id <', 1000)
->orderBy('ct_id', 'desc')
->get();
$result = $qry->getResultArray();
return $result;
}
2번은 빌더 방식
public function getCartRyu() {
$db = db_connect();
// OR
$db = db_connect('default');
$builder = $db->table($this->table);
$builder->select('ct_id');
$builder->where('ct_id <', 1000);
$builder->orderBy('ct_id', 'desc');
$result = $builder->get()->getResultArray();
return $result;
}
$this->db 하실때는 한번에 체인닝 해서 써주셔야 합니다.
나눠서 조건문을 넣어야 한다면 빌더로 받아와서 조건을 거는 방식을 추천드립니다.