I need to switch between 3 databases, it has occurred to me that the protected variable of the models changes according to a session variable.
I need to switch between 3 databases, it has occurred to me that the protected variable of the models changes according to a session variable that I will create in the login, but it does not allow me, does anyone know how to achieve the same effect?
Thanks!!!
Related
I have no idea how to google this since I mostly get tutorials about setting up multiple database connections.
At the moment, completely at random, my Laravel application gives me back an error about SQL, that the table could not be found. Laravel connects in this case to an entitely different database for some reason.
I connect in my .env and config/database.php to database1. Suddenly, an SQL error appears reading
Cannot find table `database2`.`table` in field list.
User also gets logged out when this error occurs.
Might be of use if i include that:
I sometimes use a custom user provider and sometimes the Laravel user provider. It depends on the subdomain. Error occurs on both configurations.
I dynamically add other database connections to the config, but never one to "database2". Database2 is a completely different application.
A search through the source code does not find any match with "database2"
Has anyone come across this problem? And if yes, how do I solve this? Thanks in advance!
Basically any Laravel Eloquent Model at seemingly Random will behave as if it was set with the public $connection="database 2"; variable.
Have you defined a model to use a different connection maybe?
Following the Laravel docs, this lets you use a different connection for specific models.
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The connection name for the model.
*
* #var string
*/
protected $connection = 'connection-name';
}
One of the most common reason is because you are setting your Configuration in a dynamic way with variables, so the Cache gets cached with the wrong information in this case the default database connection.
Or you could be setting the configuration at runtime which also makes the Illuminate\Foundation\Console\ConfigCacheCommand.php to generate the wrong files.
Make sure your configurations are serializable. Try running the command.
php artisan cache:config
I think it is necessary to access the session data from every where especially in the model, I don't know what is the conflict with the design or logic of CakePHP.
I wonder if we can use the Session class globally as Text class.
Does the problem just about the design and logic and nothing related to errors can be rises in the future?
As per Framework concept you can read/write/modify session from Controller.But have not read and write access in Model.
In CakePHP v3 you can manage session in model as mentioned below
\Cake\Routing\Router::getRequest()->session()->read('key');
\Cake\Routing\Router::getRequest()->session()->write('key', 'value');
I want to store global settings in the database, that then will be editable from cPanel. I have a model Setting, which has only 1 item with multiple columns such as siteName, language, siteDescription and etc. I want to make these accessible in every view, so do I need to do
$settings = \MyApp\Setting::find(1);
for every single controller? Is there a way to define this variable somewhere globally and have it accessible in every single controller / view ?
Or perhaps this is not the correct way to do it?
May be it's better to write that code snippet into BaseController.
or
Create a trait and write into it. use that trait in anywhere you want.
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.
I'm working on a web application, using the CAKEPHP framework. Herefor i need to request one variable on multiple pages (all pages have different controllers). it is oubvious that i get a error on several pages, since the variable isn't declared in all the different controllers.
Is there a workaround for this? i've already tried the app:: import to import a controller in another controller, but this doens't seem to work (still get a undefined variable error).
Thnx for your cooperation!
Regards,
Simon
Duplicate question, but I think it's phrased a bit better so I'll paste my answer here:
Standing on the shoulders of deceze's comment and DavidYell's answer, I think they've managed to scratch out a decent view of what you're trying to get to. Maybe. So with that loose understanding of what you're seeing and what you have...
By default, the PagesController::display() method generates the homepage view (home.ctp). I suspect that this is what you're talking about. That said, the variable you're setting in a method of your SectionsController won't be available to your homepage which is created by a different method in a different controller. If you want a variable available to all views there are several things you can do:
You can set the variable in your config/core.php file (not generally recommended)
You can set it in config/bootstrap.php if it's a constant. By that, I mean that it's a value you're going to hard code, not something dynamically generated. Whether you create the variable as a constant doesn't matter.
You can set in in your AppController in a beforeFilter() or beforeRender() method. All of your custom controllers (assuming you've followed protocol) inherit from the AppController. If you choose this path, make a copy of cake/libs/controller/app_controller.php and place it in your app/ directory.
Those are the ways that I think will best meet your needs as I understand them.
You can use the Configure.write... more info here
More on configure class
One way to make sure that the variable is available on all pages is to define it on the front controller (normally index.php) or any other always included file (such as global configs), another option might be to use the $_SESSION super global.
You can youse the beforeRender() or beforeFilter() callback methods from your AppController. :)
These'll be called on every page request. :)
If you to access a value from different controllers, you will need to save that value into a database record so it can be accessed by the different controller methods. Each controller call exists in its own context and can only share data that is stored external to the scripts.
In situations like this, I've created a preferences table (with fields like, id, name and value). Then add a $uses value to the app_controller to make it available to all controllers. Finally, just grab it with a find call. (ie. $foo = $this->Preferences->find( 'first', array('conditions'=>array('name'='foo')));