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.

Displaying relationship data from a fetched data with volt

Hello, i have some trouble on getting a data from a related table. I have checked everything but it seems all is already right according to the tutorial, so i need your help on figuring what's wrong with it.

The generated error:

    Fatal error: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::getUser() in ...(my view page line 10)...

Here's the code :

Controller:

public function indexAction()
{
  $data = BrowsingItem::find(array("order" => "dateAdded"));
  $this->view->setVar("data", $data);
}

View:

{{ content() }}

{% for data in page.items %}
    {% if loop.first %}
        <div id="h-list">


    {% endif %}
        <div id="h-item">
            Supplier : {{ data.getUser().name }}
            Item Name : {{ data.itemName }}
            Description : {{ data.desc }}
            Price : {{ "%.2f"|format(data.price) }}
            Date Added : {{ data.dateAdded }}
        </div>
    {% if loop.last %}
        <div class="btn-group">
            //Paging things
        </div>
    {% endif %}
        </div>
{% else %}
No products are recorded
{% endfor %}

Model (BrowsingItem):

class BrowsingItem extends Model{

    public $itemID;
    public $supplierID;
    public $itemName;
    public $desc;
    public $photo;
    public $price;
    public $discount;
    public $stock;
    public $dateAdded;
    public $views;
    public $liked;
    public $itemStatus;

    public function initialized(){
        $this->skipAttributes(array('itemID', 'dateAdded', 'views', 'liked', 'itemStatus'));

        $this->belongsTo("supplierID", "User", "userID");
    }
}

Model (User):

class User extends Model{
    public $userID;
    public $password;
    public $name;
    public $email;
    public $phone;
    public $city;
    public $postal;
    public $joinDate;
    public $balance;
    public $accStatus;

    public function initialize(){
        $this->skipAttributes(array('userID', 'joinDate', 'balance', 'accStatus'));

        $this->hasMany("userID", "BrowsingItem", "supplierID");
    }
}

Thanks in advance :D



30.0k

Hi,

the code in your controller is not good because you do not pass $data to the view.

use:

$this->view->setVars(array("page" => $page, 'data' => $data));



2.7k
edited May '15

Hi,

the code in your controller is not good because you do not pass $data to the view.

use:

$this->view->setVars(array("page" => $page, 'data' => $data));

Oh sorry, i did write that code before. I must have erase the wrong sentence in my snippet. Sorry about that.

I have added the generated error to the OP, maybe you can help find the bug with that info ?



30.0k

try with

{{ data.user.name }}



2.7k

try with

{{ data.user.name }}

I got these errors instead :

Notice: Access to undefined property BrowsingItem::user in ...(that code line)...

Notice: Trying to get property of non-object in ...(that code line)...


30.0k

In BrowserItem model add:

$this->belongsTo("supplierID", "User", "userID",array('alias'=>'user');



2.7k

In BrowserItem model add:

$this->belongsTo("supplierID", "User", "userID",array('alias'=>'user');

Nope, the error still there. I already suspect that either my phpmyadmin, model, or controller have some uppercase/lowercase typo but it still produce that problem...



30.0k
edited May '15

Hi,

I think this is due to the fact that you try in your view to use some pagination, but in your indexAction, yo do not set any pagination.

First try:

// in controller
$datas = BrowsingItem::find(array("order" => "dateAdded"));
$this->view->setVar("datas", $datas);

// in view

{% for data in datas %}
        <div id="h-item">
            Supplier : {{ data.user.name }}
            Item Name : {{ data.itemName }}
            Description : {{ data.desc }}
            Price : {{ "%.2f"|format(data.price) }}
            Date Added : {{ data.dateAdded }}
        </div>
{% endfor %}


2.7k
edited May '15

Hi,

I think this is due to the fact that you try in your view to use some pagination, but in your indexAction, yo do not set any pagination.

First try:

```php

// in controller $datas = BrowsingItem::find(array("order" => "dateAdded")); $this->view->setVar("datas", $datas);

// in view

{% for data in datas %} <div id="h-item"> Supplier : {{ data.user.name }} Item Name : {{ data.itemName }} Description : {{ data.desc }} Price : {{ "%.2f"|format(data.price) }} Date Added : {{ data.dateAdded }} </div> {% endfor %}

```

Nope, i already build the pagination earlier, but again, i remove it for clarity sake. I also did your recent code but still no luck. Here's the current code :

Controller:

public function indexAction()
  {
      $datas = BrowsingItem::findFirst(array("order" => "dateAdded"));
      //if ($this->request->isPost()){
         // echo 'temp';
     // }else{
         // $page = $this->request->getQuery("page", "int");
         // if ($page <= 0) {
           //   $page = 1;
         // }
     // }

     // $paginator = new PaginatorModel(array(
        //  "data" => $data,
        //  "limit" => 10,
        //  "page" => $page
    //  ));

     // $page = $paginator->getPaginate();

      //$this->view->setVar("page", $page);
      $this->view->setVar("datas", $datas);
  }

View:

{{ content() }}

{% for data in datas %}

    {% if loop.first %}
        <div class="h-list">


    {% endif %}
        <div class="h-item">
            Supplier : {{ data.getUser().name }}<br/>
            Item Name : {{ data.itemName }}<br/>
            Description : {{ data.desc }}<br/>
            Price : {{ "%.2f"|format(data.price) }}<br/>
            Date Added : {{ data.dateAdded }}<br/><br/>
        </div>
    {% if loop.last %}
            //paging
    {% endif %}
        </div>
{% else %}
No products are recorded
{% endfor %}

Result page:

Supplier : 
Fatal error: Call to a member function getUser() on string in C:\xampp\htdocs\wantderland\cache\volt\c%%%%xampp%%htdocs%%wantderland%%app%%views%%hunting%%index.phtml.php on line 11

Btw, thanks for spending your time replying :)

EDIT :

After several trying, i suspect of two things :

  1. I insert the data not through the system but directly through phpmyadmin. Does this make phalcon cannot recognize the datas ?

  2. I try to call the related table in controller with $res = $datas->getUser(); echo $res->name; but it returned the error Notice: Trying to get property of non-object in C:\xampp\htdocs\wantderland\app\controllers\HuntingController.php on line 11. Does this mean the getUser() is working but the name is not ?



30.0k

{{ data.user.name }}

// with $this->belongsTo("supplierID", "User", "userID",array('alias'=>'user'); in BrowserItem model

do var_dump(BrowsingItem::findFirst(array("order" => "dateAdded"))); return a valid model collection result ?



30.0k

You can't use $res = $datas->getUser();

because $datas is a resultset of BrowserItem objects, thats's why you get "Notice: Trying to get property of non-object"

you have to cycle through the resultset

foreach( $datas as $data ){ 
  var_dump($data->getUser()); // or $data->user;
}


2.7k
edited May '15

You can't use $res = $datas->getUser();

because $datas is a resultset of BrowserItem objects, thats's why you get "Notice: Trying to get property of non-object"

you have to cycle through the resultset

```php

foreach( $datas as $data ){ var_dump($data->getUser()); // or $data->user; }

```

I tried this and got Fatal error: Call to a member function getUser() on string in C:\xampp\htdocs\wantderland\app\controllers\HuntingController.php on line 12

When i use vardump($data->user); i got this instead :Notice: Trying to get property of non-object in C:\xampp\htdocs\wantderland\app\controllers\HuntingController.php on line 12 followed by NULL, Repeating for 13 times

Oh, and when i tried the vardump of the findFirst, it just produced a page-whole text, but with a lot of NULL value.



30.0k

so, I believe that you get an empty object resultset with BrowsingItem::findFirst(array("order" => "dateAdded"));



2.7k
edited May '15

so, I believe that you get an empty object resultset with BrowsingItem::findFirst(array("order" => "dateAdded"));

Okay....That's weird, i already inserted the data in the phpmyadmin and my previous codes never have problem with this...Do you know what cause this problem ?

Oh, when i dump just Browsing Item, without trying to connect to other table it works fine. It just when i try to connect it the error triggers...



2.7k
Accepted
answer

I found the error....I accidentally wrote 'initalized' instead of 'initialize'.....Kill Me....