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

Orm couldn't select columns

Hi all

İ write a code like that :

$user = Users::findFirst(array(
        "columns" => "id,email",
        "id = '".$this->ses['id']."'",
));

When I want to get id ($user->getid();) it returns an error :

Call to undefined method Phalcon\Mvc\Model\Row::getid() in

What is the problem ?



26.3k
Accepted
answer
edited Jul '14

You asked Phalcon to return not the whole model entity but only two columns. So I think that Phalcon is returning you only a row as an array.

Try this to look what have you received as $user:


var_dump($user);

Error says you have received an object of Phalcon\Mvc\Model\Row like here https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Row.html. It does not have magic getters/setters. Only array.

Look at the docs on this page https://docs.phalcon.io/en/latest/reference/models.html#finding-records go to the table "The available query options", row "columns". It says:

Return specific columns instead of the full columns in the model. When using this option an incomplete object is returned

So to get the id you should do this:


echo $user['id'];

Or you need to change your query to:


$user = Users::findFirst(array(
    "id = '".$this->ses['id']."'",
    ));

So that you will receive complete model entity. And then as you wanted:


$user->getId();


26.3k

Other problem is - but I am not sure here - that you are violating the security principle in terms of SQL injection. It is not a good practice. Query to be secure you should make it this way:


$user = Users::findFirst(array(
  "id = :id:",
  "bind" => array(
    "id" => $this->ses['id'],
  ),
  // to make it more secure add this:
  "bindTypes" => array(
    \Phalcon\Db\Column::BIND_PARAM_INT,
  ),
));