Yii2 Giving ActiveRecord Model Database Details on Construction - database

I am construction a login system for Yii2 that can create the identity from either within a table in the Yii2 application or from data from an external database, the data could reside in one of many, many databases.
I know i could setup second, third, fourth databases in the apps config, but it doesn't fit the use case as, as the database belongs to a user who could change it, etc.
What i need to do it instantiate an ActiveRecord model passing it the database details of the database it should connect to, to query.
I have managed to very much confuse myself over this, in trying to work out the correct way to do it.
I know you can pass a config array to the constructor, but am unable to understand how i should do it, should i create and object of \yii\db\connection and pass it to the __construct?
If so how do i then perform the connection ?
I have searched all over for similar use case, but am unable to discover anything that helps, even a pointer would be great.
Many Thanks

Ok, so after so more head banging and searching in a different way, i have discovered a very simple and sensible approach, and the answer does come from SO.
Yii2 set db connection at runtime

Related

How to pass database name dynamically to RoundHousE

I am trying to setup the RoundHousE project in my application to handle the database migration and version handling. I am following this article. It is fine as far as I know the database name exactly.
But I am not able to find, how should I handle the dynamic name of databases, because in my application I have separate database for each client, and list of these databases in a table in my main database. So name goes like: client1_db, client2_db etc.
Any solution or pointer towards the solution will be a great help.
A pointer towards the solution - The wiki https://github.com/chucknorris/roundhouse/wiki
Asking about passing the database name dynamically is a bit weird to me as when you run rh.exe or use the embedded DLL, database name is one of the required arguments. So you always have to pass the name dynamically. See https://github.com/chucknorris/roundhouse/wiki/ConfigurationOptions#main-stuff
Reading and trying to understand what you are asking, it seems you have a list of database names in a main database somewhere that you want to give to RoundhousE? To do that you would need to create something custom that can gather the name of the database(s) you are looking for and provide the result to RoundhousE.

Change Implementation of Select, Update, and Insert in SQL Server

To give you the question first: I want to know if it is possible to create a stored procedure or something in SQL Server that intercepts and translates SELECT, INSERT, and UPDATE commands. Now for the explanation:
I am writing a web application to replace an old desktop app. Its a business app which is basically a database interface with reports and searches and all the good ol' CRUD. The new and old apps need to live in harmony together since some customers may be using the old and new together to access the same DB.
My problem is that the original database format stores most data in a single blob of text (1 nvarchar(MAX) field). I want to add functionality to search on fields stored in the blob, but it will be cumbersome and slow. I would like to update the database format without changing the desktop app at all, hence the question above.
It occurs to me that I could do this on the client by writing a wrapper class for the data access object and then do a bulk replace in the client code to reference the wrapper, but I want to know what my options are on the server as well.
In case anyone wants to know, the old app is in VB6 and the new in C#.
EDIT
Alright, so it looks like if I do anything on the server side we are looking at adding stored procedures and then updating the client VB6 code to reference the stored procs. Do something like a bulk replace of SELECT with sp_oldselect ... To return the data in a different format. I'm guessing a client-side wrapper would be the best solution for the time-being. Old apps die hard.
You can create a bunch of views for the old client and let it to query those views. It will be slow as hell in most cases, but it can 'replace' the select query. For updates and insert.. well.. instead of triggers on the views could help is some cases, but it will require lots of processing.
However my suggestion is to provide exactly the same functionality in the web app and deprecate the desktop app. When the desktop app's share is low enough, stop supporting it. From this point, you are (mostly) free to add new functions, upgrade the database schema, etc.
I agree with JonH, that alot can go wrong here, but you can try and read up on the INSTEAD OF Triggers in MS SQL server here: https://technet.microsoft.com/en-us/library/ms179288(v=sql.105).aspx

Convert Connection to EntityManagerFactory or EntityManager

The question: Is there any way to get an EntityManagerFactory or EntityManager object when all you have is a java.sql.Connection object?
The explanation: We have a project specifically built to access our Oracle database. This is all pretty much legacy code and the database isn't exactly set up ORM friendly. We are trying to make any new tables and additions use Eclipselink though. The problem I am having is that this entire project was setup to pass around the connection object. So I won't have the url, username, or password for accessing the database with an EntityManagerFactory.
I have tried pulling the information from the connection with the metadata and all I can get seems to be the url and the user name. The password seems to not be there, I am assuming for security reasons.
It seems like the EntityManagerFactory and Connection are very similar objects so I was hoping to find a simple way to convert it but have only found the EM to Connection, not the Connection to EM(or EMF) and so I come seeking help, or at least a definitive no, it is not possible. Thanks!
This was answered in the comments section. So thought I'd post it as an answer for clarity and completeness. In case anyone happens by this question in the future.
"You can wrap the connection in an EMF/EM using native API, but without knowing anything about the persistence unit (defined in a persistence.xml), you won't get any value from it. You would need to create a persistence unit (with entities etc) and then load it to use your connection, though it would be better if you used a connection pool which you can pass as a property to JPA" http://onpersistence.blogspot.com/2008/04/eclipselink-and-datasources.html
Thanks Chris (from the comments.)

Symfony 2: Build own DB access layer

I'm new here and on a research trip. We would like to use Symfony2 for a new project.
Now we have the problem that we need to use a company wide self-developed DB access layer.
We don't want to lose the whole Symfony/Doctrine layer for handling data.
Our idea is now to create or overwrite database access layer below Doctrine.
Something like creating a bridge (like PDO) between Doctrine and our DB access layer.
Has anyone an idea how we can do this or maybe someone has done similar things already and can provide us with an how-to?
Please no questions why. We just have to use the company access layer!
Thank you
http://forum.symfony-project.org/viewtopic.php?f=23&t=37637
I found the solution in Doctrine2 doc. It's pretty easy.
I needed for development and testing (frontend and console) about half a day
I've done it in 4 steps:
copy Doctrine\DBAL\Driver\OCI8 to a new folder.
rename files.
change functions to use own database access layer.
add in config file
doctrine:
dbal:
driver_class: /Path/To/Driver/Class
Doctrine has not much but a bit of information about Abstraction Layer:
Supporting Other Databases
If you believe this ยง of the doctrine documentation, you can see that you may use the Common package because it does not require the DBAL. Sadly, the same thing cannot be said of the ORM package. The best thing to do is probably to fork this package so that it supports your own DBAL.
Condolences for being obliged to use this DBAL.

MVC 3 CodeFirst Share sql express database with all of my models

I am creating a data driven website with mvc3 using EF and code first. I am trying to have all of my models share one database so that seperate roles can have access to common tables. I am not sure weather to just use one db context because that seems like it might be the wrong way. I do not want my models to create their own seperate databases. I want them to share but I cant seem to achieve this. Thank to any help.
Instantiating multiple DbContexts will not cause you to have multiple databases unless you pass different connection strings into each one. If you have a single connection string, which will typically be kept in your web.config file, you can have many DbContexts, but they will all share the same database.
You must deploy your database directly to SQL Server instead of using local file in AppData => your connection string mustn't use AttachDBFile.
I was setting seperate DB contexts in all of my Models. I know, what a noob mistake. Thanks for the quick responses anyhow. THIS helped me figure it out and its really and amazaing resource for this stuff

Resources