⚠ 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

Update issues with relation_n_n & newb questions



25cc

25cc
  • profile picture
  • Member

Posted 17 June 2013 - 06:52 AM

Hi! Everyone,

 

Let me take this opportunity to thank Grocery Crud developers, it has simplified life of programmers for sure.

 

Thank you.

 

Scenario:

 

If we refer to example for set_relation_n_n given in the documentation

(http://www.grocerycrud.com/documentation/options_functions/set_relation_n_n), the three tables film, film_actor and actor. Lets add one column to film_actor let say nick_name ( nick name of the actor in that particular film).

 

Question:

1. I want to insert to film_actor not only the relationship keys but also nick_name.

 

2. I hear this is for back end, is there a problem if someone controls the access to actions and use it in front end?

 

3. Is it possible to output data from custom model in themes (databases/flexigrid)?

 

Replies with code snippets and/or examples will be appreciated.

 

Thank you all in advance.


davidoster

davidoster
  • profile picture
  • Member

Posted 17 June 2013 - 09:02 AM

Hello [member=25cc] and welcome to the forums.

 

Question: How do we add this column (nick_name) to the film_actor? Either by physically adding the field to the database or by using the columns and/or fields.

 

Let's assume that if we physically add the field (nick_name)

r0a8.png

 

then on 1. you don't need to do anything except changing this code to, 

$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', '{fullname} "{nick_name}" {last_update}','priority');

Check it here: http://eletter.gr/samples/gc_ic/index.php/gc_examples/film_management/add

 

2. You might have seen on the forums that some people say that Grocery CRUD is better for the backend.

My opinion is that it all depends on

a. what system/application you need to built

b. what kind of security directions need to be followed

c. what this application will do

d. is this application going to have different access levels?

 

3. this uses a custom model

 

I hope this answers your questions.


25cc

25cc
  • profile picture
  • Member

Posted 17 June 2013 - 12:27 PM

Thank you for the reply.

 

3. OK I will try doing that.

 

2. Ok

 

1. I understood it but the thing is I want to supply a value to that nick_name field in film_actor table while updating films table i.e when I am inserting a new film I choose an actor from actors table then enter the nick_name.

 

Additional questions:

 

1. I am trying to use callback_after_insert function to update but it does not work, find my codes below

 

function transaction_management() {
        try {
            $crud = new grocery_CRUD();
            $crud->set_theme('datatables');
            $crud->set_table('financial_transactions');
            $crud->set_subject('financial_transactions');
            $crud->set_relation('transaction_type_code', 'transaction_type', 'transaction_type_description');
            $crud->set_relation_n_n('accounts', 'accounts_in_transactions', 'chart_of_accounts','transaction_id','account_number','account_name');
            $crud->set_relation('party_id', 'members', 'first_name');
            $crud->columns('transaction_type_code','transaction_date','amount','party_id','accounts');
           
            $crud->callback_after_insert(array($this, 'multi_tran_after_insert'));
           
            $output = $crud->render();

            $this->_example_output($output);
        } catch (Exception $e) {
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }
    }

 

 

function multi_tran_after_insert($post_array,$primary_key) {
        $cash_data = array(
            'transaction_id' => $primary_key,
            'account_number'=> '1',
            'amount'=> $post_array['amount'],
            'transaction_nature'=> 'Dr'
        );
       
        $update_data = array(
            'amount1'=> $post_array['amount'],
            'transaction_nature'=> 'Cr'
        );
       
        $test_data = array(
            'first_name'=> 'tested'
        );
       
        $this->db->trans_start();
        $this->db->insert('accounts_in_transactions', $cash_data);
        $this->db->where('transaction_id', $primary_key);
        $this->db->update('accounts_in_transactions', $update_data);
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

 

I do not know why my callback_after_insert function is not called and executed.

 

Please help me analyse the codes above as it will be the alternative solution to questions asked earler.

 

Thank you


davidoster

davidoster
  • profile picture
  • Member

Posted 17 June 2013 - 12:55 PM

If you had asked this from the start my answer would have been different.

Anyway, please post the structure of the tables.


25cc

25cc
  • profile picture
  • Member

Posted 17 June 2013 - 14:53 PM

 

Ok, excuse me. Been trying to upload a picture but can not find the way thru and I am not sure if the above one will display if not please refer to codes below:

 

-- -----------------------------------------------------
-- Table `accountsdb`.`chart_of_accounts`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `accountsdb`.`chart_of_accounts` (
  `account_number` INT(11) NOT NULL ,
  `group_name` VARCHAR(45) NULL DEFAULT NULL ,
  `account_name` VARCHAR(45) NULL DEFAULT NULL ,
  `account_owner` INT(11) NULL ,
  PRIMARY KEY (`account_number`) ,
  INDEX `group_name` (`group_name` ASC) ,
  INDEX `account_owner` (`account_owner` ASC) ,
  CONSTRAINT `group_name`
    FOREIGN KEY (`group_name` )
    REFERENCES `accountsdb`.`account_groups` (`group_name` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `account_owner`
    FOREIGN KEY (`account_owner` )
    REFERENCES `accountsdb`.`departments` (`department_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1,
COMMENT = 'List of all accounts' ;

-- -----------------------------------------------------
-- Table `accountsdb`.`financial_transactions`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `accountsdb`.`financial_transactions` (
  `transaction_id` INT(11) NOT NULL AUTO_INCREMENT ,
  `transaction_type_code` INT(11) NULL DEFAULT NULL ,
  `transaction_date` DATE NULL DEFAULT NULL ,
  `amount` INT(11) NULL DEFAULT NULL ,
  `transaction_description` VARCHAR(100) NULL DEFAULT NULL ,
  `party_id` INT(11) NULL DEFAULT NULL COMMENT 'id of payer or payee\nShould reference members or departments table...' ,
  `party_type` VARCHAR(45) NULL DEFAULT NULL COMMENT 'type of payer or payee i.e member, department etc' ,
  `entered_by` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`transaction_id`) ,
  INDEX `transaction_type_code` (`transaction_type_code` ASC) ,
  CONSTRAINT `transaction_type_code`
    FOREIGN KEY (`transaction_type_code` )
    REFERENCES `accountsdb`.`transaction_type` (`transaction_type_code` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 5
DEFAULT CHARACTER SET = latin1,
COMMENT = 'Details of the actual transaction....' ;

-- -----------------------------------------------------
-- Table `accountsdb`.`accounts_in_transactions`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `accountsdb`.`accounts_in_transactions` (
  `transaction_id` INT(11) NOT NULL ,
  `account_number` INT(11) NOT NULL DEFAULT '0' ,
  `amount` INT(11) NOT NULL COMMENT 'Saves the amount cr/dr in that particular account' ,
  `transaction_nature` VARCHAR(2) NULL DEFAULT NULL COMMENT 'Debited or credited?' ,
  INDEX `transaction_id` (`transaction_id` ASC) ,
  INDEX `account_number` (`account_number` ASC) ,
  PRIMARY KEY (`transaction_id`, `account_number`) ,
  CONSTRAINT `account_number_1`
    FOREIGN KEY (`account_number` )
    REFERENCES `accountsdb`.`chart_of_accounts` (`account_number` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `transaction_id_1`
    FOREIGN KEY (`transaction_id` )
    REFERENCES `accountsdb`.`financial_transactions` (`transaction_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1,
COMMENT = 'Shows which accounts are affected by a particular transactio' ;