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

Notice: Access to undefined property

Hello

I have a little problem. I don't know why show this error/notice in my view.

Notice: Access to undefined property Institucion::name

I am trying show the data from my view (I am working with .volt template).

<?php $institucion = institucion::find(); foreach ($institucion as $obj) { echo $obj->name; } ?>

I have a model called Institucion. My autolader and services files It's good, because I have conection with database and I can insert data on it.

Really I am begginer and I don't know what is the best practices to show data from database in a view.



2.1k
public function viewAction()
{
    $institucion = institucion::find(); 
    $this->view->institucion = $institucion;
}
{% for inst in institucion %}
    {{ inst.name}}
{% endfor %}

Thanks! but It's not working.

Error: Notice: Access to undefined property Institucion::name.

<?php
class InstitucionController extends \Phalcon\Mvc\Controller
{
    public function ListarAction()
    {   
        $institucion = institucion::find();
        $this->view->institucion = $institucion;
    }
}
?>
<html>
<head>
<title>List Institucions</title>
</head>
<body>
{% for inst in institucion %}
    {{ inst.name}}
{% endfor %}
</body>
</html>

View Name = Listar.volt

I access for this URL = https://localhost/system/institucion/listar



43.9k

Hi,

please check that

$institucion = institucion::find();

doesn't return an empty object

edited Jan '15

Hi le51,

The variable is not empty because I use a var_dump($institucion) in this View (listar.volt) and it devolve a array. I show here:


object(Phalcon\Mvc\Model\Resultset\Simple)[51]
  protected '_type' => int 0
  protected '_result' => 
    object(Phalcon\Db\Result\Pdo)[52]
      protected '_connection' => 
        object(Phalcon\Db\Adapter\Pdo\Postgresql)[46]
          protected '_eventsManager' => null
          protected '_descriptor' => 
            array (size=4)
              ...
          protected '_dialect' => 
            object(Phalcon\Db\Dialect\Postgresql)[48]
              ...
          protected '_connectionId' => int 1
          protected '_sqlStatement' => null
          protected '_sqlVariables' => null
          protected '_sqlBindTypes' => null
          protected '_transactionsWithSavepoints' => int 0
          protected '_pdo' => 
            object(PDO)[47]
              ...
          protected '_affectedRows' => null
          protected '_transactionLevel' => int 0
          protected '_type' => string 'pgsql' (length=5)
          protected '_dialectType' => string 'postgresql' (length=10)
      protected '_result' => null
      protected '_fetchMode' => int 2
      protected '_pdoStatement' => 
        object(PDOStatement)[53]
          public 'queryString' => string 'SELECT "institucion"."id_institucion", "institucion"."rif", "institucion"."nombre", "institucion"."tipo" FROM "institucion"' (length=123)
      protected '_sqlStatement' => string 'SELECT "institucion"."id_institucion", "institucion"."rif", "institucion"."nombre", "institucion"."tipo" FROM "institucion"' (length=123)
      protected '_bindParams' => null
      protected '_bindTypes' => null
      protected '_rowCount' => int 2
  protected '_cache' => null
  protected '_isFresh' => boolean true
  protected '_pointer' => int -1
  protected '_count' => int 2
  protected '_activeRow' => null
  protected '_rows' => null
  protected '_errorMessages' => null
  protected '_hydrateMode' => int 0
  protected '_model' => 
    object(Institucion)[45]
      public 'id_institucion' => null
      public 'rif' => null
      public 'nombre' => null
      protected '_dependencyInjector' => 
        object(Phalcon\DI\FactoryDefault)[5]
          public '_services' => 
            array (size=24)
              ...
          public '_sharedInstances' => 
            array (size=8)
              ...
          public '_freshInstance' => boolean true
      protected '_modelsManager' => 
        object(Phalcon\Mvc\Model\Manager)[43]
          protected '_dependencyInjector' => 
            object(Phalcon\DI\FactoryDefault)[5]
              ...
          protected '_eventsManager' => null
          protected '_customEventsManager' => null
          protected '_readConnectionServices' => null
          protected '_writeConnectionServices' => null
          protected '_aliases' => null
          protected '_hasMany' => null
          protected '_hasManySingle' => null
          protected '_hasOne' => null
          protected '_hasOneSingle' => null
          protected '_belongsTo' => null
          protected '_belongsToSingle' => null
          protected '_hasManyToMany' => null
          protected '_hasManyToManySingle' => null
          protected '_initialized' => 
            array (size=1)
              ...
          protected '_sources' => 
            array (size=1)
              ...
          protected '_schemas' => null
          protected '_behaviors' => null
          protected '_lastInitialized' => 
            &object(Institucion)[45]
          protected '_lastQuery' => null
          protected '_reusable' => null
          protected '_keepSnapshots' => null
          protected '_dynamicUpdate' => null
          protected '_namespaceAliases' => null
      protected '_modelsMetaData' => null
      protected '_errorMessages' => null
      protected '_operationMade' => int 0
      protected '_dirtyState' => int 1
      protected '_transaction' => null
      protected '_uniqueKey' => null
      protected '_uniqueParams' => null
      protected '_uniqueTypes' => null
      protected '_skipped' => null
      protected '_related' => null
      protected '_snapshot' => null
  protected '_columnMap' => null
  protected '_keepSnapshots' => boolean false

So, If I detele this view (listar.volt) and print variable in the controller (InstitucionController.php) on this way, it's all right:


$institucion = institucion::find(); 

foreach ($institucion as $obj) {
    echo $obj->nombre." - ".$obj->rif."<br>";
}

But I need print variable in the view not in controller. I think which problem can be in how pass variable to the "View".

Error: Notice: Access to undefined property Institucion::name in C:\wamp\www\sistema_vit\cache\volt\c%%%%wamp%%www%%sistema_vit%%app%%views%%institucion%%listar.volt.php on line 23



43.9k
Accepted
answer
edited Jan '15

Hi,

the way you pass variable to the view is all right. But in your view, you are using institucion.name

and the object dump show :

"institucion"."nombre"

Thanks so Much Le51 ! ! !

Ofcourse my field in database is called nombre and not name.