avenirer / CodeIgniter-MY_Model

Base model (MY_Model) for the Codeigniter framework.
324 stars 203 forks source link

How where add in join table #290

Closed Hothi-Jimit closed 5 years ago

Hothi-Jimit commented 5 years ago

Hello,

i have two table 'user_master' and 'role_master'

User_model.php

class User_model extends BaseModel {
    public function __construct() {
        $this->table = 'user_master';
        $this->primary_key = 'USER_ID';
        $this->soft_deletes = true;
        $this->has_one ['role'] = array (
                'foreign_model' => 'Role_model',
                'foreign_table' => 'role_master',
                'foreign_key' => 'ROLE_ID',
                'local_key' => 'ROLE_ID' 
        );

        parent::__construct ();
    }
}

Role_model.php

class Role_model extends BaseModel
{
    public function __construct() {
        $this->table = 'role_master';
        $this->primary_key = 'ROLE_ID';
        $this->soft_deletes = true;
        $this->has_many ['users'] = array (
                'local_key' => 'ROLE_ID',
                'foreign_key' => 'ROLE_ID',
                'foreign_model' => 'User_model' 
        );
        parent::__construct ();
    }
}

Controller : Admin.php

$this->security->xss_clean ( $post_data = array (
    'EMAIL' => $this->input->post ( 'email' ),
    'PASSWORD' => _md5_format ( $this->input->post ( 'password' ) ),
) );

$this->load->model ( 'user_model' );            
$data['user'] = $this->user_model->where($post_data)->where_role('fields:ROLE_NAME,LOGIC_NAME','where:`role_master`.`LOGIC_NAME`=\'ADMINISTRATION\'')->get_all();

Error

Unknown column 'user_master.roles' in 'where clause'

SELECT * FROM `user_master` WHERE `EMAIL` = 'admin@admin.com' AND `PASSWORD` = 'f925916e2754e5e03f75dd58a5733251' AND `user_master`.`roles` IN('fields:ROLE_NAME,LOGIC_NAME', 'where:`role_master`.`LOGIC_NAME`=\'ADMINISTRATION\'')

Filename: C:/xampp/htdocs/demo/system/database/DB_driver.php

Line Number: 691

How solve this error

i want check EMAIL and PASSWORD in 'user_master' table and LOGIC_NAME check in 'role_master' how check in one go.

DB Structure

user_master

CREATE TABLE `user_master` (
    `USER_ID` INT(11) NOT NULL AUTO_INCREMENT,
    `EMAIL` VARCHAR(100) NOT NULL,
    `PASSWORD` VARCHAR(50) NOT NULL,
    `ROLE_ID` INT(11) NOT NULL,
    `FORGOT_PASSWORD_CODE` VARCHAR(50) NULL DEFAULT NULL,
    `INACTIVE` TINYINT(1) NOT NULL DEFAULT '0',
    `LOGIN_ACTIVITY` DATETIME NULL DEFAULT NULL,
    `CREATED_DATE` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
    `UPDATED_DATE` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`USER_ID`),
    INDEX `FK_user_master_role_master` (`ROLE_ID`),
    CONSTRAINT `FK_user_master_role_master` FOREIGN KEY (`ROLE_ID`) REFERENCES `role_master` (`ROLE_ID`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2131
;

role_master

CREATE TABLE `role_master` (
    `ROLE_ID` INT(11) NOT NULL AUTO_INCREMENT,
    `ROLE_NAME` VARCHAR(50) NOT NULL,
    `LOGIC_NAME` VARCHAR(50) NOT NULL,
    `INACTIVE` TINYINT(1) NOT NULL DEFAULT '0',
    `CREATED_DATE` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
    `UPDATED_DATE` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`ROLE_ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=4
;