Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 ?



17.1k
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 http://docs.phalconphp.com/en/latest/api/Phalcon%5Mvc%5Model%5Row.html. It does not have magic getters/setters. Only array.

Look at the docs on this page http://docs.phalconphp.com/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();


17.1k

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,
  ),
));