When does CakePHP call the is_connect method in a data source? - cakephp

I notice many CakePHP data source authors create the method to always return true. Is this so that you can ask the data source if it is connected ( like in a MySQL or Oracle db) or does the CakePHP data source itself call that function at some point?

there is a connected property in DBO_* but no methods like is_connected. you can also find a connect() method in the DBO files.
The connect will be called when the model is called as that will call the datasource. The reason most datasources will just return true is that they do not connect to anything. If might just be a file (csv datasource etc)

Related

in Entity Framework, DbSet.Local remains out of sync

This one is making me crasy : I have an EF model built upon a database that contains a table named Category with 6 rows in it.
I want to display this in a drop down list in WPF, so I need to bind it to the Categories.Local Observable collection.
The problem is that this observable collection never receives the content of the database table. My understanding is that the collection should get in sync with the database when performing a query or saving data with SaveChanges() So I ran the followin 2 tests :
Categories = _db.Categories.Local;
// test 1
Debug.WriteLine(_db.Categories.Count());
Debug.WriteLine(_db.Categories.Local.Count());
// test 2
_categories.Add(new Category() { CategoryName = "test" });
_db.SaveChanges();
Debug.WriteLine(_db.Categories.Count());
Debug.WriteLine(_db.Categories.Local.Count());
Debug.WriteLine(_categories.Count());
The test 1 shows 6 rows in the database, and 0 in local.
The test 2 shows 7 rows in the database, and 1 in local (both versions)
I also atempted to use _db.Category.Load() but as expected, it doesn't work because it is db first, not code first.
I also went through this page https://msdn.microsoft.com/en-us/library/jj574514(v=vs.113).aspx, created an object-based data source and linked my combo box to it, without success.
Does anyone know what I am doing wrong?
Thank you in advance for your help.
The DbSet<T> class is IQueryable<T>, hence DbSet<T>.Count() method maps to Queryable.Count<T> extension method, which in turn is translated to SQL query and returns the count of the records in the database table without loading anything into db context local cache.
While DbSet<T>.Local simply gives you access to the local cache. It contains the entities that you added as well as the ones being loaded by query that returns T instances (or other entities referencing T via navigation property). In order to fully load (populate) the local cache, you need to call Load:
_db.Categories.Load();
The Load is a custom extension method defined in QueryableExtensions class, so you need to include
using System.Data.Entity;
in order to get access to it (as well as to typed Include, XyzAsync and many other EF extension methods). The Load method is equivalent of ToList but without overhead of creating additional list.
Once you do that, the binding will work. Please note that the Local will not reflect changes made to the database through different DbContext instances or different applications/users.

using connection manager with orm cakephp 3

Every tutorial or documentation regarding this subject is used with sql queries but never with new orm.
I have two data sources defined in config/app.php: default and not_default
with same tables and columns, when the user login the default database is connected.
when the user clicks a certain button i need to change the connected database to not_default:
I achieved this so far:
ConnectionManager::get('default')->disconnect();
ConnectionManager::get('not_default')->connect();
ConnectionManager::get('not_default')->begin(); //dont know if required
when doing this: echo ConnectionManager::get('not_default')->isConnected(); I get 1 for not_default and 0 for default so it is connected
but the problem is when getting the model's TableRegistry the connectionname is still the default one!! How to change it???
I want to be able to get data using orm from database other than the default one.
You can use ConnectionManager::alias().
Connection aliases allow you to rename active connections without overwriting the aliased connection.
ConnectionManager::alias('not_default','default');
//from now on, all calls to default will resolve to not_default datasource

How to identify a BackboneJS Model serverside

I'm just getting my head around BackboneJS, but one of the (many) things I'm still struggling with is how exactly the Models Sync up and relate to the serverside DB records.
For example, I have a Model "Dvd", now I change an attribute on the "Dvd" Model, the Name for example, and then I call save(), how exactly does the server side know what DB record to update? Does Backbone hold the DB row ID or something else?
Yes, typically you will set up your Backbone models so that they have database row IDs for any objects you're working with. When one of them is brand new on the client side and not yet saved to the server, it will either have a default or no ID, which doesn't matter since the server will be assigning the ID if and when the initial save transaction succeeds.
Upon saving or updating a model item, Backbone expects the server to reply with some JSON that includes any attributes that have changed since the save or update request was made. In the response to the initial save request, the server informs the client of a newly saved item's row ID (and you can also send along any other information you might need to pass to the client at the same time).
By default, the 'id' attribute of a model object is assumed to be its unique identifier, but backbone lets you change this if you're using a different identifier for the primary key. Just give your model an idAttribute parameter (see the docs for Backbone.Model.extend()) to do that.
Meanwhile, either the urlRoot parameter or a url function can be given to your models to characterize the urls that should be used to send the various ajax requests to the server for a given model.

yii framework models database access object

I want to use the database access objects to access a table in my database instead of doing active record since that takes a long time to load. if I use database access objets instead (calling createCommand, query, execute, etc), do i still need to create a model class for the table? if so what would be the parent class of this model class? My goal is to access/edit the table values using yii database access objects. I'm using YII FRAMEWORK. Or is it best to use a component? If so, when do you usually have to use components? I don't understand what components are for....
No, you can create a generic class which has a member property of a CDbConnection. For that matter, you can just use CDbConnection, but you might end up creating a lot of connections that way.
class Foo
{
private $conn;
function __construct(){ $this->conn =
new CDbConnection($dsn,$username,$password); }
function runQuery($sql) {
$command=$connection->createCommand($sqlStatement);
return $command->query();
}
}
If you have your database settings in your config/main.php file, you can also do:
$command = Yii::app()->db->createCommand($sql);
$result = $command->queryAll();
and if you need to reset the command:
$command = false;
If you create a custom class, you don't need to reference a Model or extend an existing class, your connection will use CDbConnection (by using the method above or calling it directly in your connection statement).
this page has pretty clear info on Yii's DAO.
As far as components go, they can mean different things - there are Yii's "core components", which are things like urlManager, user, db, etc. and can have their default properties set in the config/main.php file. Then there is the "components" directory which can be configured to autoload classes and "contains components (e.g. helpers, widgets) that are only used by this application." So you can put custom classes in there that you want available throughout your app.
Components are used to make widgets.which is available through out the app.

Silverlight / .NET RIA Services - Exposing a custom property to the client

I have a table in my database called "Task". Task has the following fields:
- ID
- Description
- AssignedUserID
- TaskTypeID
I am accessing this table through a class that was created automatically after I used an ADO.NET Entity Data Model. I can load and show the fields mentioned above in a DataGrid in my Silverlight application. However, AssignedUserID and TaskTypeID are not very descriptive. So I decided to create a stored procedure that gets the tasks and the user and task type names through their respective lookup tables. This is where the problem lies.
I want to create some custom properties in the automatically generated "Task" class. The custom properties would be named "AssignedUserName" and "TaskType". I then want to make these properties available to my Silverlight client. However, I cannot seem to figure out how to get them exposed to my Silverlight client.
Can someone help?
Thank you
If your EDM is in the same project as the DomainService you can do this:
create a partial class on the Entity type, and add your calculated property in there.
name the file **.shared.cs
it will then be auto-shared with the client/Silverlight code.
Edit:
I was assuming that you could do this calculation in app logic rather than use an sp, which seems more straightforward to me.
If you do use an SP, you'll need to use the Function Import feature in the designer to map the SP to a function in the EDM. This function can then return entities, with properties mapped however you like.
An easier way would be to just use the object model: Have Task.AssignedUser and Task.TaskType objects off of your Task class. Map these to lookup tables in your db. This will work out-of-the box (assuming the Id's are FK's to those lookup tables).
So, a couple options:
use app-logic--properties in a partial class to return the descriptions
use the object model driven by FKs to lookup tables, then just access Task.AssignedUser.Name or Task.TaskType.Description
use a function import to access the SP and map the returned values to entity properties
1 or 2 being the best options IMHO.
Another approach might be to update your EF model to include the lookup tables, add Associations between the tables, add [Include]s in the (auto-gen'd) metadata class and let EF and RIA do it for you. Maybe.

Resources