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

Error - Query

Hello,

I have a little problem with my code when I execute a Query.

Really I don't understand why show this error:

Can't obtain the model 'nombre' source from the _models list, when preparing: UPDATE institucion SET nombre = :InstitucionName:, rif = :InstitucionRif: WHERE id = :InstitucionId:

I am using this model:

<?php 
use Phalcon\Mvc\Model;

class Institucion extends Phalcon\Mvc\Model
{
    public $id_institucion;
    public $rif;
    public $nombre;
}
?>

My Controller is here:

    public function Editar2Action($id)
    {

        if ($id == 'true') 
        {
            if ($this->request->isPost() == true) {
            // Form Variables - It's ok
            $InstitucionName = $this->request->getPost("nombre", "string");
            $InstitucionRif = $this->request->getPost("rif", "string");
            $InstitucionId = $this->request->getPost("id", "int");

            $this->modelsManager->executeQuery("UPDATE institucion SET nombre = :InstitucionName:, rif = :InstitucionRif: WHERE id = :InstitucionId:", array('InstitucionName' => $InstitucionName, 'InstitucionRif' => $InstitucionRif, 'InstitucionId' => $InstitucionId));
            }   
        }

    }

Anybody can help me?, Thanks



1.7k

From your model structure I think that it should be

WHERE id_institucion = :InstitucionId:

in your query

Dzung you are right, it was a little mistake, so I have error yet :/

Can't obtain the model 'nombre' source from the _models list, when preparing: UPDATE institucion SET nombre = :InstitucionName:, rif = :InstitucionRif: WHERE id_institucion = :InstitucionId:

Thien,

I modified my controller for your suggestion, but the result is same.

$this->modelsManager->executeQuery("UPDATE institucion SET nombre = ?0 , rif = ?1 WHERE id_institucion = ?2", array(0 => $InstitucionName, 1 => $InstitucionRif, 2 => $InstitucionId));
Can't obtain the model 'nombre' source from the _models list, when preparing: UPDATE institucion SET nombre = ?0 , rif = ?1 WHERE id_institucion = ?2


1.7k

can you show your database fields for institucion table? is there a field name nombre in the database table?

Dzung you are right, it was a little mistake, so I have error yet :/

Can't obtain the model 'nombre' source from the _models list, when preparing: UPDATE institucion SET nombre = :InstitucionName:, rif = :InstitucionRif: WHERE id_institucion = :InstitucionId:


58.4k
edited Feb '15

Hey

Do you have use namesapce in autoloader, if you have replace institucion to like example below

    $phql = "UPDATE Zphalcon\Models\Job SET Zphalcon\Models\Job.status = ?0 WHERE Zphalcon\Models\Job.id = ?1";
                        $result = $this->modelsManager->executeQuery($phql, array(
                            0 => Job::STATUS_DISPLAY,
                            1 => $params['id']
                        ));

In my experience you need to specify Models in your PHQL query, not table names.

So I suggest trying "UPDATE \Your\Model\Namespace\Institution...."



125.8k
Accepted
answer

Why are you writing the query itself? Using the ORM is easier:

public function Editar2Action($id)
    {

        if ($id == 'true') 
        {
            if ($this->request->isPost() == true) {
            // Form Variables - It's ok
            $InstitucionName = $this->request->getPost("nombre", "string");
            $InstitucionRif = $this->request->getPost("rif", "string");
            $InstitucionId = $this->request->getPost("id", "int");

            $Institucion = Institucion::findFirst($InstitucionId);
            $Institucion->save([
                'nombre'=>$InstitucionName,
                'rif'=>$InstitucionRif
            ]);
            }   
        }

    }

Anyway, to answer your question: Whenever I've encountered this error in the past, it means that Phalcon, when converting the query to SQL, was unable to determine what model you were referencing.

In your query: UPDATE institucion SET nombre, "institucion" refers to a model, not a table name. So, I wonder if the problem is simply that it needs to be capitalized - "Institucion"?

Yes, the names is fine. My error was partly solved. Thanks

can you show your database fields for institucion table? is there a field name nombre in the database table?

Dzung you are right, it was a little mistake, so I have error yet :/

Can't obtain the model 'nombre' source from the _models list, when preparing: UPDATE institucion SET nombre = :InstitucionName:, rif = :InstitucionRif: WHERE id_institucion = :InstitucionId:

You solved my error partly (because I could update data with your method), thanks so much Quasipickle.

But I want know why my model don't work to update data. You sayed: "I wonder if the problem is simply that it needs to be capitalized - "Institucion"?". How can I make it? and If I make it the query will work perfectly?

For example:

$this->modelsManager->executeQuery("UPDATE institucion SET nombre = :InstitucionName:, rif = :InstitucionRif: WHERE id_institucion = :InstitucionId:", array('InstitucionName' => $InstitucionName, 'InstitucionRif' => $InstitucionRif, 'InstitucionId' => $InstitucionId));

Because in the documentation: https://docs.phalcon.io/en/latest/reference/phql.html#updating-data

Refers directly the model "cars" to update.

Why are you writing the query itself? Using the ORM is easier:

public function Editar2Action($id)
   {

       if ($id == 'true') 
       {
           if ($this->request->isPost() == true) {
           // Form Variables - It's ok
           $InstitucionName = $this->request->getPost("nombre", "string");
           $InstitucionRif = $this->request->getPost("rif", "string");
           $InstitucionId = $this->request->getPost("id", "int");

          $Institucion = Institucion::findFirst($InstitucionId);
          $Institucion->save([
              'nombre'=>$InstitucionName,
              'rif'=>$InstitucionRif
          ]);
           }   
       }

   }

Anyway, to answer your question: Whenever I've encountered this error in the past, it means that Phalcon, when converting the query to SQL, was unable to determine what model you were referencing.

In your query: UPDATE institucion SET nombre, "institucion" refers to a model, not a table name. So, I wonder if the problem is simply that it needs to be capitalized - "Institucion"?

Because in the documentation: https://docs.phalcon.io/en/latest/reference/phql.html#updating-data

Refers directly the model "cars" to update.

Actually, it refers to "Cars", not "cars", which I'm guessing might be an important distinction.

Hi Datanator, thanks for suggestion,

I understand you, but a little, because I don't know what you refer exactly with:

Your = Main Carpet? Namespace = ? Model = Main Carpet of models?

I think which is a route of model " \Your\Model\Namespace\Institution". Am I correct?

In my experience you need to specify Models in your PHQL query, not table names.

So I suggest trying "UPDATE \Your\Model\Namespace\Institution...."

edited Feb '15

Basically I am saying the same thing as quasipickle.

UPDATE institucion SET nombre ...

is wrong

You should write

UPDATE Institucion SET nombre...

(note the capital I)

The name of your model is "Institution" (not "instucion").

If you use namespaces for your model you need to include the namespace in your phql:

<?php 

namespace Models

class Institucion extends \Phalcon\Mvc\Model
{
}
?>

would require

UPDATE \Models\Institucion SET nombre...
UPDATE \Models\Institucion SET nombre...

Except drop the leading \ . That causes the same error for some reason.

@quasipickle: that is weird. Leading slash works great for me and even is required when the calling method is in a different namespace...

Hmmm, well then I guess there's more to it than just "Leave it off" or "Must have it".