CakePHP Model with $useTable = false still tries to connect to database - cakephp

I'm trying to develop an application that does not use a database, but that still has some models.
First problem: Cake look however for the configuration of the database (APP/Lib/Cake/Model/ConnectionManager.php, line 69), it tries to include the file Config/database.php.
If the file doesn't exist, I get 2 warnings. If it exists (even if it is completely empty) all ok. I want to clarify that this takes place even in the absence of models.
Second problem: if I try to use a model, I get the error:
Missing Datasource Configuration
Error: The datasource configuration default was not found in database.php.
This happens whether the Config/database.php file exists, whether the file is not present.
Finally, I specify that models (including the AppModel) have the property $useTable set to false, but it seems that it makes no difference.
But I have noticed that everything works correctly when in an application that uses a database there are only a few models that do not use it (but the app as a whole uses it).
I found this which proposes a solution, which then is the solution that is found on the web.
But below, in the comments, I read that Lorenzo says:
This does not make any sense at all, CakePHP 2.0 will not try to
perform any datbase connection unless you request it to do so
It seems to me that it has not, maybe CakePHP does not perform any query but still want a database is configured. Is that so? Or I missed something?
Thank you.

Possible causes
CakePHP doesn't try to connect to the database until it is actually required, however;
The app/Config/database.php file should always be present, even if you're not using a database. However, you don't have to 'configure' the various connections if you're not using them
If your models use behaviors, it's possible that database-calls are made by the behavior, causing CakePHP to attempt to connect to the database
Various methods inside the Model class will make connections to the database (e.g. requesting the 'schema' or 'columnType'.
Non-existing Model-methods will be mapped to a magic __call() method; This method will first check if a Behavior is able to handle the call, and otherwise will make a call to the database. E.g. using $this->Mymodel->doSomething() will cause CakePHP to attempt to open a database connection and execute doSomething in the database.
If a Model doesn't exist, CakePHP will create one, basically, it creates an instance of Model, using schema information from the database, causing a connection to be opened.
Example
class PostsController extends AppController {
// This controller does not use a Model
public $uses = false;
public function index() {
// CakePHP will construct a 'Post' model here
$this->Post->find('something');
}
}
Datasources, even if no database is used
Having said that. In most cases, it's best to have your models use a datasource; A datasource does not have to be a database, but may also be, for example, a Twitter Feed or a static Array().
Having a datasource simplifies most tasks (e.g. you can just perform find(....) to get the data from your source, whether that is a database or not.

Related

Yii2 Giving ActiveRecord Model Database Details on Construction

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

umbraco 7 how to access database MVC

I have Umbraco 7 website with MVC.
I want to perform some custom action on the database.
As I understand I should be using DbContext to connect.
I have referenced System.Data.Entity to get to DbContext class. However when I'm trying to use DbContext I'm getting an error saying
The type or namespace name 'DbContext' could not be found (are you missing
a using directive or an assembly reference?)
In my models namespace:
public class umbracoDbDSN : DbContext
{
//some code
}
Can you let me know what I am missing?
Thanks
You are mixing things up. Umbraco uses PetaPoco as ORM, not entity framework. You don't need to include the System.Data.Entity. Neither you need the DbContext.
However, if you have existing DataLayer logic which you need to incorporate, for legacy systems, you might need to continue with your code above. Then look for entity framework tutorials on the internet to continue your journey.
If you are not dragging legacy stuff, then the question is: do you want to perform queries on custom tables or do you want to query the Umbraco tables for some reason.
Let's start with the last one. Querying the umbraco tables:
If you want to connect to the umbraco SQL tables, I start wondering why. There is a ContentCache, which is blasting fast, and it enables you to query very quickly everything you need from the content section. You have API's for relationships, media, users, members and everything you need. So the question remains, WHY would you ever connect to the umbraco tables.
However, if you want to store data in custom tables, I would read this article of Warren: http://creativewebspecialist.co.uk/2013/07/16/umbraco-petapoco-to-store-blog-comments/
The idea is simple, you reuse the existing code base to extend umbraco behaviour without storing stuff in the content section.
Below a simple example for reusing the databaseconnection while querying some proper created table...
var db = ApplicationContext.Current.DatabaseContext.Database;
// Fetch a collection of contacts from the db.
var listOfContacs = db.Fetch<Contact>(new Sql().Select("*").From("myContactsTable"));

How do I use the CakePHP Configure class?

I am trying to use the Configure class in CakePHP, but I'm not sure if I am using it correctly. I have read through the cook book and the API, but I can't seem to do what I want.
I have created a configuration file: app/config/config.php. I can directly edit this file and set variables in there and access them using Configure::read().
Is it possible to update the values of the configuration file from the application itself, i.e., from a controller? I have tried using Configure::write(), but this does not seem to change the value.
app/config/config.php isn't a file that's automatically loaded by Cake. Either move these variables into app/config/bootstrap.php or tell your bootstrap.php file to load your custom file. You could also put your variables in app/config/core.php, but I'd recommend against that. I tend to like leaving that file alone and adding/overwriting values in bootstrap.php.
According to the API, Configure is supposed to be used "for managing runtime configuration information".
You can use its methods to create, read, update and delete (CRUD) configuration variables at runtime. The Configure class is available everywhere in your CakePHP application and therefore CRUD operations performed on its data in any place, including a controller.
If you are looking for persistent storage, you could consider a database (SQL or NoSQL). I would not recommend using a text file, as it raises a lot of security concerns. Even if security is not an issue, a database is propably a more fitting solution.
More on the Configure class in the Cookbook.

Zend Framework database initialisation

in tutorials such as this http://akrabat.com/zend-framework-tutorial/, the database parameters for the application are stored in the application.ini config file. Reading through the docs for zend_db and other database interaction tutorials, it suggests that the database object is created from parameters hard coded into php code. Whats confusing is that there doesnt appear to be any explicit initialisation of the database object in tutorials such as the one above. So my natural conclusion to this is that the database object is automatically generated from the parameters provided in the application.ini config file?
So my natural conclusion to this is
that the database object is
automatically generated from the
parameters provided in the
application.ini config file?
Kind of, in fact, there are a few step before your database get initialized.
Your application is bootstraped
It reads the config file
When a resource.* is found, check if the according resource class exists
The resource class initialize an object with the given parameter
Zend_Db_Table has a static method setDefaultAdapter($db) which takes the newly created Zend_Db object, now every Zend_Db_table object can use the Db object you set in your configuration.
Return the newly created object
Go back to 3.
Router, Controller, Layout, View, etc.
This "behavior" is recent, it's why you may found some old tutorial which shows you how to bootstrap your Zend_Db object manually, sometimes, it's just to show you how Zend_Db works.

Adding additional entities to a Entity Framework backed Domain Service

We're investigating using RIA Services (July 09 Preview) to expose parts of an existing EF model. We've added a Domain Service class to our web application and specified the EF model to use and selected a few of the entities we wish to make available via the domain service (some have editing enabled, most do not).
We build and everything is great, but if we want to add an additional entity to the domain service how do we do that. Is it a case of delete your current class and re-add and this hole will be plugged when RIA Services hits RTM?
I agree, that's annoying to type in all that manually every time the DB changes. What i end up doing is creating a new temporary domain service classes (and metadata) and cut&pasting the code into the existing domain service and then removing the temp service from the project.
Another option can be (didn't try it) to make the generated file a partial class, put all the new queries into a separate file and every time the DB Schema changes just blow away the generated file and recreate it using the wizard. Just a thought
You can just add the code for the new entities... just add the right methods, query, and depending on which operations you need, insert, update, delete and custom ones.
Yoiu shouldn't have to delete your current class, which theoretically contains a bunch of interesting app logic (I'd imagine) just because you want to add an entity.
My solution to this problem was to create a code snippet that does most of the work.
I only have to type efdsmethods, tab twice, and replace the EntitySet name, EntityType name, and entity variable for the methods to use and then I'm done. It makes adding the 4 standard methods very easy.
I've submitted my snippet as a patch (#10154) to the Silverlight Contrib project on codeplex, but it hasn't been accepted yet. Until then you can download the snippet from here.
Hope this helps you.

Resources