We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Custom query in model

In my model I have a "afterCreate()". Within "afterCreate()" I would like to execute a simple custom query that will insert a record into another table with the foreign key of the record just created.

If you could provide an example of how to do this with a custom query (ie no association to model) and also using Phalcon to save (ie there is an association).

Thank you in advance for the help.



58.4k

Hey man

You can use modelManager in Phalcon

     $phql = "UPDATE Phalcontip\ModelsJob SET Phalcontip\Models\Job.status = ?0 WHERE Phalcontip\Models\Job.id = ?1";
            $result = $this->modelsManager->executeQuery($phql, array(
                0 => Job::STATUS_DISPLAY,
                1 => $idJob
            ));


621

I wound up running this and it worked.

public function afterCreate()
{
    //Save 
    $data = new Motors();
    $data->save(array(
        "car_id" => $this->id,
        "identifier" => 1
    ));

}


621

Thiện I will look into your response. Thank You!