How to set attributes (PDO::ATTR_ERRMODE) on the PDO database handle in Codeigniter?
I think a better option is to use a MY_Model (which you then extend and this is available then across the application) and define something like this in the construct:
$this->db->conn_id->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
Note conn_id allows you to access the main PDO object.
There are two ways:
1. The lazy (hacky) way
Add to the following code into system/core/database/drivers/pdo/pdo_driver.php (in CI 3):
public function db_connect($persistent = FALSE)
{
$this->options[PDO::ATTR_PERSISTENT] = $persistent;
// Added code start
$this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
// Added code end
try
{
return new PDO($this->dsn, $this->username, $this->password, $this->options);
...
}
2. The right way
Extend Database Driver and add the same line
Note: If you will set PDO::ERRMODE_EXCEPTION in Codeigniter it will show exception errors even in Production environment.
Related
I have a strange Problem in Zend Framework 2. I've used the Zend Skeleton Application (https://github.com/zendframework/ZendSkeletonApplication) and added PhlyContact as Vendor Module (https://github.com/weierophinney/PhlyContact). I changed the Translation-Type to PhpArray so that i can use the Zend_Validate.php located in the resources-dir of the ZF2-Dist.
Everything translates EXCEPT the validation Messages :/ So i guess i am missing something:
I must pass the Translator to Zend_Validate (but how and where?)
The Translation should use a Text-Domain, but doesn't
When i remember right in ZF1 you had to set the Translator to default to pass it to Zend_Validate. Any Ideas on that !?
have a look at these methods
\Zend\Validator\AbstractValidator::setDefaultTranslator();
\Zend\Validator\AbstractValidator::setDefaultTranslatorTextDomain();
You can even do this with only one line (2nd parameter is text domain):
AbstractValidator::setDefaultTranslator($translator, 'default');
Example within Module.php:
use Zend\Validator\AbstractValidator;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$translator = ....
AbstractValidator::setDefaultTranslator($translator, 'default');
}
}
I'm using PetaPoco T4 Template (http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx), It always converting table names, for example it converts a table named "Media" to "Medium". Have you any idea how can I make it to stop doing that?
Thanks
In your .Database.tt file you can tweak the class names to your liking for each table:
tables["Media"].ClassName = "Media";
I don't know of a way to turn this off everywhere. I believe it is a remnant from the SubSonic inflector. In the SchemaReader classes you will see a call like this:
tbl.ClassName=Inflector.MakeSingular(tbl.CleanName);
You could try changing that to
tbl.ClassName=tbl.CleanName;
To prevent that, I put always this piece of code in the Database.tt file:
// Read schema
var tables = LoadTables();
foreach(Table t in tables)
{
if(!t.Name.ToLower().StartsWith("tablePrefix_"))
{
// Filter tables by prefix
t.Ignore = true;
}
else
{
// This do the trick discussed here
t.ClassName = t.CleanName;
}
}
I'm am using Zend Framework.
I need to put multiple mail configurations in application.ini for Zend_Mail (using Zend_Application_Resource_Mail). Is it possible to do this using the standard classes in Zend Framework or do I need to create my own class?
I am using the latest stable version of Zend Framework.
Thanks for the answers
It does not appear to be possible to set multiple configurations for Zend_Mail with Zend_Application_Resource_Mail.
You could add the various configurations to application.ini but you will have to write your own class/functions to make the desired configuration active.
The things that are set by Zend_Application_Resource_Mail that you will have to override are Zend_Mail::setDefaultTransport($newTransport);, Zend_Mail::setDefaultReplyTo($email);, and Zend_Mail::setDefaultFrom($email);.
I tested something and found an easy thing you can do.
Set up your different configurations like this in application.ini:
mail_config.mail_test.transport.type = smtp
mail_config.mail_test.transport.host = "smtp.example.com"
mail_config.mail_test.transport.auth = login
mail_config.mail_test.transport.username = myUsername
mail_config.mail_test.transport.password = myPassword
mail_config.mail_test.defaultFrom.email = john#example.com
mail_config.mail_test.defaultFrom.name = "John Doe"
mail_config.mail_test.defaultReplyTo.email = Jane#example.com
mail_config.mail_test.defaultReplyTo.name = "Jane Doe"
Note how we are setting up options under mail_config. This will be the set of options to apply. mail_test is an example configuration. You can have multiple by setting mail_config.mail_test2, mail_config.corporate_mail, or mail_config.production etc.
Next, create an empty class that extends from Zend_Application_Resource_Mail. Preferably, it should be named and placed so it can be autoloaded.
The class:
<?php
class Application_Service_MailSettings extends Zend_Application_Resource_Mail { }
Now, here is how to override the default mail configuration easily with something else.
This example assumes you are in a controller:
// get the bootstrap, so we can get mail_config options
$bootstrap = $this->getInvokeArg('bootstrap');
$options = $bootstrap->getOption('mail_config');
// initialize the resource loader with the options from mail_config.mail_test
$mailSettings = new Application_Service_MailSettings($options['mail_test']);
$mailSettings->init(); // call init() so the settings are applied
// now the default transport, from, and reply to are set using mail_config.mail_test options.
// to use a different set of options, just do
// $mailSettings = new Application_Service_MailSettings($options['other_config');
This should accomplish what you want with very little new code.
i want to use zend_db standalone cos zend framework is too much for my project but i'm new with it,
is it correct to do this:
$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES
UTF8;');
$params = array(
'host' => 'localhost',
'username' => 'ss_fraat',
'password' => 'jos10',
'dbname' => '_a2hDB',
'driver_options' => $pdoParams
);
try {
$db = Zend_Db::factory('PDO_MYSQL', $params);
//set default adapter
Zend_Db_Table_Abstract::setDefaultAdapter($db);
} catch (Exception $e) {
exit($e->getMessage());
}
//save Db in registry for later use
Zend_Registry::set('dbAdapter', $db);
then in any class do this:
$db = Zend_Registry::get('db');
/** quote to avoid sql injection */
$date = $db->quote('1980-01-01');
$sql = 'SELECT * FROM product WHERE name = ' . $date;
$result = $db->query($sql);
$db->query(); //run a query
i really need to do this
Zend_Db_Table_Abstract::setDefaultAdapter($db);
i get this code from a website,
is it necessary to use Zend_Db_Table_Abstract if i'm not using the full zend framework,
or it is better for example to use this:
$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($db);
what i want is to setup a pdo/mysql connexion in my bootstrap php page and be able to get that db instance in any class without starting a new connexion to execute queries but i'm not sure how to do that use Zend_Db_Table_Abstract or Zend_Db_Select use the registry Zend_Registry::set('dbAdapter', $db) or not
thanks a lot
The purpose of Zend_Db_Table_Abstract is so you can create your own model classes based around the Table Data Gateway design pattern. The idea of that pattern is that you have a class that encapsulates all the sql you would need for interfacing with a table. So the assumption is that you will be creating model classes that extend Zend_Db_Table_Abstract for each table. If you are going to do that, then you will want to call Zend_Db_Table_Abstract::setDefaultAdapter($db) in your setup/bootstrap. Recent versions of ZF provide as an alternative a quick way of getting basic functionality without having to create a custom class definition by just instantiating Zend_Db_Table:
$userTable = new Zend_Db_Table('users');
In summary, none of this particularly has to do with the MVC part of the framework, although some people choose to use Zend_db as the basis for db connections and models, instead of using a more fully featured ORM like Doctrine or Propel.
The other code you provided simply illustrates that you do not need to use Zend_Db_Table_Abstract either -- you can simply setup an instance of a Zend_Db_Adapter and use that instance to call query() or its other methods.
In my app, I use Groovy as a scripting language. To make things easier for my customers, I have a global scope where I define helper classes and constants.
Currently, I need to run the script (which builds the global scope) every time a user script is executed:
context = setupGroovy();
runScript( context, "global.groovy" ); // Can I avoid doing this step every time?
runScript( context, "user.groovy" );
Is there a way to setup this global scope once and just tell the embedded script interpreter: "Look here if you can't find a variable"? That way, I could run the global script once.
Note: Security is not an issue here but if you know a way to make sure the user can't modify the global scope, that's an additional plus.
Shamelessly stolen from groovy.codehaus :
The most complete solution for people
who want to embed groovy scripts into
their servers and have them reloaded
on modification is the
GroovyScriptEngine. You initialize the
GroovyScriptEngine with a set of
CLASSPATH like roots that can be URLs
or directory names. You can then
execute any Groovy script within those
roots. The GSE will also track
dependencies between scripts so that
if any dependent script is modified
the whole tree will be recompiled and
reloaded.
Additionally, each time you run a
script you can pass in a Binding that
contains properties that the script
can access. Any properties set in the
script will also be available in that
binding after the script has run. Here
is a simple example:
/my/groovy/script/path/hello.groovy:
output = "Hello, ${input}!"
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
String[] roots = new String[] { "/my/groovy/script/path" };
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
Binding binding = new Binding();
binding.setVariable("input", "world");
gse.run("hello.groovy", binding);
System.out.println(binding.getVariable("output"));
This will print "Hello, world!".
Found: here
Would something like that work for you?
A simple solution is to use the code from groovy.lang.GroovyShell: You can precompile the script like so:
GroovyCodeSource gcs = AccessController.doPrivileged( new PrivilegedAction<GroovyCodeSource>() {
public GroovyCodeSource run() {
return new GroovyCodeSource( scriptCode, fileName, GroovyShell.DEFAULT_CODE_BASE );
}
} );
GroovyClassLoader loader = AccessController.doPrivileged( new PrivilegedAction<GroovyClassLoader>() {
public GroovyClassLoader run() {
return new GroovyClassLoader( parentLoader, CompilerConfiguration.DEFAULT );
}
} );
Class<?> scriptClass = loader.parseClass( gcs, false );
That's was the expensive part. Now use InvokeHelper to bind the compiled code to a context (with global variables) and run it:
Binding context = new javax.script.Binding();
Script script = InvokerHelper.createScript(scriptClass, context);
script.run();