Search

Oct 12, 2015

Yii: Insert new record to database - Cdbcommand

// Save to database
    public function save(){
        $conn = Yii::app()->db;
        $conn->active = true; // Start connect
        $command = $conn->createCommand();
        $command->insert('archives', array(
            'IdUserCreate' => $this->idUserCreate,
            'Name'=> $this->name,
            'Path'=> $this->path,
            'DateCreate' => $this->dateCreate,
        ));
        $conn->active =  false; // Close connect

    }

Yii: Get all record by id - Cdbcommand Yii


// Get all record from archives table by IdUserCreate
    public function getAllByUserId($id) {
        $conn = Yii::app()->db;
        $conn->active = true; // Start connect
        $command = $conn->createCommand();
        $rows = $command->select('*')
            ->from('archives')
            ->where('IdUserCreate=:id', array(':id'=>$id))
            ->query();
        $result = $rows->readAll();
        $conn->active =  false; // Close connect
        return $result;
    }