Skip to Content
It is currently July 5th, 2022, 8:15 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2, 3
Author Message
PostPosted: June 30th, 2013, 12:38 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
Code:
$result = array_flip(array_map('strtolower', array_flip($result)));

I'm not sure if there is any way to get the keys from MySQL in all lowercase directly, so I hacked up this messy solution.
array_flip flips keys and values of an array, array_map applies a callback function on each element of the array (though only on the values).
So we flip keys and values, apply strtolower on the new values (aka the keys) and then flip everything back so that keys are keys again and values are values again. Can you follow me?

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: June 30th, 2013, 12:50 pm 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Kinda... ;)
Didn't work though - got an error in the error_log file:
Quote:
Can only flip STRING and INTEGER values!

I saw something about passing in something like this, since it seems that the query is returning the keys as uppercase:
Code:
$options = array(
    Zend_Db::CASE_FOLDING => Zend_Db::CASE_LOWER
);

Not sure how to add that in here though...
Any ideas?
Thanks!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
Reply with quote  
PostPosted: July 1st, 2013, 10:05 am 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
There are several options for changing the database behavior.

I guess the best way is to get the config array for the Zend_Db_Adapter, manipulate that and create a new Zend_Db_Adapter instead.
Code:
$configarray = $db->getConfig();
$configarray['params']['options'][Zend_Db::CASE_FOLDING] = Zend_Db::CASE_LOWER;
$db = new \Zend_Db::factory($configarray['adapter'], $configarray['params']);

Better create a var_dump of configarray and check whether the array keys are really looking that way. I just copied the names from the original spacial code in library/Common/Database.php

---

Another way (also rather clean and definitely a way that the Zend Developers thought of) is to change the global settings. This might run you into problems with the other parts of the Web templates though. The spacial code in the Database class where it initializes the Zend_Db_Adapter from the XML config file explicitly changes the CASE_FOLDING option to CASE_UPPER, so I guess the other samPHPweb code expects column names to be all upper case.

Line 94-97 of library/Common/Database.php:
Code:
      $options = array(
         Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER,
         Zend_Db::AUTO_QUOTE_IDENTIFIERS => false,
      );

You can clearly see that Spacial deviates from the default settings for Quoting and Case Folding.

---

Last and least, there's an easy way that does not appear in the normal manual of Zend Framework and is discouraged because it's only available due to limitations in PHP.
The Zend_Db_Statement must be able to set the case folding in the Zend_Db_Adapter_* object for some reason and that is only possible if the method or property is public (aka accessible even from application code)
Code:
$db->foldCase(\Zend_Db::CASE_LOWER);

http://framework.zend.com/apidoc/1.12/f ... ysql.html#\Zend_Db_Adapter_Pdo_Mysql::foldCase%28%29

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: July 1st, 2013, 10:21 am 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Guess I'll try the first option then - thanks!
Where am I putting that though - after the $db = Database::getInstance line?
Didn't think of checking to see what was already used in the Spacial templates, but I tried showing the array that was retrieved from the database, and I saw it was in uppercase, so I assume that was the default...
Thanks a lot!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
Reply with quote  
PostPosted: July 1st, 2013, 10:26 am 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Hm... Getting an error:
Quote:
PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'

The error is on the last new line:
Code:
$db = new \Zend_Db::factory($configarray['adapter'], $configarray['params']);

Any ideas?
Thanks!

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
Reply with quote  
PostPosted: July 1st, 2013, 10:51 am 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 8306
Location: Cologne (Germany)
Ok, getConfig does not return the full Zend_Config array, but only the parameters part of it.
Therefore you need to find out the adapter by other means, like this (first and last line are only for reference and did not change from the original you posted)
Code:
$db = Database::getInstance(APP_PATH.DS.'config'.DS.'dbconfig.xml.php');
$adapter = explode('Zend_Db_Adapter_', get_class($db))[1]; // only works in PHP 5.4+
/* PHP 5.3- solution:
$adapter = explode('Zend_Db_Adapter_', get_class($db));
$adapter = $adapter[1];
// */
$configarray = $db->getConfig();
$configarray['options'][Zend_Db::CASE_FOLDING] = Zend_Db::CASE_LOWER;
$db = Zend_Db::factory($adapter, $configarray);

$db->setFetchMode(Zend_Db::FETCH_OBJ);

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

Shop for readymade PAL scripts by countrywesterndj - Or get a custom script made by me (or others)

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: July 1st, 2013, 11:29 am 
Offline
SVS Member
SVS Member

Joined: December 28th, 2009, 12:33 pm
Posts: 1344
Running PHP 5.3, so I had to use that version, but that seems to work.
Thanks a lot, and sorry for driving you crazy! ;)

_________________
Isaac Levine - Spacial Volunteer Support (SVS)


Top
 Profile  
Reply with quote  
PostPosted: March 21st, 2022, 11:51 pm 
Offline
New User
New User

Joined: May 9th, 2014, 10:36 pm
Posts: 2
Hello there,

Can someone please help me to get this code work with MySQL instead of ZendDB?

I tried to change the info $db->setFetchMode(Zend_Db::FETCH_OBJ); but i get error.


isaacl wrote:
OK, here's the final code that seems to be working, if anyone has any use for this:

Code:
<?php
// Define path to application directory
defined('APP_PATH') || define('APP_PATH', realpath(dirname(__FILE__)));
// Ensure common is on include_path
set_include_path(implode(PATH_SEPARATOR, array(realpath(APP_PATH . '/library'), '.')));
// Make a shortcut to DIRECTORY_SEPARATOR
define('DS', DIRECTORY_SEPARATOR);

// The Singleton Database class
require_once('Common/Database.php');

$db = Database::getInstance(APP_PATH.DS.'config'.DS.'dbconfig.xml.php');

$db->setFetchMode(Zend_Db::FETCH_OBJ);

// show the new requests
$select = $db->select()->from('requestlist','')->joinLeft('songlist', 'requestlist.songID = songlist.ID',array('songlist.title', 'songlist.trackno', 'songlist.artist', 'songlist.album'))->order('requestlist.id ASC')->where('requestlist.status = ?', 'new');
$stmt = $db->query($select);
$result = $stmt->fetchAll();

header('Content-Type: application/json');
echo str_replace(array('[', ']'), '', (stripslashes(json_encode($result))));
?>


In this case, the only thing I wanted to pull from the requestlist is the songID, and filter it based on the status.
I'm hoping to find some way to password protect this, and then I should be able to have a nice ways to view the request queue without having to log in to the SAM machine.
Thanks Mastacheata for all your help!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 38 posts ]  Go to page Previous  1, 2, 3

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group