114 lines
4.2 KiB
PHP
114 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class ApiIntegrationModel extends Model
|
|
{
|
|
protected $table;
|
|
public function setTable($tableName)
|
|
{
|
|
$this->table = $tableName;
|
|
return $this;
|
|
}
|
|
|
|
public function insertData($table, $data)
|
|
{
|
|
try {
|
|
$this->db->table($table)->insert($data);
|
|
$last_insert_id = $this->db->insertID();
|
|
return ["info"=>$last_insert_id,"err"=>""];
|
|
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
|
|
return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
|
|
} catch (\Exception $e) {
|
|
return ["info"=>"","err"=>'An error occurred: ' . $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
public function updateData($table, $data, $where)
|
|
{
|
|
try {
|
|
$this->db->table($table)->update($data, $where);
|
|
$affected_rows = $this->db->affectedRows();
|
|
return ["info"=>$affected_rows." Affected","err"=>""];
|
|
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
|
|
return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
|
|
} catch (\Exception $e) {
|
|
return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
public function countAll($table, $where = null)
|
|
{
|
|
$query = $this->db->table($table);
|
|
if ($where) {
|
|
$query->where($where);
|
|
}
|
|
return $query->countAllResults();
|
|
}
|
|
|
|
|
|
public function getData($table, $where = null,$whereIn = null)
|
|
{
|
|
$query = $this->db->table($table);
|
|
if ($where) {
|
|
$query->where($where);
|
|
}
|
|
if($whereIn){
|
|
// $query->whereIn($column_name,$missingValues)
|
|
$query->whereIn($whereIn[0],$whereIn[1]);
|
|
}
|
|
return $query->get()->getResult();
|
|
}
|
|
|
|
public function insertAndUpdateChildDetails($data,$table_name,$column_name){
|
|
$i = 1;
|
|
$text = ' Child-table Name = '.str_replace("_", " ",ucwords($table_name)) .', Column = '.str_replace("_", " ",ucwords($column_name)) .'. ';
|
|
foreach ($data as $row) {
|
|
$id = isset($row[$column_name]) ? $row[$column_name] : "";
|
|
if($id != ''){
|
|
unset($row[$column_name]); // Remove the primary Id from the data to avoid updating it.
|
|
$where = [$column_name=>$id,'isactive'=>1];
|
|
$child_affected_rows = $this->updateData($table_name, $row, $where);
|
|
$text .= $child_affected_rows["info"] ? $i.') Id = '.$id.', '.$child_affected_rows["info"].'. '
|
|
: $i.') Id = '.$id.', Err = '.$child_affected_rows["err"].'. ';
|
|
}else{
|
|
$child_insert_id = $this->insertData($table_name, $row);
|
|
$text .= $child_insert_id["info"] ? $i.') Inserted ID = '.$child_insert_id["info"].'. '
|
|
: $i.') Err = '.$child_insert_id["err"].'. ' ;
|
|
}
|
|
$i++;
|
|
}
|
|
return $text;
|
|
}
|
|
|
|
// public function inactiveMissingDetails($table_name,$where,$whereIn){
|
|
// $dataToUpdate = ['isactive' => 0];
|
|
// $this->db->table($table_name)->where($where)->whereIn($whereIn[0],$whereIn[1])->update($dataToUpdate);
|
|
// }
|
|
public function inactiveMissingDetails($table_name,$dataToUpdate,$where = null,$whereIn = null)
|
|
{
|
|
$this->db->table($table_name);
|
|
$query = $this->db->table($table_name);
|
|
if ($where) {
|
|
$query->where($where);
|
|
}
|
|
if ($whereIn) {
|
|
$query->whereIn($whereIn[0], $whereIn[1]);
|
|
}
|
|
$query->update($dataToUpdate);
|
|
}
|
|
|
|
public function getBookCategories($product_id){
|
|
return $this->db->table('book_categories as BC')
|
|
->join('category as C', 'C.id = BC.category_id', 'left')
|
|
->join('books as B', 'B.book_id = BC.book_id', 'left')
|
|
->select('BC.id,BC.book_id,BC.category_id,C.wp_api_category_id,C.name as category_name,B.title as book_name,B.sku,B.duration')
|
|
->where(['BC.isactive'=>1,'B.wp_api_product_id'=>$product_id])
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
}
|