I have this config in application.ini:
resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.use_only_cookies = true
resources.session.gc_maxlifetime = 864000
resources.session.remember_me_seconds = 864000
resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
resources.session.saveHandler.options.name = "jm_sessions"
resources.session.saveHandler.options.primary.session_id = "session_id"
resources.session.saveHandler.options.primary.save_path = "save_path"
resources.session.saveHandler.options.primary.name = "name"
resources.session.saveHandler.options.primaryAssignment.sessionId = "sessionId"
resources.session.saveHandler.options.primaryAssignment.sessionSavePath = "sessionSavePath"
resources.session.saveHandler.options.primaryAssignment.sessionName = "sessionName"
resources.session.saveHandler.options.modifiedColumn = "modified"
resources.session.saveHandler.options.dataColumn = "session_data"
resources.session.saveHandler.options.lifetimeColumn = "lifetime"
Database structure is 100% right and connection to db (working) is set above this. I'm getting error that session_id, save_path etc. are undefined indexes. After that I added this code to bootstrap:
protected function _initCoreSession()
{
$config = array(
'name' => 'jm_sessions',
'primary' => array(
'session_id',
'save_path',
'name'
),
'primaryAssignment' => array(
'sessionId',
'sessionSavePath',
'sessionName'
),
'modifiedColumn' => 'modified',
'dataColumn' => 'session_data',
'lifetimeColumn' => 'lifetime'
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
Zend_Session::start();
}
After that I'm getting errors that session handler didn't found bd adapter:
Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for
Zend_Session_SaveHandler_DbTable'
Zend documentation is in this case very poor and I simply don't know what could be wrong in my configuration.
i am not very sure where is your problem , but i wanted to share with you my $config array
public function _initsession() {
$config = array(
'name' => 'session', //table name as per Zend_Db_Table
'primary' => array(
'session_id', //the sessionID given by PHP
'save_path', //session.save_path
'name', //session name
),
'primaryAssignment' => array(
//you must tell the save handler which columns you
//are using as the primary key. ORDER IS IMPORTANT
'sessionId', //first column of the primary key is of the sessionID
'sessionSavePath', //second column of the primary key is the save path
'sessionName', //third column of the primary key is the session name
),
'modifiedColumn' => 'modified', //time the session should expire
'dataColumn' => 'session_data', //serialized data
'lifetimeColumn' => 'lifetime', //end of life for a specific record
);
$adapter = new Zend_Session_SaveHandler_DbTable($config);
Zend_Session::setSaveHandler($adapter);
Zend_Session::start();
$session = new Zend_Session_Namespace('App');
Zend_Registry::set("session", $session);
}
Your config is close but not quite right. Here is the code I use in my application.ini file
resources.db.adapter = "Pdo_Mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "grabby_user"
resources.db.params.password = "w8F4cqZpaNz2WeS6"
resources.db.params.dbname = "grabby"
resources.db.isDefaultTableAdapter = true
resources.session.name = "Grabby"
resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.use_only_cookies = true
resources.session.remember_me_seconds = 3600
resources.session.saveHandler.class = "Zend_Session_SaveHandler_DbTable"
resources.session.saveHandler.options.name = "session"
resources.session.saveHandler.options.primary[] = "session_id"
resources.session.saveHandler.options.primary[] = "save_path"
resources.session.saveHandler.options.primary[] = "name"
resources.session.saveHandler.options.primaryAssignment[] = "sessionId"
resources.session.saveHandler.options.primaryAssignment[] = "sessionSavePath"
resources.session.saveHandler.options.primaryAssignment[] = "sessionName"
resources.session.saveHandler.options.modifiedColumn = "modified"
resources.session.saveHandler.options.dataColumn = "session_data"
resources.session.saveHandler.options.lifetimeColumn = "lifetime"
Just have to make sure the primary[] and primaryAssignment[] variables are set in the same order.
That is all I needed to get my sessions saved to the database. No need to set anything in the bootstrap file :)
Related
I am adding product into cart and tried to map customer id,email to that quote
using the below code
$product_id = 123;
$qty = 1;
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$superAttributeArray = array('151' => '3');
$params = array(
'product' => $product_id,
'qty' => $qty,
'super_attribute' => $superAttributeArray
);
$cart->addProduct($product, $params);
$cart->save();
$currenQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
$store = Mage::getSingleton('core/store')->load(Mage::app()->getStore()->getId());
$quote = Mage::getModel('sales/quote')->setStore($store)->load($currenQuoteId);
$quote->setCustomerId('1')->setCustomerEmail('test#gmail.com')->setCustomerFirstname('firstname')->setCustomerLastname('lastname');
$quote->save();
When I try to set customerid,email,fname,lname am getting error as "Mage registry key "controller" already exists".
Can anyone help me in fixing this issue?
Something like this might work
$customerObj = Mage::getModel('customer/customer')->load($customerId);
$quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
$storeId = Mage::app()->getStore()->getId();
$quoteObj->setStore(Mage::getSingleton('core/store')->load($storeId);
$productObj = Mage::getModel('catalog/product')->load($productId);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty('1');
$quoteItem->setStoreId($storeId);
$quoteObj->addItem($quoteItem);
$quoteObj->setStoreId($storeId);
$quoteObj->collectTotals();
$quoteObj->save();
I was wondering if there anyway to move this configuration from the INI into the bootstrap.php in a way it would be the default globally. If so, how should I do it? I'm with zend 1.11.
thanks in advance.
;resources.mail.transport.type = smtp
;resources.mail.transport.host = "smtp.gmail.com"
;resources.mail.transport.auth = login
;resources.mail.transport.ssl = ssl
;resources.mail.transport.port = 465
;resources.mail.transport.username = email#example.com
;resources.mail.transport.password = pass
;resources.mail.transport.register = true ; True by default
;resources.mail.defaultFrom.email = email#example.com
;resources.mail.defaultFrom.name = "Foo"
;resources.mail.defaultReplyTo.email = email#example.com
;resources.mail.defaultReplyTo.name = "Foo"
[SOLUTION]
Actually it worked for me adding:
$config = array(
'ssl' => $transport_ssl,
'port' => $transport_port,
'auth' => $transport_auth,
'username' => $transport_username,
'password' => $transport_password,
'register' => $transport_register,
);
$transport = new \Zend_Mail_Transport_Smtp($transport_host, $config);
// var_dump($transport);exit;
\Zend_Mail::setDefaultTransport($transport);
I have a requirement to implement audit logging functionality in a zend project. The models are created using zend db and the update function is as follows.
public function updateGroup($data,$id)
{
$row = $this->find($id)->current();
// set the row data
$row->name = $data['name'];
$row->description = $data['description'];
$row->updatedBy = $data['updatedBy'];
$row->updatedOn = date('Y-m-d H:i:s');
$id = $row->save();
return $id;
}
I have to create a table with the auditlog information which includes the current userid. I have tried many methods and nothing is a good solution. What is the best practice for a good audit logging functionality for zend?
I just want to log only the modified data. and the log table schema is like
id,
table,
column,
rowId
oldvalue,
newvalue,
updatedon,
updatedbyuser
use Zend_Log_Writer_Db :
Zend_Log_Writer_Db writes log information to a database table using
Zend_Db. The constructor of Zend_Log_Writer_Db receives a
Zend_Db_Adapter instance, a table name, and a mapping of database
columns to event data items
for example :
$columnMapping = array('name' => 'name',
'desc' => 'desc',
'updatedBy' => 'userid',
'updatedOn' => 'date');
$writer = new Zend_Log_Writer_Db($db, 'auditlog_table', $columnMapping);
$logger = new Zend_Log($writer);
$logger->setEventItem('name', $data['name']);
$logger->setEventItem('desc', $data['name']);
$logger->setEventItem('updatedBy',$data['updatedBy']);
$logger->setEventItem('updatedOn',date('Y-m-d H:i:s'));
EDIT : to log only the modified data :
public function logUpdate(array $values)
{
$columnMapping = array('id' => 'id',
'table' => 'table',
'column' => 'column',
'rowId' => 'rowId',
'oldvalue' => 'oldvalue',
'newvalue' => 'newvalue',
'updatedon' => 'updatedon',
'updatedbyuser' => 'updatedbyuser');
$writer = new Zend_Log_Writer_Db($db, 'auditlog_table', $columnMapping);
$logger = new Zend_Log($writer);
$logger->setEventItem('id', $values['id']);
$logger->setEventItem('table', $values['table']);
$logger->setEventItem('column', $values['column']);
$logger->setEventItem('rowId', $values['rowId']);
$logger->setEventItem('oldvalue', $values['oldValue']);
$logger->setEventItem('newValue', $values['newValue']);
$logger->setEventItem('updatedon', $values['updatedon']);
$logger->setEventItem('updatedbyuser', $values['updatedbyuser']);
}
and in updateGroup :
public function updateGroup($data,$id)
{
$row = $this->find($id)->current();
$values = array('table' => $this->name);
$values = array('updatedon' => $data['updatedBy']);
$values = array('updatedbyuser' => date('Y-m-d H:i:s'));
//go through all data to log the modified columns
foreach($data as $key => $value){
//check if modified log the modification
if($row->$key != $value){
$values = array('column' => $key);
$values = array('oldValue' => $row->$key);
$values = array('newValue' => $value);
logUpdate($values);
}
}
// set the row data
$row->name = $data['name'];
$row->description = $data['description'];
$row->updatedBy = $data['updatedBy'];
$row->updatedOn = date('Y-m-d H:i:s');
$id = $row->save();
return $id;
}
Note that its better to implement logging for all your application and seperate logging from update , see this answer for that .
I have the following PHP code:
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
'subdomain.example.com',
array(
'module' => 'subdomain',
'controller' => 'index',
'action' => 'index'
)
);
$plainPathRoute = new Zend_Controller_Router_Route(
':controller/:action/*',
array(
'controller'=> 'index',
'action' => 'index'
)
);
$router->addRoute('subdomain', $hostnameRoute->chain($plainPathRoute));
Now I would like to have this route in my application.ini
I tried it with this code, but this is not working:
resources.router.routes.subdomain.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.subdomain.route = "subdomain.example.com"
resources.router.routes.subdomain.defaults.module = "subdomain"
resources.router.routes.subdomain.chains.default.route = ":controller/:action/*"
resources.router.routes.subdomain.chains.default.defaults.controller = "index"
resources.router.routes.subdomain.chains.default.defaults.controller = "index"
Does anybody has an idea how to solve this?
resources.router.routes.subdomain.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.subdomain.route = ":module.example.com"
resources.router.routes.subdomain.defaults.module = ""
resources.router.routes.subdomain.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.subdomain.chains.index.route = ":controller/:action/*"
resources.router.routes.subdomain.chains.index.defaults.controller = "index"
resources.router.routes.subdomain.chains.index.defaults.action = "index"
Try this
Is it possible to generate Doctrine 2 entities, with the relevant docblock annotations, from an existing database schema?
I had to made these changes for the above code to work..
<?php
use Doctrine\ORM\Tools\EntityGenerator;
ini_set("display_errors", "On");
$libPath = __DIR__; // Set this to where you have doctrine2 installed
// autoloaders
require_once $libPath . '/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
// config
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities'));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionParams = array(
'path' => 'test.sqlite3',
'driver' => 'pdo_sqlite',
);
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$cmf->setEntityManager($em);
$classes = $driver->getAllClassNames();
$metadata = $cmf->getAllMetadata();
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
print 'Done!';
?>
and mysql connection configuration like :
$connectionParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'database',
'charset' => 'utf8',
);
Yes it possible though RDBMS data types are not fully supported, so you might have to play with your code a bit before using it in your project. It's not straight forward as Doctrine 1.x used to be but still rather easy. Here some sample code I used myself (create folders properly before using it)
use Doctrine\ORM\Tools\EntityGenerator;
ini_set("display_errors", "On");
$libPath = __DIR__ . '/../lib/doctrine2';
// autoloaders
require_once $libPath . '/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
// config
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities'));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionParams = array(
'dbname' => 'xx',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);
$classes = $driver->getAllClassNames();
foreach ($classes as $class) {
//any unsupported table/schema could be handled here to exclude some classes
if (true) {
$metadata[] = $cmf->getMetadataFor($class);
}
}
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
print 'Done!';
I have implemented new command to achieve that https://github.com/umpirsky/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesDbCommand.php
Just add it like this:
$cli->addCommands(array(
// DBAL Commands
new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(),
// ORM Commands
new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(),
new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesDbCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(),
new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),
));
$cli->run();
As of https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php , generating entities is already supported by Doctrine's default CLI