Jump to content

via Ad Packs

Create an alias of a column

Alias

  • Please log in to reply
31 replies to this topic

#1 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 24 June 2012 - 11:01 AM

Hello friends, I am using the grocery CRUD for one of my projects. Its really nice. However, I have a small problem. I have set a relation between 2 tables and they are working just fine except they have 1 common field name. I need to set an alias for a column. Is that possible ? I didnot find anything in the API Doc. May be I missed, I'm not sure. Can you guys help ? thnx in advance.

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 24 June 2012 - 11:13 AM

Hello and welcome to the forum. This is probably a bug so I am looking on it. For any news about it I will inform you.
For now a workaround for this till will be fixed is to have only the one of the two columns there or ... if it is possible to rename the one of the two fields. So you can use display_as to change it and add your names there.

Kindest Regards
Johnny
Posted Image

#3 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 24 June 2012 - 11:27 AM

Thanx a lot for the reply. I think at this time it will be a real pain for me to change the column names of the tables but this is a design mistake. I should have given distinct column names. I will wait for your bugfix.

Best regards,
Arefin

#4 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 24 June 2012 - 11:38 AM

I think that it not a good technique to have distinct column names. So it is better to just unset these columns from the list grid for now. In my oppinion I hate when for example I have the table name subjects and it is like this:
subjects
- subject_id (this is ok)
- subject_name
- subject_description
- subject_date
....
it is better to have
subjects
- subject_id
- name
- description
- inserted_date
...

So it is not a design mistake for me it's just OK.

I also added as a bug to track it and it will probably be fixed as for the next version ( https://github.com/s...-crud/issues/68 )
Posted Image

#5 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 24 June 2012 - 12:24 PM

Hey, I didnot mean that it was your mistake. However we share the same thought. I will keep you busy with the bugs. :)

#6 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 24 June 2012 - 12:32 PM

hahahahah :) I didn't meant that it was my mistake ( I just read my post and it was a bit misunderstanding ). I just said that you don't have to change your design as for me it's seems to be right to have same names at tables. You will just have to wait for the bug fix.
Thanks to share your thoughts and thanks to mention the bug B)
Posted Image

#7 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 24 June 2012 - 12:51 PM

Hello again Jubayer Arefin I looked at it and it is not a bug. Can you please post your code, your tables and a print-screen to check what it is wrong with this?
Posted Image

#8 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 24 June 2012 - 01:45 PM

This is the function:

public function view() {
	    $data = array();
	    if (!($this->ion_auth->logged_in() && $this->ion_auth->is_admin())) {
		    $data['login'] = TRUE;
		    $data['logout'] = FALSE;
		    $data['label'] = 'Signin';
		    redirect($this->config->item('base_url') . 'auth', 'refresh');
	    } else {
		    $data['login'] = FALSE;
		    $data['logout'] = TRUE;
		    $data['label'] = 'Signout';
	    }
	    $this->grocery_crud->set_theme('datatables');
	    $this->grocery_crud->set_table('subcategories');
	    //Set relation
	    $this->grocery_crud->display_as('category_id','Category Name');
	    $this->grocery_crud->set_subject('Subcategory');
	    $this->grocery_crud->set_relation('category_id','categories','name');
	    //Set relation
	    //Validation
	    $this->grocery_crud->required_fields('name','description');
	    $output = $this->grocery_crud->render();
	    $this->load->view('header');
	    $this->load->view('site_start');
	    $this->load->view('admin/menu', $data);
	    $this->load->view('admin/category/subcategory/view', $output);
	    $this->load->view('admin/footer');
    }

Attached the images of db and the output
Posted Image
Posted Image

cheers :)

#9 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 24 June 2012 - 02:18 PM

You can simply take the value with a callback_column for example:

//As for PHP version >= 5.3
   $this->grocery_crud->callback_column('name',function($value,$row){
		return $row->name;
   });

or as for PHP version < 5.3
//As for PHP version < 5.3
   $this->grocery_crud->callback_column('name',array($this,'custom_callback_column'));

and

public function custom_callback_column($value,$row){
		return $row->name;
   }

at your main controller
Posted Image

#10 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 24 June 2012 - 04:31 PM

I did not understand. I'm confused. What did you mean by main controller ? Can you plz change my code and repost it ?

#11 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 24 June 2012 - 06:27 PM

yeap no problem. So in your case you have to do:

public function view() {
				$data = array();
				if (!($this->ion_auth->logged_in() && $this->ion_auth->is_admin())) {
						$data['login'] = TRUE;
						$data['logout'] = FALSE;
						$data['label'] = 'Signin';
						redirect($this->config->item('base_url') . 'auth', 'refresh');
				} else {
						$data['login'] = FALSE;
						$data['logout'] = TRUE;
						$data['label'] = 'Signout';
				}
				$this->grocery_crud->set_theme('datatables');
				$this->grocery_crud->set_table('subcategories');
				//Set relation
				$this->grocery_crud->display_as('category_id','Category Name');
				$this->grocery_crud->set_subject('Subcategory');
				$this->grocery_crud->set_relation('category_id','categories','name');

			   $this->grocery_crud->callback_column('name',array($this,'custom_callback_column'));

				//Set relation
				//Validation
				$this->grocery_crud->required_fields('name','description');
				$output = $this->grocery_crud->render();
				$this->load->view('header');
				$this->load->view('site_start');
				$this->load->view('admin/menu', $data);
				$this->load->view('admin/category/subcategory/view', $output);
				$this->load->view('admin/footer');
		}

public function custom_callback_column($value,$row){
				return $row->name;
   }


For more about how to use the callback_column you can see and example at: http://www.grocerycr..._column_example and http://www.grocerycr...callback_column
Posted Image

#12 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 25 June 2012 - 03:15 AM

I did the same before I requested but same result. I dont know whats the problem. Do you want to take a look at the whole source ? I can setup a svn acc for you.

#13 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 25 June 2012 - 06:25 AM

You can always fork my project at github https://github.com/s...is/grocery-crud but still... I want to understand the problem. Can you post your table like this?

CREATE TABLE IF NOT EXISTS `employees` (
  `employeeNumber` int(11) NOT NULL AUTO_INCREMENT,
  `lastName` varchar(50) NOT NULL,
  `firstName` varchar(50) NOT NULL,
  `extension` varchar(10) NOT NULL,
  `email` varchar(100) NOT NULL,
  `officeCode` varchar(10) NOT NULL,
  `file_url` varchar(250) CHARACTER SET utf8 NOT NULL,
  `jobTitle` varchar(50) NOT NULL,
  PRIMARY KEY (`employeeNumber`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1703 ;

also can you please tell me what codeigniter version do you use and what grocery CRUD version?

Sorry but I still don't understand where the problem can be as I didn't have a same bug before :huh:
Posted Image

#14 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 25 June 2012 - 08:50 AM

My CI version is 2.1.0 and GroceryCRUD version is 1.2

Subcategory:
DROP TABLE IF EXISTS `subcategories`;
CREATE TABLE `subcategories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` varchar(255) NOT NULL,
  `category_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_category` (`category_id`),
  CONSTRAINT `fk_category` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Category:
DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `cat_name` (`name`) USING BTREE,
  KEY `cat_desc` (`description`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;


#15 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 25 June 2012 - 10:02 PM

I just noticed that you have ordering by Name so the empty columns will show-up first. Can you just go to the last page to see if this works for you? Perhaps some time the solution is in front of our eyes and easier than we think :)
Posted Image

#16 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 26 June 2012 - 04:54 AM

Actually the name column for both table is required so no empty fields. I just checked now to be sure and its okay. Thanks for mentioning :) I am sending you the table with data.

#17 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 26 June 2012 - 06:24 AM

It is really strange I just tested in my local machine with your versions of CI and grocery CRUD and it works as expected .

Untitled-1.png

So perhaps it is a key problem that grocery CRUD haven't recognized the primary key but not sure as it is a really strange bug. A solution for this is to update the grocery CRUD to the latest version 1.2.3 (you will not have any problem with the update probably), just make sure that you copy all the files (included the assets folder) and not only the library and the model. After upgrading it you can have this simple line of code:

$crud->set_primary_key('id');

Hopefully this will solve the problem. But still I don't know what else to see. There is something with your local machine. So you have to install the grocery CRUD from scratch just to check that this is working for you and then debug it and see what is wrong with the template e.t.c. It is not the best solution but I really don't know what could cause this problem to you.
Posted Image

#18 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 26 June 2012 - 06:33 AM

Sorry I forgot to mention that I am using the latest version 1.2.3 from the beginning. I have set
$crud->set_primary_key('id');

But no result. One thing I am not sure about is that when I use
$this->grocery_crud->callback_column('name', array($this, 'custom_callback_column'));
how this should work bcoz both of the column name is 'name'. I am confused here.

#19 Jubayer Arefin

Jubayer Arefin

    Member

  • Members
  • PipPip
  • 19 posts
  • LocationDhaka, Bangladesh

Posted 26 June 2012 - 06:43 AM

And plz share the function you used to generate the crud.

#20 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 922 posts
  • LocationLondon

Posted 26 June 2012 - 07:14 AM

public function my_view() {
  $crud = new grocery_CRUD();

  $crud->set_theme('datatables');
  $crud->set_table('subcategories');
  //Set relation
  $crud->display_as('category_id','Category Name');
  $crud->set_subject('Subcategory');
  $crud->set_relation('category_id','categories','name');
  //Set relation
  //Validation
  //$crud->callback_column('name',array($this,'custom_callback_column'));

  $crud->required_fields('name','description');
  $output = $crud->render();

  $this->_example_output($output);
}

But still I have not luck to find where the problem can be
Posted Image





Also tagged with one or more of these keywords: Alias

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users