CakePHP 3 - ForceIndex on MySQL - cakephp

does anybody knows how it's possible to use the "force index" function on a mysql query in cakephp3 ORM?
i found some soloutions for cakephp2, but nothing for cakephp3. I know that's possible to make a raw query, but i prefer a way to use it with the cakephp orm.
thank you for your help. :-)

This isn't supported out of the box. For a clean implementation you'd have to create custom/extened query and query compiler classes to add such functionality, something similar to Query::modifiers().
https://github.com/cakephp/cakephp/blob/3.1.3/src/Database/Query.php#L357
https://github.com/cakephp/cakephp/blob/3.1.3/src/Database/QueryCompiler.php#L140
The only other way I can think of, would be to slip it in via Query::from(), however this feels kinda akward, and more of a workaround.
$query = $Table->find();
$query->from($query->newExpr('table_name TableAlias FORCE INDEX (index_name)'), true);
A little more dynamic:
$table = $Table->table();
$alias = $Table->alias();
$query->from($query->newExpr($table . ' ' . $alias . ' FORCE INDEX (index_name)'), true);
See also
API > \Cake\Database\Query::from()
Cookbook > Database Access & ORM > Query Builder > Raw Expressions

Related

filter with dynamic dict peewee ORM

Using peewee as my ORM, is there a way to directly filter with a dict?
For example, if I have a model
class User(BaseModel):
username = CharField(unique=True)
password = CharField()
email = CharField()
join_date = DateTimeField()
How can I filter all results with username Bob, with something like
params = {'username':'Bob'}
User.select().where(**params)
update
I found a solution, but I'm wondering if there's a better way ...
params = {'username':'Bob'}
User.select().where(*[getattr(User, k) == v for k, v in params.items()])
First of all, where are you getting this "dynamic dict" from? Presumably you would want to do some field validation and things before just hucking shit at the database -- and during that time you could move it into a better data-structure.
Also note that the only operation you would be able to do with the above is equality testing.
To answer your question, Peewee has a .filter() method which behaves like the one in Django. So you can throw your dictionary of data at it. docs are sparse because this method is really not recommended:
http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.filter

XPcollection not loaded - why?

This must be something very simple, I just don't see it (and can not find the answer :(
I am trying to learn DevExpress controls and have read that eXpress Persistent Objects is recommended for O/R mapping.
1) I have an existing SQL Server Compact 4.0 database for which I generated ORM
2) I have a Winform with XtraGrid.GridControl gridControl1
3) In Form_Load event I have this code:
XPCollection cName = new XPCollection(typeof(WindowsFormsApplication1.DUzskv1r6.XPO_TableName));
int c = cName.Count; //didn't help...
cName.DisplayableProperties = "Name;Nr"; //choose columns to display
gridControl1.MainView.PopulateColumns();
gridControl1.DataSource = cName;
I have read that it using "delayed loading" - loading when it is necessary (http://documentation.devexpress.com/#XPO/clsDevExpressXpoXPCollectiontopic), but reading XPcollections record Count didn't do the trick as it was suggested.
As a result I get an empty gridControl1 with columns "Name" and "Nr".
Please help - what am I missing?
I think the issue is somewhere in your datalayer initialization.
You use XPCollection with default session, maybe you forgot to initialize it.
The best way is to specify the session is in the XPCollection contractor.

Select query of sqlite in metro app?

I have seen quite a number of examples describing the usage of SQLite in Metro app. Most of the examples have either Orderby/Insert/Delete statements. May I know how do I get the data from a pre-populated db using the Select statement?
Secondly, how does someone store the data into an array or arrayList after the execution of the query?
Kindly help me with this,
Thanks.
See if this example is what you're looking for:
return db.runAsync('SELECT * FROM Table');
Here is a pretty useful article on it.
Extending the example to C#:
SQLiteAsyncConnection conn = new SQLiteAsyncConnection("people");
var query = conn.Table<Person>().Select();
var result = await query.ToListAsync();

In Magento how do you get the database name?

How do you get the database name from Magento?
I have seen functions like the one below that can get the table name.
$orderItemTable = Mage::getSingleton('core/resource')->getTableName('sales/order_item');
I was hoping that there was some sort of function like this:
Mage::getSingleton('core/resource')->getDatabaseName();
Thanks in advance for any ideas.
Each module can specify it's own connection so you are right to go via a model.
$config = Mage::getResourceModel('sales/order')->getConnection()->getConfig();
// $config is an array
$dbname = $config['dbname'];
If you're only interested in the default a slightly more efficient way might be:
$dbname = (string)Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname');
To get the db name try
Mage::getConfig()->getResourceConnectionConfig('default_setup')->dbname;
See How to get magento database details
It is always possible to get the current connection from Mage::getSingleton('core/resource')->getConnection('core_write'), quite specially if you use only one database.
$configArray = Mage::getSingleton('core/resource')->getConnection('core_write')->getConfig();
$db = $configArray['name'];
But it's comming with a zend adapter Zend_Config
// Create the object-oriented wrapper upon the configuration data
$config = new Zend_Config($configArray);
$db = $config->get('dbname');

Wordpress selecting wrong DB

I have a Wordpress site that uses two databases -- one section queries one database ("database_A"), and then Wordpress makes its connection to its own database ("database_B").
Everything it working well until I go to call this function:
$pageposts = $wpdb->get_results($querystr, OBJECT);
The Wordpress suddenly selects the wrong database ("database_A") when it was just using ("database_B").
How do I (a) prevent it from selecting ("database_A") or (b) make a call to have it select ("database_B")?
The wpdb class in WP ha a select() method. You should just be able to call it directly.
$wpdb->select('database_B');
You could also instantiate a second object that uses database_b:
$wpdb_b = new wpdb($db_b_user, $db_b_pwd, 'database_B', $db_b_host);
You can create a new $wpdb-var, like this:
<?php
$wpdb2 = new wpdb($user, $dbpassword, $db2, $dbhost);
?>
Now you can easily select items from the other database:
<?php
$pageposts = $wpdb2->get_results($querystr, OBJECT);
?>
I hope it helps you :]
(edit: Are you sure it suddenly changes the database? I mean, is it using database A before it is using database B, that's almost impossible...)

Resources