Jump to content


Laurvin

Member Since 25 May 2019
Offline Last Active Jun 04 2019 01:26 PM
-----

Posts I've Made

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

04 June 2019 - 01: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


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

04 June 2019 - 01: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

 


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

29 May 2019 - 06: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....387916c8beb60f6

 

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.

 


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

25 May 2019 - 02: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.