PHP Fatal error, trying to request method inside model multiple times - php

The error message
[23-Mar-2010 08:36:16] PHP Fatal
error: Cannot redeclare humanize()
(previously declared in
/Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php:133)
in
/Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php
on line 133
I have a "Filer" model which contains several methods to generate graphs. Each method in there related to generating graphs has the suffix "Graph" in the method name. As we have some performance issues, I try to render the graphs in advance (using cron) instead of rendering them on each request. The code below is what I came up with:
public function generategraphsAction()
{
$this->_helper->viewRenderer->setNoRender();
$config = Zend_Registry::get('config');
$id = $this->_getParam('filerid');
$filer = new Filer($id);
$filer_methods = get_class_methods($filer);
foreach ($filer_methods as $filer_method) {
if (preg_match('/^(.*)Graph$/i', $filer_method, $matches)) {
$path = $config->imaging_caching_dir . "/$id/{$matches[1]}.png";
$filer->$matches[0]($path);
}
}
// var_dump(get_class_methods($filer)); die;
}
The result from the var_dump(), when uncommented, is:
array
0 => string '__construct' (length=11)
1 => string 'find_by_name' (length=12)
2 => string 'getPartner' (length=10)
3 => string 'getSlots' (length=8)
4 => string 'getGroups' (length=9)
5 => string 'grouplist' (length=9)
6 => string 'getAggregates' (length=13)
7 => string 'getVolumes' (length=10)
8 => string 'getAggregateVolumes' (length=19)
9 => string 'getShelves' (length=10)
10 => string 'getAutoSupportHistory' (length=21)
11 => string 'getAutoSupportMail' (length=18)
12 => string 'getOrphans' (length=10)
13 => string 'getAll' (length=6)
14 => string 'getDiskRevOverview' (length=18)
15 => string 'getDiskTypeOverview' (length=19)
16 => string 'getDiskTypeSizeFunctionOverview' (length=31)
17 => string 'getLicenses' (length=11)
18 => string 'removeGroup' (length=11)
19 => string 'addGroup' (length=8)
20 => string 'hasGroup' (length=8)
21 => string 'aggdefaultGraph' (length=15)
22 => string 'aggbarGraph' (length=11)
23 => string 'voldefaultGraph' (length=15)
24 => string 'volbarGraph' (length=11)
25 => string 'replicationGraph' (length=16)
26 => string 'getReplicationData' (length=18)
27 => string 'humanize' (length=8)
28 => string 'getFiler' (length=8)
29 => string 'getOptions' (length=10)
30 => string 'getCifsInfo' (length=11)
31 => string 'getCifsStats' (length=12)
32 => string '__get' (length=5)
33 => string 'tr' (length=2)
34 => string 'trs' (length=3)
35 => string 'fieldList' (length=9)
The generategraphsAction() method finds the 'Graph' methods correctly:
array
0 => string 'aggdefaultGraph' (length=15)
1 => string 'aggdefault' (length=10)
array
0 => string 'aggbarGraph' (length=11)
1 => string 'aggbar' (length=6)
array
0 => string 'voldefaultGraph' (length=15)
1 => string 'voldefault' (length=10)
array
0 => string 'volbarGraph' (length=11)
1 => string 'volbar' (length=6)
array
0 => string 'replicationGraph' (length=16)
1 => string 'replication' (length=11)
However when the first graph is generated, it generates the above listed PHP fatal error. Anyone can come up with a solution to this? I tried to pass by reference or switch a few things around (like re declare the Filer model, $current_filer = new Filer($id); and unset() it again after the request, but resulted in the same error) without much success.
The referenced method "humanize" isn't used for anything I'm doing at the moment, but belongs to the Model because it's used in several other places. Of course, removing the method is not really an option right now, and the model contains several other methods as well so I assume if I just move the humanize method around, it will generate an error on the next one.
For reference, the humanize() method:
public function humanize ($kbytes, $unit = null) {
// KiloByte, Megabyte, GigaByte, TeraByte, PetaByte, ExaByte, ZettaByte, YottaByte
$units = array('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if (null !== $units) {
$i = array_search(substr($unit, -2), $units);
if (! $i) {
$i = floor((strlen($kbytes) - 1) / 3);
}
} else {
$i = floor((strlen($kbytes) - 1) / 3);
}
$newSize = round($kbytes / pow(1024, $i), 2);
return $newSize . $units[$i];
}
Thanks in advance for the help offered.

I expect your parenthesise are wrong and your function humanize declaration is inside a while loop hence the redeclaration.
Unless of course you are including the file that defines this function twice somewhere?

public static function sums ($aggregates) {
function humanize(&$item, $key) {
$item = Filer::humanize($item);
}
$sums = array('size_total' => 0, 'size_usable' => 0, 'size_snapshot_reserve' => 0,
'size_snapshot_used' => 0, 'size_snapshot_free' => 0,
'size_active_fs_used' => 0, 'size_active_fs_free' => 0,
'size_active_fs_reserved' => 0);
foreach ($aggregates as $aggregate) {
if ($aggregate->state !== 'online') continue;
$sums['size_total'] += $aggregate->size_total;
$sums['size_usable'] += $aggregate->size_usable;
$sums['size_snapshot_reserve'] += $aggregate->size_snapshot_reserve;
$sums['size_snapshot_used'] += $aggregate->size_snapshot_used;
$sums['size_snapshot_free'] += $aggregate->size_snapshot_free;
$sums['size_active_fs_used'] += $aggregate->size_active_fs_used;
$sums['size_active_fs_free'] += $aggregate->size_active_fs_free;
$sums['size_active_fs_reserved'] += $aggregate->size_active_fs_reserved;
}
$humanSums = $sums;
array_walk($humanSums, 'humanize');
return array($sums, $humanSums);
}
The function inside function is the culprit.

Problem fixed.
Adding if(!function_exists('humanize')) block around the method declaration solved it.

Related

MYSQL PHP Array grouping

Having trouble wrapping my head around this conceptually. Still new to this. Basically I have this return from my database :
array (size=456)
0 =>
object(stdClass)[358]
public 'id' => string '2432' (length=4)
public 'symbol' => string '.AMLP' (length=14)
public 'last' => string '0.01' (length=4)
public 'volume' => string '3690' (length=4)
public 'the_date' => string '2019-09-13' (length=10)
public 'the_screener' => string '1' (length=1)
public 'notes' => string 'notes here' (length=149)
1 =>
object(stdClass)[726]
public 'id' => string '2417' (length=4)
public 'symbol' => string '.ARCC' (length=14)
public 'last' => string '2.25' (length=4)
public 'volume' => string '1633' (length=4)
public 'the_date' => string '2019-09-13' (length=10)
public 'the_screener' => string '1' (length=1)
public 'notes' => string 'notes' (length=60)
2 =>
object(stdClass)[726]
public 'id' => string '2447' (length=4)
public 'symbol' => string '.ARCC' (length=14)
public 'last' => string '2.25' (length=4)
public 'volume' => string '1633' (length=4)
public 'the_date' => string '2019-09-12' (length=10)
public 'the_screener' => string '1' (length=1)
public 'notes' => string 'notes here 3' (length=60)
3 =>
What I'm trying to do with PHP is create an object/array that I can work with that displays these items like
AMLP 1 found on dates 2019-09-13
ARCC 2 found on dates 2019-09-13, 2019-09-12
In the end I would display these in a table, but conceptually this is what I'm trying to do.
I've tried creating an array in my foreach I use to display this information in a table, but I was thinking about it and it's probably better to just use the same query data and break it down separately.
So I'd like to create an array like :
Array
(
[1] => Array
(
[id] => 1
[symbol] => ARCC
[dates] => Array
(
[3] => Array
(
2019-09-13
2019-09-12
)
)
)
)
Consider your array is $arrDates. If you want to access properties like object properties
$arrFinal = [];
foreach ($arrDates as $intKey => $obj){
$strSymbol = getSubSymbol($obj->symbol);
if(!isset($arrFinal[$strSymbol])) {
$arrFinal[$strSymbol] = [ 'id' => $obj->id, 'symbol' => $obj->symbol];
}
$arrFinal[$strSymbol]['dates'][] = $obj->the_date;
}
// Now loop throuh arrFinal and do print the statements you want.
foreach($arrFinal as $strSubSymbol => $arrData){
echo $strSubSymbol . ' '. count($arrData['dates']) . ' found on dates ' . implode(',', $arrData['dates']). PHP_EOL;
}
Function to get the desired subpart of symbol
function getSubSymbol($symbol_original){
$symbol = preg_split('/(?=\d)/', $symbol_original, 2); //get everything up until first number or the date in the string in this case.
$symbol_here = substr($symbol[0], 1);
return $symbol_here;
}

SQL Select inside Select doesn't accept WHERE x LIKE %y%

I want this code to accept more than one WHERE parameter at the bottom, but when the added text comes into play, it returns nothing. It returns what it should without the filter.
What I'm trying to do is filter the results I get from the inner query from user inputted data that gets posted into the PHP code.
Any advice would be helpful at this point.
$OgrenciNo= " AND OgrenciNo LIKE %1005%";
$sth = $conn->prepare('SELECT t.* FROM (SELECT ogrenciler.OgrenciNo, ogrenciler.adsoyad as adsoyad1,
dersler.Ders, dersler.DersKodu, tarih.Tarih, ogretmenler.isim,
login.adsoyad, onayturu.Onay, mazeretturu.tur,
mazeretaciklama.Aciklama, mazeretaciklama.LogTarihi
FROM ogrenciler, tarih, aratablo, ogretmenler,
mazeretaciklama, login, dersler, mazeretturu, onayturu
WHERE aratablo.AciklamaID = mazeretaciklama.AciklamaID
AND aratablo.TarihID = tarih.TarihID
AND aratablo.DersID = dersler.DersID
AND aratablo.OnaylayanID = login.OnaylayanID
AND aratablo.OnayID = onayturu.OnayID
AND aratablo.Mazturu = mazeretturu.Mazturu
AND dersler.OgrGorID = ogretmenler.OgrGorID
AND mazeretaciklama.OgrenciNo = ogrenciler.OgrenciNo) t
WHERE LogTarihi > "1900-01-01"
'.$OgrenciNo
);
$sth->execute();
Normal results are as follows:
array (size=2)
0 =>
array (size=11)
'OgrenciNo' => string '1005.02021' (length=10)
'adsoyad1' => string 'Örnek Öğrenci' (length=13)
'Ders' => string 'Ders Adı' (length=8)
'DersKodu' => string 'KOD000' (length=6)
'Tarih' => string '2016-02-01' (length=10)
'isim' => string 'Öğretim Elemanı İsmi' (length=20)
'adsoyad' => string 'ROOT' (length=4)
'Onay' => string 'Onaylandı' (length=9)
'tur' => string 'Sınav' (length=5)
'Aciklama' => string 'hastalık hastası' (length=16)
'LogTarihi' => string '2016-02-09 09:14:52' (length=19)
1 =>
array (size=11)
'OgrenciNo' => string '1035.02021' (length=10)
'adsoyad1' => string 'ahmet mehmet' (length=12)
'Ders' => string 'E-Ticaret Sistem Tasarımı' (length=25)
'DersKodu' => string 'BIL446' (length=6)
'Tarih' => string '2016-02-07' (length=10)
'isim' => string 'Yrd. Doç. Dr. Mustafa Cem Kasapbaşı' (length=35)
'adsoyad' => string 'Araş. Gör. Erdem Yavuz' (length=22)
'Onay' => string 'Onaylanmadı' (length=11)
'tur' => string 'Ders' (length=4)
'Aciklama' => string 'cenaze vardı' (length=12)
'LogTarihi' => string '2016-02-11 13:33:53' (length=19)
I'm trying to filter out the second result but the LIKE argument doesn't seem to work.
By popular demand here's the whole query after I print it out in the page (P.S. the only thing different from the top one is that it contains "AND OgrenciNo LIKE %1005%" at the end):
SELECT t.* FROM (SELECT ogrenciler.OgrenciNo, ogrenciler.adsoyad as adsoyad1, dersler.Ders, dersler.DersKodu, tarih.Tarih, ogretmenler.isim, login.adsoyad, onayturu.Onay, mazeretturu.tur, mazeretaciklama.Aciklama, mazeretaciklama.LogTarihi FROM ogrenciler, tarih, aratablo, ogretmenler, mazeretaciklama, login, dersler, mazeretturu, onayturu WHERE aratablo.AciklamaID = mazeretaciklama.AciklamaID AND aratablo.TarihID = tarih.TarihID AND aratablo.DersID = dersler.DersID AND aratablo.OnaylayanID = login.OnaylayanID AND aratablo.OnayID = onayturu.OnayID AND aratablo.Mazturu = mazeretturu.Mazturu AND dersler.OgrGorID = ogretmenler.OgrGorID AND mazeretaciklama.OgrenciNo = ogrenciler.OgrenciNo) t WHERE LogTarihi > "1900-01-01" AND OgrenciNo LIKE %1005%
Your like condition should be like this. Just add ' in it. :
$OgrenciNo= " AND OgrenciNo LIKE '%1005%'";

Building a form dynamically with Codeigniter and Active Record queries

Model
function get_prices()
{
$table_by_product = 'printer_businesscards'; //replace with URI Segment
//Get all the columns in the table set by the page URI of $table_by_product variable
$get_all_col_names = $this->db->list_fields($table_by_product);
//Loop through the column names. All names starting with 'O_' are optional fields for the
//current product. Get all Distinct values and create a radio button list in form.
foreach ($get_all_col_names as $key => $value) {
//get all o_types for the product by column name
if ($all_O_types = preg_match('/O_/', $value))
{
$O_types = array($value);
foreach ($O_types as $O) {
//echo $O;
$this->db->select($O);
$this->db->distinct();
$qO = $this->db->get($table_by_product);
$qO_Array = $qO->result_object();
}
}
//Get all x_types for the product by column name. All 'X_' types are specific product options.
//Create a dropdown menu with all DISTINCT product options.
if ($all_X_types = preg_match('/X_/', $value))
{
$X_types = array($value);
foreach ($X_types as $X) {
//echo $X;
$this->db->select($X);
$this->db->distinct();
$qX = $this->db->get($table_by_product);
$qX_Array = $qX->result_object();
}
}
}
return array($qX_Array,$qO_Array);
}
}
So each product has different options but all products options are prefixed by "X_" or "O_". I need to get the DISTINCT values of each COLUMN of "X_" and "O_" and in any VIEW I need to build a form with these values. Here is a look at the array:
array (size=3)
0 =>
object(stdClass)[19]
public 'X_SIZE' => string '1.75x3' (length=6)
1 =>
object(stdClass)[20]
public 'X_SIZE' => string '1.75x3.5(slim)' (length=14)
2 =>
object(stdClass)[21]
public 'X_SIZE' => string '2x3' (length=3)
array (size=3)
0 =>
object(stdClass)[17]
public 'X_PAPER' => string '14ptGlossCoatedCoverwithUV(C2S)' (length=31)
1 =>
object(stdClass)[18]
public 'X_PAPER' => string '14ptPremiumUncoatedCover' (length=24)
2 =>
object(stdClass)[24]
public 'X_PAPER' => string '16ptDullCoverwithMatteFinish' (length=28)
array (size=2)
0 =>
object(stdClass)[23]
public 'X_COLOR' => string '1000' (length=4)
1 =>
object(stdClass)[22]
public 'X_COLOR' => string '1002' (length=4)
array (size=4)
0 =>
object(stdClass)[20]
public 'X_QTY' => string '100' (length=3)
1 =>
object(stdClass)[21]
public 'X_QTY' => string '250' (length=3)
2 =>
object(stdClass)[17]
public 'X_QTY' => string '500' (length=3)
3 =>
object(stdClass)[19]
public 'X_QTY' => string '1000' (length=4)
array (size=3)
0 =>
object(stdClass)[25]
public 'O_RC' => string 'YES' (length=3)
1 =>
object(stdClass)[26]
public 'O_RC' => string 'NO' (length=2)
2 =>
object(stdClass)[27]
public 'O_RC' => string 'NA' (length=2)
My current MODEL is only returning X_QTY and O_RC to my view.
What am I doing incorrectly?
You are only returning the last result object of each; ie, you are setting
$qO_Array = $qO->result_object();
to a variable, and it should be:
$qO_Array[] = $qO->result_object();
to get all of them
BTW - do you have a specific reason for calling result_object() instead of the usual result() or result_array()? It's not necessarily wrong, but I wonder if you are doing it intentionally?

accessing objects data in an array

If someone has some help/advice about how to tackle this problem I'd really appreciate it. I've created a metasearch engine that for all intensive purposes works ok, but my code is pretty breakable! The following code is a sample var_dump of the array of objects - searchEngineArray. I want to store the results of each search engine in an array, but it needs to be in some kind of generic loop that (A). won't break if one search engine doesn't return results and (B). can easily accommodate additional search engines.
object(BingSearch)[1]
private 'formatted_query' => string 'england' (length=7)
public 'search_results' =>
array
0 =>
array
'title' => string 'England - Wikipedia, the free encyclopedia' (length=42)
'url' => string 'http://en.wikipedia.org/wiki/England' (length=36)
'score' => int 30
1 =>
array
'title' => string 'BBC News - England' (length=18)
'url' => string 'http://www.bbc.co.uk/news/england/' (length=34)
'score' => int 28
2 =>
array
'title' => string 'The FA - The website for the English football association, The FA ...' (length=69)
'url' => string 'http://www.thefa.com/' (length=21)
'score' => int 26
object(BlekkoSearch)[2]
private 'formatted_query' => string 'england' (length=7)
public 'search_results' =>
array
0 =>
array
'title' => string '<strong>England</strong> - Wikipedia' (length=36)
'url' => string 'http://en.wikipedia.org/wiki/England' (length=36)
'score' => int 25
1 =>
array
'title' => string 'The official site of Visit <strong>England</strong> - The <strong>England</strong> Tourist Board' (length=96)
'url' => string 'http://www.enjoyengland.com/' (length=28)
'score' => int 23
2 =>
array
'title' => string 'Arts Council <strong>England</strong> - Arts Council' (length=52)
'url' => string 'http://www.artscouncil.org.uk/' (length=30)
'score' => int 21
object(EntirewebSearch)[3]
private 'formatted_query' => string 'england' (length=7)
public 'search_results' =>
array
0 =>
array
'title' => string 'Arts Council England | Arts Council' (length=35)
'url' => string 'http://www.artscouncil.org.uk/' (length=30)
'score' => int 20
1 =>
array
'title' => string 'Sport England ' (length=14)
'url' => string 'http://www.sportengland.org/' (length=28)
'score' => int 18
2 =>
array
'title' => string 'Bank of England ' (length=16)
'url' => string 'http://www.bankofengland.co.uk/' (length=31)
'score' => int 16
I have it working to an extent, but its just not good code really. Heres how it works at the moment, has anyone got any ideas on how to create a more generic way of storing the information?
public function storeResults($searchEnginesArray)
{
//The following is very bad
//$blekko_Array = "";
//$bing_Array = "";
//$entireweb_Array = "";
for($x=0; $x<sizeof($searchEnginesArray); $x++)
{
var_dump($searchEnginesArray[$x]);
/*switch ($searchEnginesArray[$x]->getEngineName()) {
case "Bing":
$bing_Array = $searchEnginesArray[$x]->getResults();
break;
case "Blekko":
$blekko_Array = $searchEnginesArray[$x]->getResults();
break;
case "Entireweb":
$entireweb_Array = $searchEnginesArray[$x]->getResults();
break;
default:
echo "Error: Unexpected Search Engine : ".$searchEnginesArray[$x]->getEngineName(). " Expects [Bing/Blekko/Entireweb]";
} */
}
<?
public function storeResults($searchEnginesArray) {
for($i=0;$i<count($searchEnginesArray);$i++) {
$result = $searchEnginesArray[$i]->getResults();
// Ignore engines with empty results
if( !empty($result) ) {
$results[ $searchEnginesArray[$i]->getEngineName() ] = $result;
}
}
// create an array for every engine, are you sure it's needed?
extract($results, EXTR_SKIP);
}
?>

Doctrine_Collection __toString() type of functionality

Anyone have any ideas on the best way to convert a Doctrine_Collection to a CSV based on a specific column name?
Example array:
array
0 =>
array
'id' => string '2' (length=1)
'name' => string 'metallica' (length=14)
'created_at' => string '2011-09-02 23:15:15' (length=19)
'updated_at' => string '2011-10-05 02:51:23' (length=19)
1 =>
array
'id' => string '7' (length=1)
'name' => string 'coal chamber' (length=13)
'created_at' => string '2011-09-06 00:24:02' (length=19)
'updated_at' => string '2011-10-05 02:51:11' (length=19)
2 =>
array
'id' => string '14' (length=2)
'name' => string 'slayer' (length=14)
'created_at' => string '2011-10-05 02:48:58' (length=19)
'updated_at' => string '2011-10-05 02:50:15' (length=19)
I would like to end up with:
string 'metallica,coal chamber,slayer' (length=29)
Now I could easily do this with something like:
foreach ($this->getBands()->toArray() as $array) {
$names[] = $array['name'];
}
var_dump(implode(',', $names));
But, I'd like to see if there is a more elegant solution using the built-in methods provided by the Doctrine_Collection class.
Ended up just writing a wrapper method to transform Doctrine_Collections to CSVs based on a specific column:
public static function toString(array $options)
{
$collection = $options['collection'];
$columnName = $options['columnName'];
$separator = (isset($options['separator'])) ? $options['separator'] : ', ';
foreach ($collection->toArray() as $element) {
if (isset($element[$columnName])) {
$columnValues[] = $element[$columnName];
}
}
return (isset($columnValues)) ? implode($separator, $columnValues) : null;
}

Categories