Simple Display Results from Database - zend-framework

I have set up correctly the parameters for Zend_Db::factory and then I am querying like:
$select = $db->select()
->from('imdb')
->limit(10);
$stmt = $db->query($select);
$result = $stmt->fetchAll();
Question:
Why I do not see anything displayed?
I continue and I am trying to display results by creating a new object
$moviesTBL = new Application_Model_DbTable_Imdb();
$this->view->albums = $moviesTBL->fetchAll();
If I combine it with a view it works fine BUT fetches all rows!!! How to make it fetch only first 10?
foreach($this->albums as $key=> $value)
{
echo $value ->rank.' '.$value->rating.' '.$value->title.' '.$value->year.' '.$value->number_of_votes.'<br>';
}
pana4219
Posts: 2
Joined: Mon Nov 04, 2013 6:45 pm

Try somthing like this:
$select = $db->select()
->from('imdb')
->limit(10);
$result = $db->fetchAll($select);
Other example:
$class = new Zend_Db_Table();
$db = $class->getDefaultAdapter();
$select = $db->select();
$select->from('imdb');
$select->limit(10);
$result = $db->fetchAll($select);
.ini
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.dbname = ""
resources.db.params.charset = "utf8"
resources.db.isDefaultTableAdapter = true
Bootstrap.php:
protected function _initDb() {
Zend_Db_Table_Abstract::setDefaultAdapter($this->getPluginResource('db')->getDbAdapter());
}

Related

sphinx autocomplete CALL SNIPPETS excerpts not returning result set

i am able to get a result set from print_r($stmt->fetchAll()); however, i cannot get a result set from print_r($r); below is the exact code from https://github.com/adriannuta/SphinxAutocompleteExample. the only change is the sql_query inside the sphinx.conf file.
ajax_suggest_excerpts.php
require_once 'common.php';
require_once 'functions.php';
$indexes = 'simplecomplete';
$arr =array();
$q = trim($_GET['term']);
$stmt = $ln_sph->prepare("SELECT * FROM $indexes WHERE MATCH(:match) LIMIT 0,10 OPTION ranker=sph04");
$aq = explode(' ',$q);
if(strlen($aq[count($aq)-1])<3){
$query = $q;
}else{
$query = $q.'*';
}
$stmt->bindValue(':match', $query,PDO::PARAM_STR);
$stmt->execute();
$docs = array();
$title = "";
$stmsnp = $ln_sph->prepare("CALL SNIPPETS(:doc,'simplecomplete',:query)");
$stmsnp->bindValue(':query',$query,PDO::PARAM_STR);
$stmsnp->bindParam(':doc',$title,PDO::PARAM_STR);
print_r($stmt->fetchAll()); //THIS RETURNS RESULT SET
foreach($stmt->fetchAll() as $r){
$title = $r['city'];
$stmsnp->execute();
$r = $stmsnp->fetch();
print_r($r); //THIS DOES NOT.
$arr[] = array('id' => utf8_encode($r['city']),'label' =>utf8_encode( $r['city']));
}
echo json_encode($arr);
exit();
sphinx.conf
source simplecomplete
{
type = mysql
sql_host = localhost
sql_user = swflorib
sql_pass = cii65419
sql_db = swflorib_db
sql_port = 3306 # optional, default is 3306
sql_query = SELECT id, Sic_Code_description, Sic_Code6_Description, City, hidden_keywords FROM no_admin_emails GROUP BY Company_Name
sql_field_string = Sic_Code_description
sql_field_string = Sic_Code6_Description
sql_field_string = City
sql_field_string = hidden_keywords
}
index simplecomplete
{
source = simplecomplete
path = /usr/local/sphinx/var/data/simplecomplete
docinfo = extern
charset_type = utf-8
min_word_len = 3
enable_star = 1
min_prefix_len = 3
}
From the documentation of fetchAll
http://us3.php.net/manual/en/pdostatement.fetchall.php
returns an array containing all of the remaining rows in the result set.
So the fetchAll inside the print_r() fetches all the rows from the database. The fetchAll inside the foreach() then has no rows left to fetch!
Looks like a misunderstanding of PHP/pdo.

PDO_Mysql adapter - data type comparison (identical comparison)

I have a few problem when I use PDO_Mysql adapter with Zend.
application.ini
resources.db.adapter = mysqli
resources.db.host = localhost
resources.db.dbname = myproject
resources.db.username = root
resources.db.password = 123456
resources.db.profiler.enabled = true
resources.db.profiler.class = "Zend_Db_Profiler_Firebug"
DB init in bootstrap.php
$database = $config['resources']['db'];
$db = Zend_Db::factory($database['adapter'], $database);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
$db->query("SET NAMES 'utf8';");
$db->query("SET CHARACTER SET 'utf8';");
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::getInstance()->set('db', $db);
return $db;
Ok, when I do a simple fetchAll() and check the loop:
foreach ($rows as $row) {
if ($row->id === 1) {
echo 'first id';
} else {
echo 'other rows';
}
}
Exists a row in table with "ID" = "1" (integer), but, for any reason... when I use PDO_Mysql, I cant do data type comparison ("===" or "!=="), but if I use "Mysqli" adapter works fine.
With PDO_Mysql adapter, each column of each row are "string" (when I check with var_dump).
Somebody knows the reason ?
Thanks.

Zend config inheritence

I have these values in my application.ini
[production]
; Database;
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user1"
resources.db.params.password = "password1"
resources.db.params.dbname = "projects__01"
;Names
website.settings.websiteName = "My website 1"
website.settings.websiteUrl = "http://www.mydomain1.com"
website.settings.title = "mydomain.com - mydomain"
website.settings.titleSeperator = " - "
[staging : production]
; Database;
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user2"
resources.db.params.password = "password2"
resources.db.params.dbname = "projects__02"
;Exceptions
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
;Title and url
website.settings.websiteName = "My website 2"
website.settings.websiteUrl = "http://www.mydomain2.com"
[development : staging]
;Database
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user3"
resources.db.params.password = "password3"
resources.db.params.dbname = "projects__03"
;Title and url
website.settings.websiteName = "My website 3"
website.settings.websiteUrl = "http://www.mydomain3.com"
The problem is all database and exception values work properly, meaning that they are inheriting properly as they are supposed to
But the values I have set for Title and url do not inherit properly, only the first defined ones are used.
Why is this? is this by design? are only predefined/standard environment values such as database and exceptions are inherited?
Or am I making a mistake somewhere?
Okay, from your comment it sounds like you are creating a new Zend_Config object in the bootstrap, putting this in the registry, and it's this that's not returning what you'd expect. If this is the case I'm guessing that you've omitted the second parameter on the config object, so you have something like this:
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini');
but what you should have is more like this:
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);
the second param tells it what section of the config file to use, without that it will always get the same value.
However, you don't actually need to re-parse the config file since Zend Application has done this already. Instead you can access the options from the bootstrap class and use these to create an object (or just store the options in their existing array format):
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
return $config;
}
and this should work as you expect.
If my guess was wrong, please can you edit your question to include the relevant part of your bootstrap where the config object is created and stored in the registry.

Zend_Paginator and cache. How can I read the datas that I have sent to cache

I register the data to cache and I see the foliowing :
zend_cache---Zend_Paginator_1_42242d5fa3c4e4b7758810c276163e8a
but I can't read.
$request = $this->getRequest();
$q = new Model();
$paginator = Zend_Paginator::factory($q->fetchAll());
$paginator->setCurrentPageNumber($request->getParam('p'));
$paginator->setItemCountPerPage(40);
$this->view->q = $paginator;
$fO = array('lifetime' => 3600, 'automatic_serialization' => true);
$bO = array('cache_dir'=> APPLICATION_PATH . '/cache/');
$cache = Zend_cache::factory('Core', 'File', $fO, $bO);
Zend_Paginator::setCache($cache);
Check to see if DB profiler is enabled. It seems that there is a conflict between DB profiler and Zend_Paginator_Adapter_DbTableSelect.
I've also created a new Paginator class that extends Zend_Paginator and I've modified getItemsByPage function.
$offset = ($pageNumber - 1) * $this->getItemCountPerPage();
$items = $this->_adapter->getItems($offset, $this->getItemCountPerPage());
These two lines of code should be added at the beggining, after $pageNumber = $this->normalizePageNumber($pageNumber).

Zend_Auth and Firebird DB

A short question for the PROs.
Is it possible to use Zend_Auth_Adapter_DbTable with ZendX_Db_Adapter?
I've tried this:
$adapter = new Zend_Auth_Adapter_DbTable(
Zend_Registry::get('db')
);
$adapter->setTableName('USERS')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setIdentity('FOO')
->setCredential('BAR');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate();
But it doesn't work.
ErrorMsg:
Catchable fatal error: Argument 1 passed to Zend_Auth::authenticate() must implement interface Zend_Auth_Adapter_Interface, none given, called in D:\xampp\htdocs\liquisales-online\application\controllers\IndexController.php on line 35 and defined in D:\xampp\htdocs\liquisales-online\library\Zend\Auth.php on line 115
Any hints?
Btw. ZendX_Db_Adapter ist registerd in application.ini
resources.db.adapter = Firebird
resources.db.params.dbname = "/var/db/liquisales.FDB"
resources.db.params.host = "127.0.0.1"
resources.db.params.username = sysdba
resources.db.params.password = masterkey
resources.db.params.adapterNamespace = "ZendX_Db_Adapter"
Okay, here is the right way to use Firebird DB with Zend_Auth.
At first we had to use capitol letters for column names.
Furthermore I've forgotten to pass the adapter.
Here's the right code.
$adapter = new Zend_Auth_Adapter_DbTable(
Zend_Registry::get('db')
);
$adapter->setTableName('USERS')
->setIdentityColumn('USERNAME')
->setCredentialColumn('PASSWORD')
->setIdentity($loginForm->getValue('kunummer'))
->setCredential($loginForm->getValue('passwd'));
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
it seems your adapter doesn't implement all the methods needed .... so you would have to code the authentication yourself.