⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

how do I write a more complex 'where' in crud ?



sebastian
  • profile picture
  • Member

Posted 12 May 2013 - 07:50 AM

Hello

 

 

As you know, if you want to make a simple filter in the list, we just add a 'where' clause like this:

 

 
$crud->where('Email', $this->session->userdata('username'));
 

 

 

but my need for filtering is more complex because actually what I have in my table is an FK to another table, so I have to make a join of the FK of table B with the ID of table A where in that table A, the email is equal to this username I have given you in the form of a variable like written above

 

Of course, I know how to do that in normal scenarios but in a crud I don't know how that is possible because:

 

some pseudocode here:

 

$crud = new grocery_CRUD();
        $crud->set_subject('Assets');
        $crud->set_table('asset');
        $crud->where B.FK = A.ID and A.email = $this->session->userdata('username'));
 

 

how do I properly write those joins and so in crud syntax?

 

Note that I am wanting to do it so that it shows me filtered results only. I am not talking about set_relation etc as that would display me Select lists, that is another story.

 

regards


davidoster
  • profile picture
  • Member

Posted 12 May 2013 - 08:16 AM

For complex situations we tend to extend the Grocery CRUD model.

Check it here.


sebastian
  • profile picture
  • Member

Posted 12 May 2013 - 10:21 AM

Text below


sebastian
  • profile picture
  • Member

Posted 12 May 2013 - 10:37 AM

UPDATE
 
 
 
This is the code that made it work. It did at the first try 
 
 
 
Initally I have
 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
 
class Atributes_Copy extends CI_Controller {
    
    function __construct()
    {
        parent::__construct();
    
        $this->load->library('grocery_crud');
        $this->load->library('image_moo');
        $this->load->library('gc_dependent_select');
    }
    
    public function index()
    {
        ;
    
    
    }
    
    
    
    
    public function atributes_code()
    {
        $crud = new grocery_CRUD();
        $crud->set_subject('Assets');
        $crud->set_table('asset');
   
   $crud->set_model('My_Custom_model');
         $crud->where('Email', $this->session->userdata('username'));
        
   $crud->set_relation('object_type_fk','object_type','type_description');
   $crud->set_relation('seller_fk','seller', '{fname} - {lname} {telephone}'); # I dont like this much because how can you edit or update all those fields if they show up in just one select list?
   $crud->set_relation('duration_fk','duration','duration_desc');
 
   more relations...
 
   then we would continue with the usual stuff
 
 
                $config = array(
                'main_table' => 'asset',
                'main_table_primary' => 'asset_id',
                "url" => base_url() .'index.php/'. __CLASS__ . '/' . __FUNCTION__ . '/'
                );
        
            $categories = new gc_dependent_select($crud, $fields, $config);
 
             // the second method:
          $js = $categories->get_js();
 
 
then some other functions to upload pictures and resize them etc
 
So: coming back to what you pointed out I should so something like:
public function atributes_code()
    {
        $crud = new grocery_CRUD();
        $crud->set_subject('Assets');
        $crud->set_table('asset');
   
  $crud->set_model('My_Custom_model');
 
  # then the rest continues as I had it before 
 
 
And here in another page I create the custom model 
<?php
error_reporting(E_ALL);
 
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->seller);
    
 if($field_info->name = 'seller_id')
 {
 $this->db->from('asset a');
 $this->db->join('seller se', 'seller.seller_id = a.seller_fk');
 $crud->where('Email', $this->session->userdata('username')); # this email is in the seller's table
 
 }
    
   
    }
    
    
    
    
 }