[Question for Core Team]: Can you explain how custom datasource work in CakePHP?

Hello,

I and many others have a plan to use nosql database or restful datasource in CakePHP 3. But we don’t have documentation which explain what is needed, what class (connections, driver,… ) should work, or the whole process from the established connection to the return result.

Yes, I know there is elastic, redis, mongo plugins

Thanks.

As far as I know, CakePHP does only support the following databases by default:

  • MySQL 5.5+
  • SQLite 3
  • PostgreSQL 8.3+
  • SQLServer 2008+

This means you can’t use a nosql database out of the box, which might explain why there is nothing on this in the official documentation.
There has been a proposal to do this, but there hasn’t been any updates on this.

Furthermore, there is an official elastic-search datasource available here.

Hi,
I’m not part of the cakePHP team, but I have written a plugin that allows to represent LDAP based structures on CakePHP-Table level.

So, what you basically need is to write a class that implements the Cake\Datasource\ConnectionInterface which manages the connection between you application and the database and another class that fulfills
the Cake\Datasource\RepositoryInterface which wraps all the operations that are done inside CakePHP table classes, therefore utilizing the Connection-class.

You can then let your tables use your driver by simply extending from the Repository-class instead of Cake\ORM\Table.

(could look like so:)

namespace App\Model\Table;

use Ldap\Datasource\LdapRepository;
//use Cake\Validation\Validator;


class MailgroupsLdap extends LdapRepository
{

	protected $_objectClasses = [
		0 => ["schema" => "misc", "class" => "nisMailAlias"],
	];

	public function initialize(array $config) {
		parent::initialize($config);

		$this->objectIdentifier("cn=");
		$this->tableDnFormat("ou=mailgroups,o=%s");
	}
}

?>

You can then (but don’t need to) write some “utility” classes for e.g. supporting data-validation, filtering and so on on your driver because those mechanisms obviously won’t work out of the box.

Writing a driver like this is hard work and makes you understand the complexity behind the simplicity of CakePHP.

I hope this helps as a brief overview about where you need to start.

Best regards,
Daniel

This plugin might give you some ideas about how to proceed?

Thanks all for answers. I’m currently on vacation