In Magento how do you get the database name? - database

How do you get the database name from Magento?
I have seen functions like the one below that can get the table name.
$orderItemTable = Mage::getSingleton('core/resource')->getTableName('sales/order_item');
I was hoping that there was some sort of function like this:
Mage::getSingleton('core/resource')->getDatabaseName();
Thanks in advance for any ideas.

Each module can specify it's own connection so you are right to go via a model.
$config = Mage::getResourceModel('sales/order')->getConnection()->getConfig();
// $config is an array
$dbname = $config['dbname'];
If you're only interested in the default a slightly more efficient way might be:
$dbname = (string)Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname');

To get the db name try
Mage::getConfig()->getResourceConnectionConfig('default_setup')->dbname;
See How to get magento database details

It is always possible to get the current connection from Mage::getSingleton('core/resource')->getConnection('core_write'), quite specially if you use only one database.
$configArray = Mage::getSingleton('core/resource')->getConnection('core_write')->getConfig();
$db = $configArray['name'];
But it's comming with a zend adapter Zend_Config
// Create the object-oriented wrapper upon the configuration data
$config = new Zend_Config($configArray);
$db = $config->get('dbname');

Related

Nlog set Database target programatically and access custom log message properties

I need to support database logging.
For that I decided to use nlog because it brings database support.
But first of all I need to setup the configuration programatically.
As far as I understood it I have to set the layout for the target.
But the class "DatabaseTarget" does not have any property related to Layout :/.
var dbTarget = new DatabaseTarget();
dbTarget.ConnectionString = LogConnectionString;
dbTarget.CommandType = System.Data.CommandType.StoredProcedure;
dbTarget.CommandText = "exec dbo.InsertLog #level=${level}, #callSite=${callsite}, #message=${message}, #stackTrace=${stacktrace}, #machinename=${machinename}, #processname=${processname}";
Is the layout definition really necessary for the DatabaseTarget. If so how do I set it programatically?
Additionally I want to pass some information. But I am not sure how I can assign those informations for the procedure.
As far as I understood it I can assign those variables:
https://github.com/nlog/nlog/wiki/Layout-Renderers
But NLog support generic arguments with his Log Method. It looks like this:
_nLog.Log<AJourFaultLevel>(ConvertLogLevel(logEntry.Level), logEntry.Message, logEntry.Fault);
How can I assign the passed "logEntry.Fault" value for my stored procedure?
Best regards
Your current log-statement injects logEntry.Fault as parameter into string.Format(logEntry.Message, logEntry.Fault):
_nLog.Log<AJourFaultLevel>(ConvertLogLevel(logEntry.Level), logEntry.Message, logEntry.Fault);
If you are using NLog 4.5 then you can use structured-logging where you can name the parameter like this:
_nLog.Log<AJourFaultLevel>(ConvertLogLevel(logEntry.Level), "Fault occurred: {AJourFaultLevel}", logEntry.Fault);
Then you can access the named parameter using ${event-properties:item=}:
dbTarget.CommandText = "exec dbo.InsertLog #level=${level}, #callSite=${callsite}, #message=${message}, #stackTrace=${stacktrace}, #machinename=${machinename}, #processname=${processname}, #faultLevel=${event-properties:item=AJourFaultLevel}";

How to create a file screen exception in powershell (FSRM Api)

I need to create file screen exception in powershell using the FSRM Api, I am using this script to create the cuota but I am having trouble to commit the object.
Because I haven't achieved to meet the requirement to modify AllowedFileGroups property :(
$FSRMObject = New-Object -Com Fsrm.FsrmFilescreenManager
$createFileScreenException = $FSRMObject.CreateFileScreenException("c:\")
$createFileScreenException.AllowedFileGroups("Text Files")
$createFileScreenException.Commit()
This is what I get Listing the Properties and Methods of the Object, in the property definition of AllowedFileGroups I can see that I need to create IFsrmMutableCollection.
Does anyone have an idea of how to create the file screen exception?
AllowedFileGroups is a property, not a method, so I'd expect something like this to work:
$createFileScreenException = $FSRMObject.CreateFileScreenException('c:\')
$createFileScreenException.AllowedFileGroups = 'Text Files'
$createFileScreenException.Commit()
Can't test it, though.
This is how you can create the simplest quota using the FSRM api in powershell, to view more modificable options get the members of the object $quota.
$fsrmQuotaObject = New-Object -Com FSrm.FsrmQuotaManager
$quota = $fsrmQuotaObject.CreateQuota("c:\path")
$quota.ApplyTemplate("Select template")
$quota.Commit()

CakePHP 3 - ForceIndex on MySQL

does anybody knows how it's possible to use the "force index" function on a mysql query in cakephp3 ORM?
i found some soloutions for cakephp2, but nothing for cakephp3. I know that's possible to make a raw query, but i prefer a way to use it with the cakephp orm.
thank you for your help. :-)
This isn't supported out of the box. For a clean implementation you'd have to create custom/extened query and query compiler classes to add such functionality, something similar to Query::modifiers().
https://github.com/cakephp/cakephp/blob/3.1.3/src/Database/Query.php#L357
https://github.com/cakephp/cakephp/blob/3.1.3/src/Database/QueryCompiler.php#L140
The only other way I can think of, would be to slip it in via Query::from(), however this feels kinda akward, and more of a workaround.
$query = $Table->find();
$query->from($query->newExpr('table_name TableAlias FORCE INDEX (index_name)'), true);
A little more dynamic:
$table = $Table->table();
$alias = $Table->alias();
$query->from($query->newExpr($table . ' ' . $alias . ' FORCE INDEX (index_name)'), true);
See also
API > \Cake\Database\Query::from()
Cookbook > Database Access & ORM > Query Builder > Raw Expressions

Play Scala: How to access multiple databases with anorm and Magic[T]

I want to access two databases in Play Scala with anorm and Magic[T], (one is H2 and another is PostgreSQL). I just don't know how to config it...
I noticed that we can set another database connection in conf/application.conf
db_other.url=jdbc:mysql://localhost/test
db_other.driver=com.mysql.jdbc.Driver
db_other.user=root
db_other.pass=
However, how can I use it with Magic?
(I read the source code of Magic but don't understand it... my am a freshman of Scala)
Anyhow, if multiple database access is impossible with Magic[T] , I wish to do it with anorm, then how can I config it?
var sqlQuery = SQL( //I guess some config params should be set here, but how?
"""
select * from Country
"""
)
In play.api.db.DB it appears you can pass in a string of the name you defined in application.conf.
Then use one of the methods specified here: http://www.playframework.org/documentation/2.0/ScalaDatabase
# play.api.db.DB.class
def withConnection[A](name : scala.Predef.String)(block : scala.Function1[java.sql.Connection, A])(implicit app : play.api.Application) : A = { /* compiled code */ }

Wordpress selecting wrong DB

I have a Wordpress site that uses two databases -- one section queries one database ("database_A"), and then Wordpress makes its connection to its own database ("database_B").
Everything it working well until I go to call this function:
$pageposts = $wpdb->get_results($querystr, OBJECT);
The Wordpress suddenly selects the wrong database ("database_A") when it was just using ("database_B").
How do I (a) prevent it from selecting ("database_A") or (b) make a call to have it select ("database_B")?
The wpdb class in WP ha a select() method. You should just be able to call it directly.
$wpdb->select('database_B');
You could also instantiate a second object that uses database_b:
$wpdb_b = new wpdb($db_b_user, $db_b_pwd, 'database_B', $db_b_host);
You can create a new $wpdb-var, like this:
<?php
$wpdb2 = new wpdb($user, $dbpassword, $db2, $dbhost);
?>
Now you can easily select items from the other database:
<?php
$pageposts = $wpdb2->get_results($querystr, OBJECT);
?>
I hope it helps you :]
(edit: Are you sure it suddenly changes the database? I mean, is it using database A before it is using database B, that's almost impossible...)

Resources