⚠ 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

Error when trying to use $this-> inside of callbacks



website4you
  • profile picture
  • Member

Posted 22 April 2019 - 23:59 PM

Error when trying to use $this-> inside of callbacks 

 

When i try to use any of callbacks that require use of this-> inside of it o get this error: 

 

 Uncaught Error: Using $this when not in object context in 

$crud->callbackAfterInsert(function ($stateParameters) {
    $this->db->where('id', $stateParameters->insertId);

Throws error at 2nd line of code. (just snip of callback) 

 

Here is full:

<?php

include("libraries/autoload.php");


use GroceryCrud\Core\GroceryCrud;

$database = include('database.php');
$config = include('config.php');

$crud = new GroceryCrud($config, $database);



$crud->setTable('list');
$crud->setSubject('Contract', 'Contracts');
$crud->setRead();
$crud->readOnlyFields(['CONTRACT_TYPE','DAY']);

$crud->callbackAfterInsert(function ($stateParameters) {
    $this->db->where('id', $stateParameters->insertId);
    $record = $this->db->get('list')->row();

    if (!empty($record)) {
        $this->db->update('list',
            ['LF_TOCID' => 11],
            ['id' => $stateParameters->insertId]);
    }

    return $stateParameters;
});



$output = $crud->render();


if ($output->isJSONResponse) {
    header('Content-Type: application/json; charset=utf-8');
    echo $output->output;
    exit;
}

$css_files = $output->css_files;
$js_files = $output->js_files;
$output = $output->output;

include('view.php');

Spend whole day trying to fix it.

Can someone help?

Thanks. 


website4you
  • profile picture
  • Member

Posted 01 May 2019 - 15:37 PM

Is there a better place to ask for support?

Looks like most people who asked questions did not get any replies in a very long time. 


Ayer Siddharth
  • profile picture
  • Member

Posted 25 May 2019 - 07:41 AM

Is there a better place to ask for support?

Looks like most people who asked questions did not get any replies in a very long time. 

Hi, I am too facing this problem. I am stuck at point where i cannot get context of $this. I am not using CI or Laravel. can you please help??


Laurvin
  • profile picture
  • Member

Posted 25 May 2019 - 14:35 PM

Unfortunately I have the exact same problem as well.

 

Even if I copy the example right from here and change the table and field names to match my database I get the Uncaught Error.

 

I really hope someone will be able to shine some light on this soon! Without this function the script is useless for what I need it for.


web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 28 May 2019 - 12:22 PM

Hello all,

 

First of all I am sorry if my example is missleading. From what I understood from users that send me an email with the problem, usually this means that they probably don't have Codeigniter installed as a framework. The example is written only as a Codeigniter example as without any framework it is a slightly more complicated as you basically need the initial database connection. In case you are NOT using Codeigniter and you need a simple example of using customModel with a custom query you can also check the example here:
 

$model = new customModel($database);
$crud->setModel($model);

$crud->callbackAfterInsert(function ($stateParameters) use ($model) {
    $model->customInsert($stateParameters->insertId);

    return $stateParameters;
});

where customModel (that includes customInsert)

 

is the code that you can find here: https://gist.github.com/scoumbourdis/d296573d2d747d6b3387916c8beb60f6

 

Please let me know if this is making it a bit easier for you. If you are still having issues please let me know.

 

Regards

Johnny

 

P.S. As my time is very limited I am trying to mainly reply to emails so I am sorry if I am not very active at the forums. I hope you understand.


Laurvin
  • profile picture
  • Member

Posted 29 May 2019 - 18:04 PM

Thanks, that helped quite a bit and solved one of my two use cases.

 

The other one is where I update a different table based on what was inserted in the one with the callback function.

 

In the custom model you use this:

$row = $this->getRowFromSelect($select, $sql);

So I use that to get the columns I need from the just inserted row:

->columns(['mission_fk','ship_fk'])

But when I do the below:

$insert = $sql->insert('mission_ship');
$insert->columns(array('mission_fk', 'ship_fk'));
$insert->values(array('mission_fk' => $row[0], 'ship_fk' => $row[1]));

I get:

 

Notice: Undefined offset: 1 in (etc.)

Warning: PDO::quote() expects parameter 1 to be string, object given

 

 

Hello all,

 

First of all I am sorry if my example is missleading. From what I understood from users that send me an email with the problem, usually this means that they probably don't have Codeigniter installed as a framework. The example is written only as a Codeigniter example as without any framework it is a slightly more complicated as you basically need the initial database connection. In case you are NOT using Codeigniter and you need a simple example of using customModel with a custom query you can also check the example here:
 

$model = new customModel($database);
$crud->setModel($model);

$crud->callbackAfterInsert(function ($stateParameters) use ($model) {
    $model->customInsert($stateParameters->insertId);

    return $stateParameters;
});

where customModel (that includes customInsert)

 

is the code that you can find here: https://gist.github.com/scoumbourdis/d296573d2d747d6b3387916c8beb60f6

 

Please let me know if this is making it a bit easier for you. If you are still having issues please let me know.

 

Regards

Johnny

 

P.S. As my time is very limited I am trying to mainly reply to emails so I am sorry if I am not very active at the forums. I hope you understand.

 


web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 04 June 2019 - 08:47 AM

Hello @Laurvin,

 

I am a bit confused to be honest with your question! Usually you have one $row so the error is correct. I think what you are trying to achieve is something like this right?
 

$insert->columns(array('mission_fk', 'ship_fk'));
$insert->values(array('mission_fk' => $row['mission_fk'], 'ship_fk' => $row['ship_fk']));

Let  me know if that helped

 

Regards

Johnny


Laurvin
  • profile picture
  • Member

Posted 04 June 2019 - 13:00 PM

I tried that first but then I get an error saying: Undefined index: mission_fk

 

Below is what I use; the big difference compared to the other use case (that now works) is that I insert a new row into another table (mission_ship) where the $insertId from the row just added to the current table (mission) is used as mission_fk in mission_ship.

 

In the below code I removed the where clause as that's not needed for an Insert, right?

class customModel extends Model {
    public function customInsert($insertId)
    {
        $sql = new Sql($this->adapter);
        $select = $sql->select()
            ->columns(['mission_fk','url'])
            ->from('squad');
        $select->where([
            'squad.squad_id = ?' => $insertId
        ]);
        $row = $this->getRowFromSelect($select, $sql);
        if ($row === null) {
            return false;
        }
        $sql = new Sql($this->adapter);
        $insert = $sql->insert('mission_ship');
        $insert->columns(array('mission_fk', 'ship_fk'));
        $insert->values(array('mission_fk' => $row['mission_fk'], 'ship_fk' => '66'));
        
        $statement = $sql->prepareStatementForSqlObject($insert);
        return $statement->execute();
    }
}

 

Hello @Laurvin,

 

I am a bit confused to be honest with your question! Usually you have one $row so the error is correct. I think what you are trying to achieve is something like this right?
 

$insert->columns(array('mission_fk', 'ship_fk'));
$insert->values(array('mission_fk' => $row['mission_fk'], 'ship_fk' => $row['ship_fk']));

Let  me know if that helped

 

Regards

Johnny

 


web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 04 June 2019 - 13:23 PM

Hello @Laurvin,

 

I think you are right! I believe the correct one should be:
 

$row[0]['mission_fk'] 

and:
 

$row[0]['ship_fk'] 

Let me know if that worked for you

 

Regards

Johnny


Laurvin
  • profile picture
  • Member

Posted 04 June 2019 - 13:28 PM

That one worked, thanks!!

 

Hello @Laurvin,

 

I think you are right! I believe the correct one should be:
 

$row[0]['mission_fk'] 

and:
 

$row[0]['ship_fk'] 

Let me know if that worked for you

 

Regards

Johnny


web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 14 June 2019 - 12:32 PM

Glad that it worked :)