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

Find Firs Wrong

Hi, I'm getting the data using this method

<h1> <?php $user= Persona::findFirst("PERS_USUARIO='1724908809'" ); echo $user->pers_nombre; ?></h1>

but I get this mistake

Notice: Trying to get property 'pers_nombre' of non-object in C:\xampp\htdocs\redviva\app\views\inicio\inicio.phtml on line 10

I used this method in version 2.013 I do not know if there was a syntax change to the current version. I am doing something wrong?

edited Nov '18

Nothing has changed in the syntax, probably the problem is another, try this one that is better

$user= Persona::findFirst([
    'conditions' => 'PERS_USUARIO={id:int}',
    'bind' => [
        'id' => 1724908809
    ]
]);

if (!$user instanceof Persona) {
    throw new \Exception('User not found', 404);
}

echo $user->pers_nombre;

Good luck



81.1k

it's the result

Notice: Trying to get property 'pers_nombre' of non-object in C:\xampp\htdocs\redviva\app\views\inicio\inicio.phtml on line 10

can you share us your user model and your db conection config?



81.1k

it's the model

<?php
class Persona extends \Phalcon\Mvc\Model
{
public $PERS_CODIGO;
public $PERS_NOMBRE;
public $PERS_RUCIDE;
public $PERS_TIPOIDE;
public $PERS_TELEFONO;
public $PERS_DIRECCION;
public $PERS_UBICACION;
public $PERS_TIPO;
public $PERS_ETAPA;
public $PERS_CARGO;
public $PERS_FECHACREACION;
public $PERS_FECHACONSOLIDACION;
public $PERS_GANO;
public $PERS_SEXO;
public $PERS_ESTADC;
public $PERS_CORREO;
public $PERS_CELULAR;
public $PERS_CONTACTO;
public $PERS_PROVINCIA;
public $PERS_LINE;
public $PERS_USUARIO;
public $PERS_CONTRASENIA;
public $PERS_FOTO;

public function getSource()
{
    return 'persona';
}
public static function find($parameters = null)
{
    return parent::find($parameters);
}
public static function findFirst($parameters = null)
{
    return parent::findFirst($parameters);
}

}

it's the db.config

<?php

defined('APP_PATH') || define('APP_PATH', realpath('.'));

return new \Phalcon\Config(array(
'database' => array(
    'adapter'     => 'Mysql',
    'host'        => 'CAJA-PC',
    'username'    => 'jeiel',
    'password'    => 'jeiel',
    'dbname'      => 'redviva',
    'charset'     => 'utf8',
),
'application' => array(
    'controllersDir' => APP_PATH . '/app/controllers/',
    'modelsDir'      => APP_PATH . '/app/models/',
    'migrationsDir'  => APP_PATH . '/app/migrations/',
    'viewsDir'       => APP_PATH . '/app/views/',
    'pluginsDir'     => APP_PATH . '/app/plugins/',
    'libraryDir'     => APP_PATH . '/app/library/',
    'cacheDir'       => APP_PATH . '/app/cache/',
    'baseUri'        => '/red/',
)
));

Are you sure you have anything returned even? Just use debugger. Clearly you have false there like there is no such record in database.

all your columns name in table db are in uppercase? or are you using \PDO::CASE_UPPER?



125.7k
Accepted
answer
edited Nov '18

Just a note that practically all the code in your Persona class is unnecessary.

  • The properties of the class are automatically determined by Phalcon and made available through __get(), so you can still reference $Persona->PERS_CODIGO without declaring it as a property.
  • getSource() is unnecessary because Phalcon assumes the database table for a model is the class name. Since this is the case here, there is no need to redeclare the source
  • find() and findFirst() are simply returning the parent's return value, so they don't actually do anything. You can get rid of their declaration as well.