0

I have this array of elements to be printed as a chart:

array(7) {
  [0]=>
  array(2) {
    [0]=>
    array(0) {
    }
    [1]=>
    string(22) "/app_dev.php/users/new"
  }
  [1]=>
  array(2) {
    [0]=>
    array(0) {
    }
    [1]=>
    string(19) "/app_dev.php/users/"
  }



  [2]=>
  array(2) {
[0]=>
    array(1) {
      [0]=>
      object(BTI\RepSysBundle\Objects\gapiReportEntry)#359 (2) {
        ["metrics":"BTI\RepSysBundle\Objects\gapiReportEntry":private]=>
        array(1) {
          ["pageviews"]=>
          int(54)
        }
        ["dimensions":"BTI\RepSysBundle\Objects\gapiReportEntry":private]=>
        array(1) {
          ["dimension1"]=>
          string(14) "rainercedric23"
        }
      }
    }
    [1]=>
    string(30) "/app_local.php/admin/analytics"
  }
  [3]=>
  array(2) {
    [0]=>
    array(0) {
    }
    [1]=>
    string(16) "/admin/analytics"
  }
}

The problem is I can access the string values like the "/app_dev.php/users/new" by this code

{{ foo[0].1}}

But I can't seem to access the object with metrics and pageviews I am trying this one

{{ foo[2].0.0.metrics.pageviews}}

But it doesn't work, anyone have an idea to access an object element? I have this service:

<?php

namespace BTI\RepSysBundle\Services;

use BTI\RepSysBundle\Objects\Gapi;

class GapiManager {
public function GAPIGetter() {
    $Gapi = new Gapi('[email protected]', 'somepassword');
    $path = array("/app_dev.php/users/new",
        "/app_dev.php/users/",
        "/app_local.php/admin/analytics",
        "/admin/analytics",
        "/app_dev.php/account/",
        "/app_dev.php",
        "/app_dev.php/account/new"
    );

    foreach ($path as $filterpath) {
        $filters[] = "ga:pagePath==" . $filterpath;
    }
    $ctr = 0;
    foreach ($filters as $filter) {
        $Gapisquery[] = array($Gapi->requestReportData('81757262', array('dimension1'), array('pageviews'), 'pageviews', $filter), $path[$ctr]);
        $ctr++;
    }



    return array_filter($Gapisquery);
}

}

basically this service request the report data from the Google Analytics and returns the page views from each url path mentioned. the problem is that it returns a private object from the Gapi class.

1 Answer 1

1

Your metrics property is private. You have to write getter for this property like:

// file BTI\RepSysBundle\Objects\gapiReportEntry.php
namespace BTI\RepSysBundle\Objects;

class gapiReportEntry{
    // other code
    public function getMetrics{
       return $this->metrics;
    }
}

After that you have access in twig via object.metrics.

Please, read documentation about twig Twig Variables.

Sign up to request clarification or add additional context in comments.

8 Comments

I am really confused right there, I've just used it before and accessed it while it is private and draw a chart with that but the moment I change something it can't be accessed.
I already have these codes in my object public function getDimesions() { return $this->dimensions; } same with metrics.
So, you have to write getters for metrics property. I updated my answe, added link for documentation, please read it.
I am sorry but I've forgot to say that I have a service that gets the array mentioned above and that service calls the objects. I've already set the getters for the metrics but still getting the property private in the returned array from the service.
It gives an error "Variable "metrics" does not exist."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.