hamzataji / dev

transfère
0 stars 0 forks source link

gc #1

Open hamzataji opened 6 years ago

hamzataji commented 6 years ago

set_model void set_model( string $model_as_string ); Quick Description: Sets the model that crud will use ( The model always must extends grocery_Model ) Sets the model that crud will use .The model always must extends grocery_CRUD_Model . grocery_CRUD_Model extends CI_Model so you don't have to be afraid to use it.

Below I explain with steps how you can use the set_model

1st STEP - go to your basic function and add your custom model

function just_an_example() {
$crud = new grocery_CRUD();

$crud->set_model('My_Custom_model'); $crud->set_table('film'); $crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');

$output = $crud->render();

$this->_example_output($output);

} 2nd STEP-Your custom model MUST extend grocery_CRUD_Model. So create a new file at application/models/my_custom_model.php and it will be something like this:

class My_Custom_model extends grocery_CRUD_Model {

function get_relation_n_n_unselected_array($field_info, $selected_values)

{ $selection_primary_key = $this->get_primary_key($field_info->selection_table);

if($field_info->name = '....')

{ $this->db->where(....); .......your custom queries }

$this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");

$results = $this->db->get($field_info->selection_table)->result();

$results_array = array(); foreach($results as $row)

{ if(!isset($selected_values[$row->{$field_info->primary_key_alias_to_selection_table}])) $results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_info->title_field_selection_table}; }

return $results_array;
}

} The set_model function is good because you don't have to change the core of grocery crud. You can create whatever model you like if you just want to do more complicated queries.

For another example you can also see a full working example of using the set_model »

hamzataji commented 6 years ago

https://gist.github.com/scoumbourdis/2b75b1910b343ea00ce1fb310fffe02c

hamzataji commented 6 years ago

<?php //CustomersModel.php

use GroceryCrud\Core\Model; use GroceryCrud\Core\Model\ModelFieldType;

class CustomersModel extends Model {

protected $ci;
protected $db;

function __construct($databaseConfig) {
    $this->setDatabaseConnection($databaseConfig);

    $this->ci = & get_instance();
    $this->db = $this->ci->db;
}

public function getFieldTypes($tableName)
{
    $fieldTypes = parent::getFieldTypes($tableName);

    $fullNameFieldType = new ModelFieldType();
    $fullNameFieldType->dataType = 'varchar';

    $countOrdersFieldType = new ModelFieldType();
    $countOrdersFieldType->dataType = 'varchar';

    $fieldTypes['fullname'] = $fullNameFieldType;
    $fieldTypes['count_orders'] = $countOrdersFieldType;

    return $fieldTypes;
}

public function getOne($id)
{
    $customer = parent::getOne($id);

    $this->db->select('COUNT(*) as count_orders');
    $this->db->where('customerNumber', $id);
    $customer['count_orders'] = $this->db->get('orders')->row()->count_orders;

    return $customer;
}

public function getList() {

    $order_by = $this->orderBy;
    $sorting = $this->sorting;

    // All the custom stuff here
    $this->db->select('customers.customerNumber, CONCAT(customerName, \' \' ,contactLastName) as fullname, phone, city, country, COUNT(*) as count_orders', false);
    $this->db->join('orders', 'orders.customerNumber = customers.customerNumber', 'left');
    $this->db->group_by('customers.customerNumber');

    if ($order_by !== null) {
        $this->db->order_by($order_by. " " . $sorting);
    }

    if (!empty($this->_filters)) {
        foreach ($this->_filters as $filter_name => $filter_value) {
            $this->db->like($filter_name, $filter_value);
        }
    }

    if (!empty($this->_filters_or)) {
        foreach ($this->_filters_or as $filter_name => $filter_value) {
            $this->db->or_like($filter_name, $filter_value);
        }
    }

    $this->db->limit($this->limit, ($this->limit * ($this->page - 1)));
    $results = $this->db->get($this->tableName)->result_array();

    return $results;

}

public function getTotalItems()
{
    return parent::getTotalItems();
}

}

hamzataji commented 6 years ago

<?php //CustomersModel.php

use GroceryCrud\Core\Model; use GroceryCrud\Core\Model\ModelFieldType;

class CustomersModel extends Model {

protected $ci;
protected $db;

function __construct($databaseConfig) {
    $this->setDatabaseConnection($databaseConfig);

    $this->ci = & get_instance();
    $this->db = $this->ci->db;
}

public function getFieldTypes($tableName)
{
    $fieldTypes = parent::getFieldTypes($tableName);

    $fullNameFieldType = new ModelFieldType();
    $fullNameFieldType->dataType = 'varchar';

    $countOrdersFieldType = new ModelFieldType();
    $countOrdersFieldType->dataType = 'varchar';

    $fieldTypes['fullname'] = $fullNameFieldType;
    $fieldTypes['count_orders'] = $countOrdersFieldType;

    return $fieldTypes;
}

public function getOne($id)
{
    $customer = parent::getOne($id);

    $this->db->select('COUNT(*) as count_orders');
    $this->db->where('customerNumber', $id);
    $customer['count_orders'] = $this->db->get('orders')->row()->count_orders;

    return $customer;
}
hamzataji commented 6 years ago

`<?php //CustomersModel.php

use GroceryCrud\Core\Model; use GroceryCrud\Core\Model\ModelFieldType;

class CustomersModel extends Model {

protected $ci;
protected $db;

function __construct($databaseConfig) {
    $this->setDatabaseConnection($databaseConfig);

    $this->ci = & get_instance();
    $this->db = $this->ci->db;
}

public function getFieldTypes($tableName)
{
    $fieldTypes = parent::getFieldTypes($tableName);

    $fullNameFieldType = new ModelFieldType();
    $fullNameFieldType->dataType = 'varchar';

    $countOrdersFieldType = new ModelFieldType();
    $countOrdersFieldType->dataType = 'varchar';

    $fieldTypes['fullname'] = $fullNameFieldType;
    $fieldTypes['count_orders'] = $countOrdersFieldType;

    return $fieldTypes;
}

public function getOne($id)
{
    $customer = parent::getOne($id);

    $this->db->select('COUNT(*) as count_orders');
    $this->db->where('customerNumber', $id);
    $customer['count_orders'] = $this->db->get('orders')->row()->count_orders;

    return $customer;
}

`

hamzataji commented 6 years ago

` public function getList() {

    $order_by = $this->orderBy;
    $sorting = $this->sorting;

    // All the custom stuff here
    $this->db->select('customers.customerNumber, CONCAT(customerName, \' \' ,contactLastName) as fullname, phone, city, country, COUNT(*) as count_orders', false);
    $this->db->join('orders', 'orders.customerNumber = customers.customerNumber', 'left');
    $this->db->group_by('customers.customerNumber');

    if ($order_by !== null) {
        $this->db->order_by($order_by. " " . $sorting);
    }

    if (!empty($this->_filters)) {
        foreach ($this->_filters as $filter_name => $filter_value) {
            $this->db->like($filter_name, $filter_value);
        }
    }

    if (!empty($this->_filters_or)) {
        foreach ($this->_filters_or as $filter_name => $filter_value) {
            $this->db->or_like($filter_name, $filter_value);
        }
    }

    $this->db->limit($this->limit, ($this->limit * ($this->page - 1)));
    $results = $this->db->get($this->tableName)->result_array();

    return $results;

}

public function getTotalItems()
{
    return parent::getTotalItems();
}

}`

hamzataji commented 6 years ago

`$model = new \CustomersModel($db); $crud->setModel($model);

$crud->unsetAdd(); $crud->setTable('customers'); $crud->columns(['fullname', 'phone', 'country', 'count_orders']); $crud->fields(['customerName', 'contactLastName', 'phone', 'country', 'count_orders']); $crud->readOnlyFields(['count_orders']); $crud->displayAs('count_orders','Orders');

$output = $crud->render();`

hamzataji commented 6 years ago

`<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_Custom_model extends grocery_CRUD_Model {

function get_relation_n_n_unselected_array($field_info, $selected_values)

{ $selection_primary_key = $this->get_primary_key($field_info->selection_table);

if($field_info->name = 'film_id')

{ $where="release_year like '2007'"; $this->db->where($where);

}

$this->db->order_by("{$field_info->selection_table}.{$field_info->title_field_selection_table}");

$results = $this->db->get($field_info->selection_table)->result();

$results_array = array(); foreach($results as $row)

{ if(!isset($selected_values[$row->{$field_info->primary_key_alias_to_selection_table}])) $results_array[$row->{$field_info->primary_key_alias_to_selection_table}] = $row->{$field_info->title_field_selection_table}; }

return $results_array; }

} ?>`

hamzataji commented 6 years ago

` function just_an_example2() { $crud = new grocery_CRUD(); $crud->set_model('custom_query_model');

    $crud->set_table('employees'); //Change to your table name

    $crud->basic_model->set_query_str('SELECT * FROM employees as ee left join offices as of on of.officeCode=ee.officeCode  where ee.officeCode like 1 '); //Query text here
    $output = $crud->render();

        $this->_example_output($output);

}`
hamzataji commented 6 years ago

`<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Custom_query_model extends grocery_CRUD_model {

private  $query_str = '';
function __construct() {
    parent::__construct();
}

function get_list() {
    $query=$this->db->query($this->query_str);

    $results_array=$query->result();
    return $results_array;
}

public function set_query_str($query_str) {
    $this->query_str = $query_str;
}

}`

hamzataji commented 6 years ago

`<!DOCTYPE html>

'>Customers | '>Orders | '>Products | '>Offices | '>Employees | '>Films | '>Multigrid [BETA]

`

hamzataji commented 6 years ago

<!DOCTYPE html>

'>Customers | '>Orders | '>Products | '>Offices | '>Employees | '>Films | '>Multigrid [BETA]