Joomla External JTable connection - php

I have a database I need to connect to remotely that is different then the Joomla database.
I can do this easily within other models but JModelAdmin is giving some issues as it seems to require JTable to function.
I have attempted to override the JTable instance to use my external database instead, however it does not seem to want to work and gets an error in "reset". My guess is JTable requires access to the core Joomla tables as well.
Is there any easy way to do this? Or is overriding the core the only option?
EDIT:
To clarify I can get Joomla to connect to the database and run queries. The only problem is that JTable refuses to initialize properly with an external database.
Here is my constructor in my model:
function __construct($config = array()){
$config['dbo'] = TireApiHelper::tireAPIDB();
parent::__construct($config);
}
This works in my list model but not my admin model. my list model has no need for a table class, however the controller needs to use the admin model to publish/unpublish, this is where the issue is. Even though JTable uses the currently set DB instance, it will return false with no Joomla error (according to code I should see a joomla error if $table returns false.

There are several ways you can achieve this, but the key point is to create a new database object. You can find the instructions on how to do that here. Once you have this object you could:
set your JTable class extension to use it via constructor or using the setDBO(...) method. See this.
choose to use this object and a query object without any JTable, like is explained in the Documentation site.
Cheers.

Related

CakePHP 2.x - Database Switching On The Fly (For The Whole App)

Background:
I have an admin system, that needs to connect to and edit multiple databases. The database structures are 100% similar to each other, but the data varies.
What I have tried:
I'v tried to use $this->Model->setDataSource('db_variable_here'); in the controllers to change the database on the fly. The problem with this, is that all the related data seems to still be from my default database.
Example:
Imagine this: User HABTM Post, if I want to get a post from a different database, and use $this->Post->setDataSource('db_variable_here'); to achieve this, then it seems that I still get the related user from my default database, and not the same as the one I got the post from.
I'm guessing this is due to the fact that I only change the database on the Model Post, so it could be fixed by doing $this->Model->setDataSource('db_variable_here'); for each related model.
My Question:
Is it possible to change the datasource for every model in the app on the fly?
Ex. something like: $this->setDatasource('datasource_name')? Or do I really have to do it manually for all the related models?
Just save the database that you need to use in Session/Cookie (whatever tickles your fancy), then in your AppModel's __constructor() if the Session variable is defined then override either setDataSource() or setSource() accordingly.
Note that IIRC Cake's Session/Cookie are not available on the Models by default (because it's not supposed to be), so you might wanna use the good ol' $_SESSION or $_COOKIE or you will need to load it with App.
I do this to select to either use a Azure SQL database or a Rackspace MySQL database depending on the domain/URL, works as expected.
You could try making your own getDataSouce method in the AppModel.
You can see the CakePHP one here:
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/ConnectionManager.php
So, in your AppModel, just make sure to accept/return the da
class AppModel extends Model {
//...
public function getDataSource() {
// some logic here to determine which source you want
// Maybe use Configure::write('MYSOURCE', 'other_datasource');
// somewhere else, then just check it here.
$source = 'default';
$this->setDataSource($source);
return parent::getDataSource();
}
//...
}
This should get called instead of CakePHP's 'getDataSource()', then it will run your checks to determine which connection to use, then calls CakePHP's 'getDataSouce()' to do the rest of the actual work of getting the data source.
Assuming you set a variable (like a Configure variable) that's accessible from here, you could set it once anywhere in the app, and when this runs, it will use whatever you've specified.

Using Magento Collections with External DB

I'm trying to access data on a external DB from magento from a Module, while using Magento ORM capabilites, I was able to do so by extending the:
Mage_Core_Model_Abstract and Mage_Core_Model_Resource_Db_Abstract classes as shown on this page:
http://www.solvingmagento.com/accessing-an-external-database-from-your-magento-module/
However when I'm try to use the getCollection() method on my model and I get "false" so I'm wondering if you can create collections based on the "Mage_Core_Model_Resource_Db_Abstract" class, I tried with "Mage_Core_Model_Resource_Db_Collection_Abstract" without any luck.
If it's not possible, does that mean that I have to change my module and use:
Mage_Core_Model_Mysql4_Abstract instead of Mage_Core_Model_Resource_Db_Abstract
As it seems like it has a "Mage_Core_Model_Mysq4_Collection_Abstract" class that works for what I want to do, as shown in:
http://fishpig.co.uk/magento/tutorials/create-external-database-connection/
I was trying to avoid Mage_Core_Model_Mysql4_Abstract as it sounds to me that it's exclusive for MySQL and I was hoping to keep the code more flexible.
Thanks,

Codeigniter executing a hook with database calls prior to main Model constructor

I've been using hooks for my database migrations in my CI app using the post_controller_constructor hook.
In my recent revisions, I've changed my main Model variables from being set within the class constructor to being loaded from a db table. When I attempt to execute my new migration with the DB Forge data in it, my model doesn't load because it gets hung up on the fact that the table doesn't exist yet.
So I obviously can't make any database calls using pre_controller because I have no access to the main CI object. post_controller_constructor seems to execute after my Model constructor is loaded. What can I do to grab from the database before my Model is loaded?
Could you do it in the actual constructor (before calling parent::__construct())? I don't see the need for hooks in this case.

Yii framework - changing Database

I just migrate to yii and i created a model which it created an CActiveRecord with gii and after that i make some changes in database then it make me confused, now my question is :
1 - Should i recreate Model with gii after any change to database ? Why Active record in yii is much complicated than other frameworks like zend or codigniter !?
Edit :
If we should not change Model Class, where should we put our database functions !!? aren't Model is there for doing so?
If I generate model with gii its only one-time job. To start quickly using this model. After that if you alter your database structure( btw you didn't tell what kind of changes you made) you can change model manually.
If you generated model via gii and didn't change model you can regenerate it again (because no manual changes made).
If you already changed it there is no hard work to change it (and take journey of learning about ActiveRecord).
Just for learning create model from scratch (without gii).
You can always use Gii to preview the changes that it wants to apply to the existing model (using the diff link). Then you can apply them, but in case you have inserted custom rules inside the model (rules() method), they will be overwritten. And yes, if you do any changes to your database tables, you will have to update your CActiveRecord subclasses (your models), otherwise Yii will not be able to see your new fields or fail if it tries to access fields that it thinks it has but were deleted from the database.
Nothing stops you from writing your custom methods inside the model. But what exactly do you mean by "put our database functions"?
EASY
1) Into your aplicacion index.php create global variable
<?php
$GLOBALS['database'] = "mydbname"; //database name
// change the following paths if necessary
$yii=dirname(__FILE__).'/../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
require_once($yii);
Yii::createWebApplication($config)->run();
2) Into my model create a construct method
class Employees extends CActiveRecord
{
function __construct()
{
$dbname = $GLOBALS['database'];
Yii::app()->db->setActive(false);
Yii::app()->db->connectionString = 'mysql:host=localhost;dbname='.trim($dbname);
Yii::app()->db->setActive(true);
}
3) How to use into my controller
public function actionVer3()
{
$GLOBALS['database']="classicmodels"; //dbname
$employee = Employees::model()->findByAttributes(array("employeeNumber"=>"1002"));
print_r($employee);
echo "<br><hr>";
//$this->render("index");
}
Easy way tnx.
I think, You should ideally recreate the model after DB structural changes.
In our project, We are following the rule,
Do not make any changes in the models generated by gii. So that if
there are DB changes in future, we could directly regenerate the
models and will have no merging efforts.
This is also inline with philosophy of design,
**Objects** hide their data behind abstractions and expose functions that operate on that data.
**Data structures**
expose their data and have no meaningful functions. It only has methods to operate on data.
ActiveRecords(models in yii) are nothing but data structures.By adding business rules in ActiveRecords, you are mixing your data structures and objects.

Accessing wpdb (wordpress database) from laravel controller

The application I am working on consists of Laravel and Wordpress. I have all the data required in the wordpress database (as additional plugins if need be). I need to be able to connect to the wordpress' wpdb from the laravel controller in order to return the correct view.
however, when I include the wordpress' load.php
(require_once(<wp root>/wp-load.php)
I get the following error when accessing the laravel page:
Cannot redeclare __() (previously declared in /www/laravel/helpers.php:24)
this is because I am trying to use $wpdb to access the DB to get the cotents.
Any ideas on a workaround?
The problem is that wp-load.php boots most of the WordPress framework and WordPress has a function called __(). Apparently, so does Laravel. I tried booting wpdb by itself like this:
include('/path/to/wordpress/wp-includes/wp-db.php');
$mydb = New wpdb('user', 'pass', 'dummydb', 'localhost');
$test = $mydb->get_results("SELECT * FROM {$mydb->posts} LIMIT 5");
But it will throw undefined function errors because it uses functions in the rest of the WordPress code base, which isn't being loaded. That means you aren't going to be able to use $wpdb and Laravel without that function name conflict.
You really don't need $wpdb though, at least I don't know why you would. It isn't really much more than a fairly limited wrapper around PHP's mysql_* functions.
It is a (minor) convenience but that is all. If you have database connection information you can do about the same thing with straight PHP.
If you needed to use WP_Query I'd understand. Some of what it does would be very painful to write by hand.

Categories