How to set table schema at table name with Cake? - postgresql

PostgreSQL, Oracle and many other DBMS's use SCHEMA, so, the table name is
schema_name.table_name
But CakePHP manuals not say anithing about this. What about Model, View and Controller names in the CakePHP defaults? I can use a solution like prefix, that is, where the same schema name will be used at all database operations.
PS1: please not to be confused with method Modelschema, and questions about accessing this method.
PS2: the Bill's 2006 solution is not the better one, because is not updated (I am using CakePHP2) and is not a "official cakePHP solution".
PS3: database.php have some schema attribute? What the link to CakePHP documentation?

Good news for me, there are CakePHP 2.0 documentation about SQL-Schema... No other documentation or examples, but a starting point...

In CakePHP you must define more database config.
In CakePHP 2:
set the 'schema' param to your config
create new configs for all of your schema
use the right schema in your models
For example, database conf:
public $default = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'my_db_user',
'password' => 'my_db_passw',
'database' => 'my_project_db',
'prefix' => '',
'encoding' => 'utf8',
'schema' => 'postgres'
);
public $other_schema = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'my_db_user',
'password' => 'my_db_passw',
'database' => 'my_project_db',
'prefix' => '',
'encoding' => 'utf8',
'schema' => 'other_schema'
);
If you want to use it in a model:
class AppModel extends AppModel {
public $useDbConfig = 'other_schema';
}
In CakePHP3 is the same way, just there the database is config/app.php and you must use
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');

Related

TYPO3 Extension not fitting the Extbase Schema

Since the implementation of Doctrines DBAL we now can use different data-sources/connections (Hooray!).
We'd now like to embed one of our services within TYPO3 using this new feature.
The problem now is, that the domain models in this secondary database don't match the typical Extbase schema. E.g. it does't use int IDs nor are these models nested by any kind of parent ID.
Does anyone have experiences with this use-case or has an example?
Or do I have to setup an complete parallel Doctrine ORM to handle those models?
You could try to map your database fields, like here: https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/4-use-foreign-data-sources.html
I am not sure I understood your question correctly but we had a similar case. What we did is we added the second database in the connections.
LocalConfiguration.php
'DB' => [
'Connections' => [
'Default' => [
'charset' => 'utf8',
'dbname' => 'default_dbname',
'driver' => 'mysqli',
'host' => 'default_dbhost',
'password' => 'default_dbpwasswd',
'user' => 'default_dbuser',
],
'Secondary' => [
'charset' => 'utf8',
'dbname' => 'secondary_dbname',
'driver' => 'mysqli',
'host' => 'secondary_dbhost',
'password' => 'secondary_dbpasswd',
'user' => 'secondary_dbuser',
],
],
],
Then added a custom class that gets the data from the Database with the Query Builder
You first have to open a connection to your database, then create a query Builder Object for that connection in the class you use to get the data.
/**
* #var Connection
*/
private $connection;
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$this->secondaryConnection = $connectionPool->getConnectionByName('Secondary');
$queryBuilder = $this->secondaryConnection->createQueryBuilder();
// now you can get the data with query statements as linked above
$statement = $queryBuilder->select('your_field')->from('your_table')->execute();
while ($row = $statement->fetch()) {
$myObjects[] = new \Vendor\Extension\Domain\Model\ExternalObject($row);
}
I cast each result row to a "ExternalObject" Model that I added, it mapped the fields as closely as possible to my conventional Extbase Objects and then was able to use them almost interchangeably.
It depends on the Data you have in your database though, and the things you want to do with them.

ZfcUserDoctrineMongoODM Configuration examples

I am trying to configure ZendFramwork 2 to use MongoDB as the storage for user Authentication.
I already have installed what I believe are the correct modules and have a correctly installed instance of ZF2 running on Nginx. I also have an instance of Mongo running and I am already using this for another project. The modules declaration in application.config.php looks like this
'modules' => array(
'Application',
'ZfcBase',
'ZfcUser',
'ZfcUserDoctrineMongoODM',
'DoctrineModule',
'DoctrineMongoODMModule'
)
Documentation is a bit thin on how to configure the system to get it working. Can anyone provide any code fragments around configuring the database settings to allow Mongo to run with ZfcUser, this is where I am at a loss now.
Any help, clues or cheat sheets would be greatly appreciated.
Darren Breeze
Have a look at Doctrine Mongo ODM module readme https://github.com/doctrine/DoctrineMongoODMModule :
copy
vendor/doctrine/doctrine-mongo-odm-module/config/module.doctrine-mongo-odm.local.php.dist
into your application's config/autoload directory, rename it to
module.doctrine-mongo-odm.local.php and make the appropriate changes.
With this config file you can configure your mongo connection, add
extra annotations to register, add subscribers to the event manager,
add filters to the filter collection, and drivers to the driver chain.
So you have something like this at your autoload config:
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
// 'connectionString' => null,
// 'user' => null,
// 'password' => null,
// 'dbname' => null,
// 'options' => array()
),
),
....
'configuration' => array(
'odm_default' => array(
... 'default_db' => 'myappdb',
)
)
So tune it and try to register at user/register. Collection name is user by default.

Zend Doctrine Session SaveHandler. How make it work?

I am trying to understand how to set up a Session Save Handler (with Zend/Doctrine) using a database table but I am a bit confused about how it all should work.
I found this proposal that I think it suits my needs as I am also working with Doctrine.
All is set up: the proper class, the database table and Doctrine Model. What I don't get is this part:
$config = array(
'tableName' => 'Session',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'modifiedColumn' => 'modified',
'primaryKeyColumn' => 'id',
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_Doctrine($config));
Zend_Session::start();
I am confused here. Where should this part go? Can anyone please help? Or maybe point me to some useful tutorial to do this?
This should go in your main bootstrap class (application/Bootstrap.php). So I'd add something like this:
protected function _initDoctrineSession()
{
$config = array(
'tableName' => 'Session',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'modifiedColumn' => 'modified',
'primaryKeyColumn' => 'id',
);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_Doctrine($config));
Zend_Session::start();
}

Lithium mongodb relation between models

I'm using lithium with mongodb and I would like to know with my models below how I could get the data of the user from Posts::find('all'); query?
Do I have to do two queries?
Thanks for your help!
<?php
namespace app\models;
class Posts extends \lithium\data\Model {
protected $_schema = array(
'_id' => array('type' => 'id'),
'name' => array('type' => 'string', 'null' => false),
'description' => array('type' => 'string', 'null' => false),
'created' => array('type' => 'datetime'),
'updated' => array('type' => 'datetime'),
'user_id' => array('type' => 'integer')
);
protected $_meta = array(
'key' => '_id',
);
public $belongsTo = array('Users');
}
?>
<?php
namespace app\models;
class Users extends \lithium\data\Model {
public $hasMany = array('Posts');
public $validates = array(
'name' => 'Please enter a name',
);
protected $_schema = array(
'_id' => array('type' => 'id'),
'name' => array('type' => 'string', 'null' => false),
'slug' => array('type' => 'string', 'null' => false),
'created' => array('type' => 'datetime', 'null' => false),
);
}
?>
Currently relationships only exist for relational databases like MySQL and SQLite3. As such you'll need to make two queries to get the data you want. We're working on adding support for relationships for document based databases now however there currently is no timeframe on that.
You can use Set::extract on your result from posts to pull all of the user id's out then use the results from that to make a single query from users - so from posts you could do $userIDs = Set::extract('/posts/user_id', $posts->data()); then User::find('all', array('conditions' => array('_id' => $userIDs)));
hope this helps.
edit: You can find set::extract information here: http://li3.me/docs/lithium/util/Set::extract()
Do I have to do two queries?
This will depend on your schema.
Case #1
If Users and Posts are two different collections then you will need two different queries.
Case #2
If Users is the top level object and Posts "belongs to" Users then you would do something equivalent to db.users.find({ posts : {$exists:true} }).
I'm not 100% clear on how Lithium handles this. I cannot find a simple example of whether Lithium is doing #1 or #2.
As Howard3 said, there is currently no relationship support for MongoDB and as a result "belongs to" won't work either.
The actual decision depends on your application, but i assume that its going to be some form of a blog (users and posts). According to MongoDB schema design best practices, i'd put both in separate collections because they are "first level collections". a better fit for embedded documents would be posts and comments.
Also, you don't have to define the 'key' stuff when you're on latest master. You can write a custom find method for now that could be easily swapped to the more generic solution when relation support is finished in the core.
if you need more interactive help, visit #li3 on freenode.

Using Zend Framework Db Tables without MVC

I am trying to use the Zend Framework without using the MVC structure, specifically the Db_Table classes.
I have created a couple of classes representing my database tables, i.e.
class DBTables_Templates extends Zend_Db_Table_Abstract
{
protected $_name = "templates";
}
When I try to instantiate this class (it is included fine), I get the following error:
Fatal error: Uncaught exception 'Zend_Db_Table_Exception' with message 'No adapter found for DBTables_Templates'
Does anyone know how I create and include the database adapter for the Db_Table classes to use?
Any pointers are greatly appreciated! I am using the latest version of ZF.
You need to create a Zend_Db_Adapter, which is the class you use to connect to the database.
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
Or you can use the factory() method to make instantiation more configurable:
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
See http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting
Then specify this adapter object to your table class. There are at least three ways to do this:
Set an application-wide default for all tables:
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Specify the adapter to the table constructor:
$table = new MyTable( array('db'=>$db) );
Store the adapter in the registry and specify it to the table or set it as default:
Zend_Registry::set('my_db', $db);
$table = new MyTable( array('db'=>'my_db') );
// alternatively:
Zend_Db_Table_Abstract::setDefaultAdapter('my_db');
See http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.constructing