Cakephp using the returned array values - cakephp-2.1

I guess this should be smple, but I have been stuck at this for a while so I need help. I have a cakephp application and I am getting the following array:
chratSortedMean= array(
'totalAucFourHour' => array(
'Screening Visit-1 (scr)' => array(
'MmttFourthHour' => array(
'totalAucs' => array(
(int) 0 => (float) 0,
(int) 1 => (float) 220
)
)
),
'Month12' => array(
'MmttFourthHour' => array(
'totalAucs' => array(
(int) 0 => (float) 263.25,
(int) 1 => (float) 16.25
)
)
)
),
'totalAucTwoHour' => array(
'Day0' => array(
'MmttSecondHour' => array(
'totalAucs' => array(
(int) 0 => (float) 0,
(int) 1 => (float) 112.125
)
)
),
'DayD2' => array(
'MmttSecondHour' => array(
'totalAucs' => array(
(int) 0 => (float) 97.375,
(int) 1 => (float) 4.5
)
)
),
'Month3' => array(
'MmttSecondHour' => array(
'totalAucs' => array(
(int) 0 => (float) 8.53125,
(int) 1 => (float) 8.75
)
)
)
)
)
I want to calculate the mean of the totalAucs values for each of the timepoints(Screening Visit-1 (scr) , Month12, Day0 etc.) separately for the totalAucFourHour and totalAucTwoHour individually.
So the result i want is somewhat in the following form:
'totalAucFourHour' => array(
'Screening Visit-1 (scr)' => array(
'MmttFourthHour' => array(
'totalAucMean' => (float) 110
)
)
),
'Month12' => array(
'MmttFourthHour' => array(
'totalAucMean' => (float) 139.75
)
)
)
),
and so on. I do not want to use multiple for loops as i expect the data to grow in the future.

CakePHP has Core Utility Classes one of which is named Set. Set is the array manipulation
Class. You can call it from anywhere in your app with:
Set::methodNane();
What you need are the extract(); or classicExtract(); methods. Usage is fairly simple:
$Day0path = '/chratSortedMean/totalAucTwoHour/Day0/MmttSecondHour'
$arrayOfDay0s = Set::extract($Day0path, $data);
Check the other possibilities at the Cake Book. Also since CakePHP 2.2 there is a new array manipulation class Hash and it is faster. Since that version Set is actually deprecated but it will not be removed up until Cake 3.0.

Related

elasticsearch-php 2.0 $client->mlt($query) returns "No handler found for uri"

I tried to use elasticsearch-php 2.0 and I have been getting the error "No handler found for uri Elasticsearch/Connections/Connection.php:673'" with Elasticsearch 2.2.0.
But I wonder when I tried to call same function with Elasticsearch 1.7.5, I can get the correct result.
I didn't change my code at all, and the "$query" is completely same.
Does anyone know how can I solve the problem ?
array(
'index' => 'cat_itemnames',
'type' => 'category',
'id' => '7110',
'search_size' => (int) 20,
'percent_terms_to_match' => (float) 0.3,
'mlt_fields' => array(
(int) 0 => 'itemnames'
),
'body' => array(
'explain' => true,
'query' => array(
'more_like_this' => array(
'like_text' => 'drink'
)
)
)
)
And here is logs at Connection.php
/vendors/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php (line 672)
'No handler found for uri [/cat_itemnames/category/7110/_mlt?search_size=20&percent_terms_to_match=0.3&mlt_fields=itemnames] and method [GET]'
/vendors/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php (line 673)
array(
'transfer_stats' => array(
'url' => 'http://xxx.xxx.xxx.xxx:9200/cat_itemnames/category/7110/_mlt?search_size=20&percent_terms_to_match=0.3&mlt_fields=itemnames',
'content_type' => 'text/plain; charset=UTF-8',
'http_code' => (int) 400,
'header_size' => (int) 90,
'request_size' => (int) 273,
'filetime' => (int) -1,
'ssl_verify_result' => (int) 0,
'redirect_count' => (int) 0,
'total_time' => (float) 0.044938,
'namelookup_time' => (float) 0.000209,
'connect_time' => (float) 0.023181,
'pretransfer_time' => (float) 0.023258,
'size_upload' => (float) 110,
'size_download' => (float) 143,
'speed_download' => (float) 3182,
'speed_upload' => (float) 2447,
'download_content_length' => (float) 143,
'upload_content_length' => (float) 110,
'starttransfer_time' => (float) 0.044878,
'redirect_time' => (float) 0,
'redirect_url' => '',
'primary_ip' => 'xxx.xxx.xxx.xxx',
'certinfo' => array(),
'primary_port' => (int) 9200,
'local_ip' => '192.168.11.4',
'local_port' => (int) 49217,
'error' => '',
'errno' => (int) 0
),
'curl' => array(
'error' => '',
'errno' => (int) 0
),
'effective_url' => 'http://xxx.xxx.xxx.xxx:9200/cat_itemnames/category/7110/_mlt?search_size=20&percent_terms_to_match=0.3&mlt_fields=itemnames',
'headers' => array(
'Content-Type' => array(
(int) 0 => 'text/plain; charset=UTF-8'
),
'Content-Length' => array(
(int) 0 => '143'
)
),
'version' => '1.1',
'status' => (int) 400,
'reason' => 'Bad Request',
'body' => 'No handler found for uri [/cat_itemnames/category/7110/_mlt?search_size=20&percent_terms_to_match=0.3&mlt_fields=itemnames] and method [GET]'
)
Server -> Amazon Linux AMI 2015.09-release
Client -> Mac El Capitan 10.11.3
PHP Version - 7.0.4
ES-PHP client version - 2.0
Elasticsearch version - 1.7.5 Success
2.2.0 Fail
The More Like This API was deprecated in 1.6 and has been removed in 2.0.
As a result, you cannot call the /cat_itemnames/category/7110/_mlt endpoint anymore, which seems to be what you're doing.
You should now simply use a more_like_this query and send it to the /cat_itemnames/category/_search endpoint instead.

How to use loops with Mustache.php?

I need to loop as foreach() with my array,
$input = array (
1 => array ( 'year' => '1534', 'name' => 'test1', ),
2 => array ( 'year' => '1644', 'day' => 'test2' )
3 => array ( 'year' => '2015', 'day' => 'test3', ),
// ...
);
$m->render( $template, $input );
but can't ref without a "rooot key"... This was the first problem... Then I sulve using $input = array('list'=>$input); and ok, now list key exist to
{#list} test {/list}
but it not loops (!), it shows "test" once...
I think the problem is inside the $input array. You shouldn't use the numeric keys. So try changing the array from
$input = array (
1 => array ( 'year' => '1534', 'name' => 'test1', ),
2 => array ( 'year' => '1644', 'day' => 'test2' )
3 => array ( 'year' => '2015', 'day' => 'test3', ),
);
to
$input = array (
array ( 'year' => '1534', 'name' => 'test1', ),
array ( 'year' => '1644', 'day' => 'test2' )
array ( 'year' => '2015', 'day' => 'test3', ),
);
This is a my example, a little bit different from your code:
Mustache_Autoloader::register();
$oMustache = new Mustache_Engine( array(
'loader' => new Mustache_Loader_FilesystemLoader( 'templates' ),
));
$aVariables = array(
'list' => array(
array( 'value' => 'one' ),
array( 'value' => 'two' ),
array( 'value' => 'three' ),
)
);
$template = $oMustache->loadTemplate( 'my_template_name' );
return $template->render( $aVariables );
And this is the mustache template:
{{#list}}
test {{value}} <br/>
{{/list}}

How to select multiple check box in edit grid section of magento

http://magento.localhost.com/index.php/arithmetic/adminhtml_arithmetic/edit/id/5/key/c03c12d4c338a2e4cdbb93c3d9e511a93401d19b21a13ea77cffda20cac94577/
This is what my link looks like. I am getting all values by the ID, in the edit grid page
there is a section for multiple check boxes. How can I select all the check boxes according to the result array
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
'class' => 'required-one',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
'class' => 'required-one',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
'class' => 'required-one',
)
),
));
Thanks
Hi at the time of storing array field value we are storing as string after converting array to string,
So at the time of setValues() Magento looking for that same input field value as array to check the check boxes
Trick is that convert that stored string value into array and assign to that column field that will work
Package_Arithmetic_Block_Adminhtml_Arithmetic_Edit_Tab_Form
protected function _prepareForm()
{
$id = $this->htmlEscape(Mage::registry('arithmetic_data')->getIn_user_id());
$model = Mage::getModel("arithmeti/newuser")->load($id);
/* here is the stuff witch converting the stored string to array and set in st_user_interest */
$interest = $model->getSt_user_interest();
$model->setSt_user_interest(explode(',',$interest));
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('arithmetic_form', array('legend'=>Mage::helper('arithmetic')->__('User information')));
$fieldset-> addField('st_user_interest', 'checkboxes', array(
'label' => Mage::helper('arithmetic')->__('Interest'),
'required' => true,
'name' => 'st_user_interest[]',
'values' => array(
array(
'label' => Mage::helper('arithmetic')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('arithmetic')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('arithmetic')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('arithmetic')->__('Investment'),
)
),
));
if ( Mage::getSingleton('adminhtml/session')->getArithmeticData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getArithmeticData());
Mage::getSingleton('adminhtml/session')->setArithmeticData(null);
} elseif ( $model->getData() ) {
//Mage::registry('arithmetic_data')->getData(); /* removing this line and adding $model->getData() inslde the $form->setValues() */
$form->setValues($model->getData());
}
return parent::_prepareForm();
}

ZF2 Doctrine2 Pagination Cache

Does anybody of you know how to configure cache for zf2 pagination using doctrine2?
The way i dot it:
$cache = \Zend\Cache\StorageFactory::factory (
array (
'adapter' => array (
'name' => 'Filesystem',
'options' => array (
'ttl' => 10,
'cache_dir' =>__DIR__.'/../../../data/cache/',
),
'plugins' => array (
'serializer' => array(
'serializer' => 'igbinary'
)
)
)
) );
return $cache;
But i get Exceptions like the following.
\Zend\Cache\Storage\Adapter\Filesystem.php:1549
Message:
fwrite() expects parameter 2 to be string, object given
\Zend\Cache\Storage\Adapter\Filesystem.php:1553
Message:
Error writing file '...\data\cache\zfcache-c0\zfcache-Zend_Paginator_2_722b98872575050f0443b2b626605650.dat'
\Zend\Paginator\Paginator.php:637
Message:
Error producing an iterator
The nesting was incorrect:
$cache = \Zend\Cache\StorageFactory::factory( array(
'adapter' => array(
'name' => 'Filesystem',
'options' => array (
'ttl' => 10,
'cache_dir' =>__DIR__.'/../../../data/cache/',
),
),
'plugins' => array (
'serializer' => array(
'serializer' => 'igbinary'
)
)
) );
return $cache;

PHPUnit mocks - assert method called

i'm new to phpunit and have read the documentation on mock objects but it isn't very clear.
I am trying to write a simple test that asserts a method within a class is called. With the following code, i am testing that when the Client::exchangeArray is called, a call is made to Client::getInputFilter.
class Client implements InputFilterAwareInterface
{
public function getInputFilter() {
if(!$this->_inputFilter){
$inputFactory = new InputFactory();
$inputFilter = new InputFilter();
$inputFilter->add($inputFactory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array(
'name' => 'Int'
)
)
)));
$inputFilter->add($inputFactory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
),
array(
'name' => 'StripNewLines'
),
array(
'name' => 'Alpha'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100
)
)
)
)));
$inputFilter->add($inputFactory->createInput(array(
'name' => 'surname',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100
)
)
)
)));
$inputFilter->add($inputFactory->createInput(array(
'name' => 'email',
'required' => false,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 150
)
),
array(
'name' => 'EmailAddress'
)
)
)));
$this->_inputFilter = $inputFilter;
}
return $this->_inputFilter;
}
public function exchangeArray($data){
$inputFilter = $this->getInputFilter();
$inputFilter->setData($data);
if(!$inputFilter->isValid()){
throw new \Exception('Invalid client data');
}
$cleanValues = $inputFilter->getValues();
$this->_id = (isset($cleanValues['id']) ? $cleanValues['id'] : null);
$this->_name = (isset($cleanValues['name']) ? $cleanValues['name'] : null);
$this->_surname = (isset($cleanValues['surname']) ? $cleanValues['surname'] : null);
$this->_email = (isset($cleanValues['email']) ? $cleanValues['email'] : null);
}
}
Here is my test case:
public function testExchangeArrayCallsInputFilter(){
$data = array('id' => 54,
'name' => 'john',
'surname' => 'doe',
'email' => 'john.doe#domain.com'
);
$mock = $this->getMock('Client', array('exchangeArray'));
$mock->expects($this->once())
->method('getInputFilter');
$mock->exchangeArray($data);
}
...and i'm getting the following error:
Expectation failed for method name is equal to
when invoked 1 time(s). Method was expected to be called 1 times,
actually called 0 times.
Where am i going wrong?
It all depends on what you want test and what you want mock. Basing on the name of your test I assume that you want test exchangeArray method.
The getMock method takes as second argument names of methods that you want mock. It means that they will never be called.
So, if you want to test exchangeArray method and mock getInputFilter you should pass "getInputFilter" in second argument, like below:
$mock = $this->getMock('Client', array('getInputFilter'));
$mock->expects($this->once())
->method('getInputFilter');
$mock->exchangeArray($data);
But be careful. You didn't tell your mock to return anything, so it will return null value. That means that you'll get a fatal error on the second line of exchangeArray method (trying to call a method on a non-object). You should prepare some faked filter object to deal with that, eg:
// $preparedFilterObject = ...
$mock = $this->getMock('Client', array('getInputFilter'));
$mock->expects($this->once())
->method('getInputFilter')
->will($this->returnValue($preparedFilterObject);
$mock->exchangeArray($data);
And if you want to invoke the "real" getInputFilter method - then you just can't mock this method.

Resources