AshishJoshi-asj / zfdatagrid

Automatically exported from code.google.com/p/zfdatagrid
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Setting class method callback for CRUD fails #175

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. in (model) class set callback for a CRUD op:
  $form->setCallbackBeforeUpdate(array($this,'calbck'));
2. Add Form to Grid
  $grid->addForm($this->_zfDatagridForm);
3.

What is the expected output? What do you see instead?
Class method called

Warning: call_user_func_array() [function.call-user-func-array]: First
argument is expected to be a valid callback, 'Array' was given in
C:\...\Bvb\Grid\Deploy\Table.php on line 430

What version of the product are you using? On what operating system?
0.6alpha

Please provide any additional information below.

I think to problem is in converting Form to array on line 1615 (Table.php)

>>>>  $form = $this->_object2array($form); <<<<

 $fieldsGet = $form['fields'];
 $fields = array();
 if (isset($form['options']['callbackBeforeDelete'])) {
   $this->_callbackBeforeDelete = $form['options']['callbackBeforeDelete'];
 }

Callback contains an object reference, and when it is converted to array,
it is not an object anymore.

 if (isset($form->options['callbackBeforeDelete'])) {
   $this->_callbackBeforeDelete = $form->options['callbackBeforeDelete'];
 }

Original issue reported on code.google.com by vlatko.b...@gmail.com on 16 Feb 2010 at 6:30

GoogleCodeExporter commented 9 years ago
Hi,

You are totally right.

Thanks for noticing

Update to Revision 621

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 17 Feb 2010 at 1:58

GoogleCodeExporter commented 9 years ago
The callback gets called, but the changed field value isn't passed back to the 
$post
variable.

// setup
  $form->setCallbackBeforeUpdate(array($this,'calbck'));

// function
  public function calbck(&$fields) { //  or calbck($fields)
    $fields['userID'] = 1;
  }

Also, is there a way to pass some more params to callback. Like

  $form->setCallbackBeforeUpdate(array($this,'calbck'), array('param1', 'param2));

  function calbck($fields, $params)

Original comment by vlatko.b...@gmail.com on 17 Feb 2010 at 10:36

GoogleCodeExporter commented 9 years ago
Just found in docs that the function should be called as 

  call_user_func_array($this->_callbackBeforeUpdate, array(&$post));

This way params are passed by reference.

Or 
  call_user_func_array(
   $this->_callbackBeforeUpdate, 
   array(
    &$post, 
    array('some param', $this, 'other param')
  );

Tried it and it works nice.

Original comment by vlatko.b...@gmail.com on 17 Feb 2010 at 11:40

GoogleCodeExporter commented 9 years ago
Yes.

Again, thanks for reporting.

Check the latest revision

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 17 Feb 2010 at 4:07

GoogleCodeExporter commented 9 years ago
Hi,

checked. It works. 

However, would it be possible to automatically send the Model as param, if it 
is set?

Models often have methods that can be useful. If callback function is not from 
the
model, function can not know to which instance of Model the data belong. Or 
maybe we
can define custom params?

What do you think?

Original comment by vlatko.b...@gmail.com on 17 Feb 2010 at 4:41

GoogleCodeExporter commented 9 years ago
Hi,

Done.

Check revision 628

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 17 Feb 2010 at 5:08

GoogleCodeExporter commented 9 years ago
Hi,

works.

Just for the reference, in callback to change the values we must use reference 
if od
array:
  public function calbck($params) {
   $fields = &$params[0];
   $model = &$params[1];
   $fields['userID'] = 5;
}

Confirmed and Verified

Original comment by vlatko.b...@gmail.com on 17 Feb 2010 at 5:18

GoogleCodeExporter commented 9 years ago

Original comment by bento.vi...@gmail.com on 3 Mar 2010 at 4:56