简体   繁体   English

在杂货堆中进行一些逻辑运算后如何插入数据

[英]how to insert data after some some logical operation in grocery crud

i am very new to grocery crud.i have facing some problem with logical operation on insert_before_callback 我对杂货杂货店非常陌生。我在insert_before_callback上的逻辑运算遇到了一些问题

i have a product table,when i will sell a product it will check the availability of the product.if the its available then it will insert it to the database 我有一个产品表,当我销售产品时,它将检查该产品的可用性。如果有,则将其插入数据库

        $crud = new grocery_CRUD();
    //$crud->set_subject('Guards');
    $this->grocery_crud->set_table('chalan')
        ->set_subject('Chalan')
        //->fields('Date','cid','pid','voucher','qnty','Amount','Paid Amount','Due Amount','Remarks')
        ->columns('cdate','cid','eid','pid','voucher','qnty','price','Amount','paid','Due Amount','Check','remark')
        ->display_as('cdate','Date')
        ->display_as('check','Check')
        ->display_as('cid','Customer Name')
        ->display_as('voucher','Chalan No.')
        ->display_as('eid','Employee Name')
        ->display_as('pid','Item Name')
        ->display_as('qnty','Quantity')
        ->display_as('price','Price')
        ->display_as('remark','Remark')
        ->set_relation('eid','employee','name')
        ->set_relation('cid','Customer','name')
        ->set_relation('pid','product','name')
        ->callback_column('Amount',array($this,'_total_amount'))
        ->callback_column('Due Amount',array($this,'_due_amount'))
        ->set_rules('qnty','Quantity','numeric')
        ->set_rules('eid','Employee','String')
        ->set_rules('pid','Product','String')
        ->set_rules('qnty','Quantity','numeric')
        ->set_rules('cdate','Date','date')
        ->callback_before_insert(array($this,'_checkAvaiability'))
        ->unset_delete();
    //$crud->display_as('sec_guard_master_id','Guard Name');
    $output = $this->grocery_crud->render();
    //$data['viewName']="welcome";
    $this->load->view('search',$output);

here is my logic for _checkAvaiability 这是我的_checkAvaiability逻辑

function _checkAvaiability($post_array){
    $total=100; // just for test
    if($post_array['qnty']>$total){
        return false;
    }
}

it runs without any filtering and always insert the data though the product exceed the total value.it will be vry helpful if some one fix this 它运行时没有任何过滤,并且总是在产品超过总值时插入数据。如果有人解决了这个问题,这将非常有帮助

Note that callback_before_insert does NOT has the behavior of stoping the insert if you return false (in fact, this callback must return an array containing the values that will be used in the insert operation that will happen right after the callback´s processing). 请注意,如果您返回false,则callback_before_insert不会停止插入(实际上,此回调必须返回一个数组,其中包含将在插入操作之后使用的插入操作中使用的值)。

If you need some logic validation before a insert (in order to not allow it´s execution based on yours logic) there two things you could do: 如果您需要在插入之前进行一些逻辑验证(为了不允许基于您的逻辑执行该插入),则可以执行以下两项操作:

1) Create a custom validation rule and add it using set_rules http://www.grocerycrud.com/documentation/options_functions/set_rules 1)创建一个自定义验证规则,并使用set_rules http://www.grocerycrud.com/documentation/options_functions/set_rules添加它

I think this is probalby easier to do 我认为这很容易做到

2) You could implement a totally custom insert operation using the callback_insert . 2)您可以使用callback_insert实现完全自定义的插入操作。 This callback escapes the auto insert of the CRUD, and runs only the inserted callback. 此回调会转义CRUD的自动插入,并仅运行插入的回调。

http://www.grocerycrud.com/documentation/options_functions/callback_insert http://www.grocerycrud.com/documentation/options_functions/callback_insert

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM