Primary key column(s) (id) are not columns in this table () - zend-framework

I am following the: http://framework.zend.com/manual/en/learning.quickstart.create-model.html.
Keep hitting wall after wall of issues. My current one is the following error:
An error occurred
Application error
Exception information:
Message: Primary key column(s) (id) are not columns in this table ()
Stack trace:
#0 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Abstract.php(982): Zend_Db_Table_Abstract->_setupPrimaryKey()
#1 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Select.php(100): Zend_Db_Table_Abstract->info()
#2 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Select.php(78): Zend_Db_Table_Select->setTable(Object(Application_Model_DbTable_Guestbook))
#3 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Abstract.php(1018): Zend_Db_Table_Select->__construct(Object(Application_Model_DbTable_Guestbook))
#4 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Abstract.php(1326): Zend_Db_Table_Abstract->select()
#5 C:\wamp\www\zendtest\quickstart\application\models\GuestbookMapper.php(58): Zend_Db_Table_Abstract->fetchAll()
#6 C:\wamp\www\zendtest\quickstart\application\controllers\GuestbookController.php(14): Application_Model_GuestbookMapper->fetchAll()
#7 C:\wamp\www\zendtest\quickstart\library\Zend\Controller\Action.php(516): GuestbookController->indexAction()
#8 C:\wamp\www\zendtest\quickstart\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#9 C:\wamp\www\zendtest\quickstart\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#10 C:\wamp\www\zendtest\quickstart\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#11 C:\wamp\www\zendtest\quickstart\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#12 C:\wamp\www\zendtest\quickstart\public\index.php(26): Zend_Application->run()
#13 {main}
Request Parameters:
array (
'controller' => 'guestbook',
'action' => 'index',
'module' => 'default',
)
But I am sure my table has a primary key id for table:
CREATE TABLE guestbook (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
email VARCHAR(32) NOT NULL DEFAULT 'noemail#test.com',
comment TEXT NULL,
created DATETIME NOT NULL
);
CREATE INDEX "id" ON "guestbook" ("id");
Don't understand why getting error.
How can I see if it is even picking up the right database

Zend_Db_Table_Abstract contains a protected property called _primary which defaults to id.
It assumes that id is the table's primary key and is a sequence (auto increment).
Since your table definition says that id is a primary key, you should get rid of the second SQL command that adds an index on the id column. Since its already a primary key, it is an index.
Delete the table and re-create it using the create table statement you have in your question, just don't do the CREATE INDEX part. After that you should be able to continue.

I think that your SQL schema is incorrect. Try to create table with primary kyy like this:
CREATE TABLE guestbook (
id INTEGER NOT NULL AUTO_INCREMENT,
email VARCHAR(32) NOT NULL DEFAULT 'noemail#test.com',
comment TEXT NULL,
created DATETIME NOT NULL,
PRIMARY KEY (id)
);

This error might be induced by (accidentally) using md5(null) as the cache ID. Or any other customary values, for that matter, e.g. 1, 0, true, false. However, null is very likely to be the culprit in this context, as it could be produced by an undefined variable.
A fix could be as simple as:
$cache = Zend_Registry::get('cache');
$cache->remove(md5(null));

I had the same issue. The problem was that database was created in guestbook-dev.db instead of guestbook.db. You can check it by looking into this files. The solution was to change scripts/load.sqlite.php from
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (null === $env) ? 'development' : $env);
to:
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (null === $env) ? 'production' : $env);
and, of course, running the script once again.

Related

First wordpress plugin making

I am making my very first wordpress plugin in that I want to make table in my database when I activate the plugin. I write the code exactly what wordpress community said but it's not working. I tried the same several times but the results is the same. I also want to know my plugin showing 2 submenus in my main plugin menu, It's like
My Plugin
-- My Plugin
-- My Submenu Page
Please help I am very new to plugin development.
// Registering plugin
register_activation_hook(__FILE__, 'myplugin_activate');
//Deactivate plugin
register_deactivation_hook(__FILE__, 'myplugin_deactivate');
function myplugin_activate() {
global $wpdb, $table_prefix;
global $favoritethis_db_version;
$table_name = $table_prefix.
'my-plugin-table';
$charset_collate = $wpdb - > get_charset_collate();
if ($wpdb - > get_var("show tables like '$table_name'") != $table_name) {
require_once(ABSPATH.
'wp-admin/upgrade-functions.php');
$sql = "CREATE TABLE ".$table_name.
" (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00'
NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT ''
NOT NULL,
UNIQUE KEY id(id)
) $charset_collate;
";
}
require_once(ABSPATH.
'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
function myplugin_deactivate() {
// Deactivation code here...
echo "<script>alert('Ohhh.. no baby the plugin is deactivated now..')</script>";
}
also I found the solution of the problem, I was facing as getting extra sub menu as same name as the main menu is.
Solution
//Main admin menus
add_action('admin_menu', 'add_my_custom_menu');
function add_my_custom_menu() {
//add an item to the menu
add_menu_page(
'My Plugin',
'My Plugin',
10,
plugin_dir_path(__FILE__).
'admin/plugin-form.php',
'',
plugin_dir_url(__FILE__).
'img/contact.png'
);
add_submenu_page(
'my-plugin-name/admin/plugin-form.php',
'Plugin Setting',
'Plugin Setting',
10,
plugin_dir_path(__FILE__).
'admin/plugin-form.php',
'myplugin_options_page'
);
add_submenu_page(
'my-plugin-name/admin/plugin-form.php',
'Plugin Entries',
'Plugin Entries',
10,
plugin_dir_path(__FILE__).
'admin/entries.php',
''
);
}
reason is that every first sub-menu's destination page/function will be the same as main menu destination.
ok I got the answer myself
// Registering plugin
register_activation_hook(__FILE__, 'myplugin_activate');
//Deactivate plugin
register_deactivation_hook(__FILE__, 'myplugin_deactivate');
function myplugin_activate() {
global $wpdb;
$table_name = $wpdb - > prefix.
'my-plugin-table';
//nstalled_ver = get_option('my-voting-version');
if ($wpdb - > get_var("show tables like '$table_name' ") != $table_name) {
require_once(ABSPATH.
'wp-admin/upgrade-functions.php');
$sql = "CREATE TABLE IF NOT EXISTS `".str_replace('`', '', $table_name).
"` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00'
NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT ''
NOT NULL,
UNIQUE KEY id(id)
);
";
}
require_once(ABSPATH.
'wp-admin/includes/upgrade.php');
dbDelta($sql);
}

Zend error SQLSTATE[HY000] [1045]

I am having trouble with zend framework and connection with database, I'm trying to create a login, the table is created, and the code apparently is fine, but gives me this:
An error occurred
Application error
Exception information:
Message: SQLSTATE[HY000] [1045] Access denied for user 'onetag51_teste'#'localhost' (using password: YES)
Stack trace:
#0 C:\xampp\Zend\library\Zend\Db\Adapter\Pdo\Mysql.php(109): Zend_Db_Adapter_Pdo_Abstract->_connect()
#1 C:\xampp\Zend\library\Zend\Db\Adapter\Abstract.php(860): Zend_Db_Adapter_Pdo_Mysql->_connect()
#2 C:\xampp\Zend\library\Zend\Db\Adapter\Abstract.php(930): Zend_Db_Adapter_Abstract->quote('pass1', NULL)
#3 C:\xampp\Zend\library\Zend\Auth\Adapter\DbTable.php(449): Zend_Db_Adapter_Abstract->quoteInto('`password` = ?', 'pass1')
#4 C:\xampp\Zend\library\Zend\Auth\Adapter\DbTable.php(368): Zend_Auth_Adapter_DbTable->_authenticateCreateSelect()
#5 C:\xampp\Zend\library\Zend\Auth.php(117): Zend_Auth_Adapter_DbTable->authenticate()
#6 C:\xampp\htdocs\Eulen\application\controllers\AuthenticationController.php(27): Zend_Auth->authenticate(Object(Zend_Auth_Adapter_DbTable))
#7 C:\xampp\Zend\library\Zend\Controller\Action.php(516): AuthenticationController->logininAction()
#8 C:\xampp\Zend\library\Zend\Controller\Dispatcher\Standard.php(308): Zend_Controller_Action->dispatch('logininAction')
#9 C:\xampp\Zend\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#10 C:\xampp\Zend\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#11 C:\xampp\Zend\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#12 C:\xampp\htdocs\Eulen\public\index.php(26): Zend_Application->run()
#13 {main}
Request Parameters:
array (
'controller' => 'authentication',
'action' => 'loginin',
'module' => 'default',
)
I was searching for a solution, and some of the possible errors were, spelling errors(I didn't find any), can't connect to phpmyadmin using my pass and user(if I go to the phpmyadmim site, I can enter using the username and password).
I have try several solution but I cant figure it out.
Am I missing something.
file config.ini:
resources.db.params.charset = "utf8"
resources.db.adapter = "pdo_mysql"
resources.db.params.host = localhost
resources.db.params.username = "onetag51_teste"
resources.db.params.password = "*******"
resources.db.params.dbname = "onetag51_eulenhugo"
Its better to put here the authenticationcontroller, maybe the problem is there..
<?php
class AuthenticationController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function logininAction()
{
// action body
$authAdapter =$this->getAuthAdapter();
$username='hugo';
$password='pass1';
$authAdapter->setIdentity($username)
->setCredential($password);
$auth =Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if($result->isValid())
{
echo 'valid';
}
else
{
echo 'invalid';
}
}
public function logoutAction()
{
// action body
}
private function getAuthAdapter()
{
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password');
return $authAdapter;
}
}
ive been talking to a friend of mine, and he told me that i need to set the permission for acesse the phpmyadmim through zend...
How can i do that?
You don't need to use quotations around your adpater, username, password and dbname in your config file. I don't in mine, hope this helps.
I found a bit more reading that might be helpful access denied for user # 'localhost' to database '' because it mentions
You need grant privileges to the user if you want external acess to database(ie. web pages).
in the answer.
There is also this link about adding the users to the database in mysql.
Hope this helps
So the problem was related with the priveleges, thanks for ARJMP, ive tryed changed the priveleges but thee host didnd let me do it, so what i did was create a local database using xamp and try the login with the database that ive created in xamp, and it worked...

zend Framework quickstart A table must have a primary key, but none was found

I am following the: http://framework.zend.com/manual/en/learning.quickstart.create-model.html.
Keep hitting wall after wall of issues. My current one is the following error:
An error occurred
Application error
Exception information:
Message: A table must have a primary key, but none was found
Stack trace:
#0 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Abstract.php(982): Zend_Db_Table_Abstract->_setupPrimaryKey()
#1 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Select.php(100): Zend_Db_Table_Abstract->info()
#2 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Select.php(78): Zend_Db_Table_Select->setTable(Object(Application_Model_DbTable_Guestbook))
#3 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Abstract.php(1018): Zend_Db_Table_Select->__construct(Object(Application_Model_DbTable_Guestbook))
#4 C:\wamp\www\zendtest\quickstart\library\Zend\Db\Table\Abstract.php(1326): Zend_Db_Table_Abstract->select()
#5 C:\wamp\www\zendtest\quickstart\application\models\GuestbookMapper.php(58): Zend_Db_Table_Abstract->fetchAll()
#6 C:\wamp\www\zendtest\quickstart\application\controllers\GuestbookController.php(14): Application_Model_GuestbookMapper->fetchAll()
#7 C:\wamp\www\zendtest\quickstart\library\Zend\Controller\Action.php(516): GuestbookController->indexAction()
#8 C:\wamp\www\zendtest\quickstart\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#9 C:\wamp\www\zendtest\quickstart\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#10 C:\wamp\www\zendtest\quickstart\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#11 C:\wamp\www\zendtest\quickstart\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#12 C:\wamp\www\zendtest\quickstart\public\index.php(26): Zend_Application->run()
#13 {main}
Request Parameters:
array (
'controller' => 'guestbook',
'action' => 'index',
'module' => 'default',
)
But I am sure my table has a primary key code for table:
CREATE TABLE guestbook (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
email VARCHAR(32) NOT NULL DEFAULT 'noemail#test.com',
comment TEXT NULL,
created DATETIME NOT NULL
);
CREATE INDEX "id" ON "guestbook" ("id");
Don't understand why getting error
In Zend Framework all tables should have primary key column. In your model you should set name of the primary column.
For example, table user:
class Application_Model_TableUser extends Zend_Db_Table_Abstract
{
protected $_name = 'user'; // table name
protected $_primary = 'id'; // primary column name
}
I was getting the same error. I found out, that Zend caches DB metadata (for performance reasons obviously). In my case, I dropped and created the a table with the same name. The new table had a primary column, while the old one did not. Deleting Zend cache solved the issue for me.

Zend Framework : Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

I am getting this error
Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Stack trace:
#0 H:\Documents\IIS_Server_Root\zendframework\Zend\Db\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#1 H:\Documents\IIS_Server_Root\zendframework\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array)
#2 H:\Documents\IIS_Server_Root\zendframework\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array)
#3 H:\Documents\IIS_Server_Root\zendframework\Zend\Db\Table\Abstract.php(1526): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select))
#4 H:\Documents\IIS_Server_Root\zendframework\Zend\Db\Table\Abstract.php(1342): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select))
#5 H:\Documents\IIS_Server_Root\my.localhost\ahaweb\application\models\Tagjoin.php(28): Zend_Db_Table_Abstract->fetchAll(Object(Zend_Db_Statement_Pdo))
#6 H:\Documents\IIS_Server_Root\my.localhost\ahaweb\application\models\Tag.php(44): Model_Tagjoin->getTags('12')
#7 H:\Documents\IIS_Server_Root\my.localhost\ahaweb\application\models\Bookmark.php(30): Model_Tag->getTags('12')
#8 H:\Documents\IIS_Server_Root\my.localhost\ahaweb\application\controllers\UserController.php(69): Model_Bookmark->getUserBookmark(1, '12')
#9 H:\Documents\IIS_Server_Root\zendframework\Zend\Controller\Action.php(513): UserController->editAction()
#10 H:\Documents\IIS_Server_Root\zendframework\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('editAction')
#11 H:\Documents\IIS_Server_Root\zendframework\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#12 H:\Documents\IIS_Server_Root\zendframework\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#13 H:\Documents\IIS_Server_Root\zendframework\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#14 H:\Documents\IIS_Server_Root\my.localhost\ahaweb\public\index.php(26): Zend_Application->run()
In this code
$tj = new Model_Tagjoin();
$stmt = $tj->select('*')->setIntegrityCheck(false)
->joinInner("tags", "tags.tag_id = ".$this->_name.".tag_id",array("tag_name"))
->where($this->_name.".bmk_id = ?", $bmk_id)->query();
$r = $tj->fetchAll($stmt);
When I print_f the value of $stmt I get
SELECT `tagjoins`.*, `tags`.`tag_name` FROM `tagjoins`
INNER JOIN `tags` ON tags.tag_id = tagjoins.tag_id WHERE (tagjoins.bmk_id = '12')
which works fine if I use it directly with mysql.
Please help. Whats wrong with my code?
I don't know about using printf but as I have read it in the manual you should use the assemble() method to see your final sql query.
something like this.
$tj = new Model_Tagjoin();
$stmt = $tj->select('*')->setIntegrityCheck(false)
->joinInner("tags", "tags.tag_id = ".$this->_name.".tag_id",array("tag_name"))
->where($this->_name.".bmk_id = ?", $bmk_id)->query()->assemble();
exit($stmt);
$r = $tj->fetchAll($stmt);
Then try the query in phpMyAdmin and the like.
As a note if this call fails you might wanna remove the ->query() part of the line.

Zend_Auth : DB table structure

So followed all the basic tutorials. Am using Zend 1.8 but i keep getting this exception when using Zend_Auth_DB. Any ideas, there is only one row in the table.
Exception information:
Message: The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce a valid sql statement, please check table and column names for validity.
Stack trace:
#0 C:\projects\BHAA_ZEND\library\Zend\Auth\Adapter\DbTable.php(306): Zend_Auth_Adapter_DbTable->_authenticateQuerySelect(Object(Zend_Db_Select))
#1 C:\projects\BHAA_ZEND\library\Zend\Auth.php(117): Zend_Auth_Adapter_DbTable->authenticate()
#2 C:\projects\BHAA_ZEND\application\controllers\LoginController.php(79): Zend_Auth->authenticate(Object(Zend_Auth_Adapter_DbTable))
#3 C:\projects\BHAA_ZEND\application\controllers\LoginController.php(38): LoginController->getAuthAdapter(Array)
#4 C:\projects\BHAA_ZEND\library\Zend\Controller\Action.php(512): LoginController->processAction()
#5 C:\projects\BHAA_ZEND\library\Zend\Controller\Dispatcher\Standard.php(288): Zend_Controller_Action->dispatch('processAction')
#6 C:\projects\BHAA_ZEND\library\Zend\Controller\Front.php(945): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#7 C:\projects\BHAA_ZEND\library\Zend\Application\Bootstrap\Bootstrap.php(77): Zend_Controller_Front->dispatch()
#8 C:\projects\BHAA_ZEND\library\Zend\Application.php(328): Zend_Application_Bootstrap_Bootstrap->run()
#9 C:\projects\BHAA_ZEND\public\index.php(33): Zend_Application->run()
#10 {main}
Here's the offending code.
public function getAuthAdapter(array $params)
{
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter->setTableName('login')
->setIdentityColumn('username')
->setCredentialColumn('password');
//->setCredentialTreatment('MD5(?)');
// pass to the adapter the submitted username and password
$authAdapter->setIdentity($username)->setCredential($password);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
// success : store database row to auth's storage system
// (not the password though!)
$data = $authAdapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
//$this->_redirect('/');
return $this->_helper->redirector('index','index');
} else {
// failure: clear database row from session
$this->view->message = 'Login failed.';
return $this->render('index');
}
return $result;
}
Have you read the documentation for Zend_Auth_Adapter_DbTable? - Yes, but i'm not sure if it cleared or muddied my understanding.
Do you have a valid connection to the database? - Yup, have a working CRUD app with the same DB config.
What is the structure of your users table? -
Did you specify the identity and credential columns correctly to the constructor of Zend_Auth_Adapter_DbTable? - See code above, i believe so.
Did you also specify the username/password you're trying to authenticate? - Yup, have a Zend form submitting these variables.
What is the SQL query that is causing the error? I think this is it.
SELECT `isp_partners_view`.*, (CASE WHEN `password` = '*****' THEN 1 ELSE 0 END) AS `zend_auth_credential_match` FROM `isp_partners_view` WHERE (`username` = '*****')
You have given the error, but there are number of other questions I'd ask to try to troubleshoot this:
Have you read the documentation for Zend_Auth_Adapter_DbTable?
Do you have a valid connection to the database?
What is the structure of your users table?
Did you specify the identity and credential columns correctly to the constructor of Zend_Auth_Adapter_DbTable?
Did you also specify the username/password you're trying to authenticate?
What is the SQL query that is causing the error? You can view the query this way:
$select = $authAdapter->getDbSelect();
print $select . "\n";