Is it possible to use a REST API as Doctrine database?
Regarding the configuration on http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/configuration.html I can change the "driver" property.
But in the list of allowed drivers there is no one to use a REST API.
What I would like to do is:
<?php
$config = new \Doctrine\DBAL\Configuration();
//..
$connectionParams = array(
'dbname' => 'my_rest_api',
'user' => 'user',
'password' => 'secret',
'host' => 'localhost:3000',
'driver' => 'rest',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
$entityManager = EntityManager::create($conn, $config);
// Sends a GET request to localhost:3000/myentity/1 and maps it properly to my configured entity
$entity = $entityManager->find("MyNamespace\Entity\MyEntity", 1);
// Sends a POST request to localhost:3000/myentity
$entity = new MyEntity();
$entityManager->persist($entity);
$entityManager->flush();
// and so on
Thank you for your answers!!
DoctrineRestDriver is exactly doing what you are looking for!
https://github.com/CircleOfNice/DoctrineRestDriver
Have fun!
Related
We are trying to use dynamic facebook app credentials for Laravel 5.1 socialite.
config/services.php
'facebook' => [
'client_id' => 'xxxx',
'client_secret' => 'xxxx',
'redirect' => 'http://example.com/facebook-callback',
],
On my controller file:
public function getConnectFacebook()
{
return Socialite::driver('facebook')->redirect();
}
public function getFacebookCallback()
{
$user = Socialite::driver('facebook')->user();
}
We tried to overwrite the facebook driver this way:
public function getConnectFacebook()
{
Config::Set("services.facebook.client_id", "dynamic_app_id");
Config::Set("services.facebook.client_secret", "dynamic_app_secret");
Config::Set("services.facebook.redirect", "dynamic_app_redirect");
return Socialite::driver('facebook')->redirect();
}
But it was not working. Could you please let me know how we can achieve this?
Thanks.
like this
return Socialite::driver('facebook')->redirect()->setTargetUrl('your callback url');
Here is my solution.
private function makeFacebookDriver($domain){
//grab and set your config values from database or array. Don't do Config::Set.
$config['client_id'] = '';//grab fb id based from db based on domain
$config['client_secret'] = '';//grab fb secret from db based on domain
$config['redirect'] = 'http://'.$domain.'/fbcallback';
return Socialite::buildProvider(\Laravel\Socialite\Two\FacebookProvider::class, $config);
}
then use the function instead of calling Socialite::driver('Facebook');
$fb = $this->makeFacebookDriver('www.test.com');
return $fb->redirect();
just use it in your controller
use Laravel\Socialite\Two\FacebookProvider;
config
$config = [
'client_id' => '969935-d61celu1qck667krmbgql.apps.googlesercontent.com',
'client_secret' => 'sHrnnOz3Fmz4',
'redirect' => 'http://localhost:8000/api/login/facebook/callback'
];
$user= Socialite::buildProvider(FacebookProvider::class, $config)->stateless();
return $user->redirect();
stateless in callback
$userSocial =$config = [
'client_id' => '969d61celu1qck667krmbgql.apps.googlesercontent.com',
'client_secret' => 'sHrnnO3Fmz4',
'redirect' => 'http://localhost:8000/api/login/facebook/callback'
];
$user=Socialite::buildProvider(FacebookProvider::class, $config)-
>stateless()->user();
api or web.php
Route::get('login/{provider}', 'SocialController#redirect');
Route::get('login/{provider}/callback','SocialController#Callback');
You could also do a simple find and replace.
Let's say you have a config file like this
'facebook' => [
'client_id' => '{ID}',
'client_secret' => '{SECRET}',
'redirect' => '{REDIRECT}',
],
Now within your controller you can do something like this.
$fileName = 'path/to/file.php';
$configData = file_get_contents($file);
$configData = str_replace('{ID}','dynamic_id',$configData);
$configData = str_replace('{SECRET}','dynamic_secret',$configData);
$configData = str_replace('{REDIRECT}','dynamic_link',$configData);
file_put_contents($file, $configData);
That's it, nothing fancier.
Hi can you help me please! I want to use zend cache file since im in shared hosting.
So the problem is i cant retrieve cached doctrine file result. data has serialized coz it 's object. $cache->load($id) return as array i can't unserialize back.
Please i will glad if you could help me or suggest something. Here my code.
$request = $this->getRequest();
$slug = $request->getParam('slug');
$front = Zend_Controller_Front::getInstance();
$bootstrap= $front->getParam('bootstrap');
$cache = $bootstrap->getResource('cachemanager')->getCache('content');
$cacheId = md5('news_' . $slug);
if ( !($news = **unserialize($cache->load($cacheId))**) ) {
$news = $this->_em->getRepository('Custom\Entity\News')
->findOneBySlug($slug);
$cache->save($news, $cacheId);
var_dump('if u see me that mean not from cached');
}
$page = new Zend_Navigation_Page_Mvc(array(
'label' => $news[0]->getTitle(),
'route' => 'news-view',
'module' => 'news',
'controller' => 'index',
'action' => 'view',
'params' => array(
'slug' => $news[0]->getAlias())
)
);
$page->setActive(false);
$this->_helper->navigation->getContainer()
->findOneBy('uri', '/category/' . $news[0]->getCategory()->getSlug())
->addPage($page);
$this->view->ogpa = new OpenGraphProtocolArticle();
$this->view->news = $news;
$this->view->headTitle($news[0]->getTitle());
Do not save any Doctrine2 models in Zend_Cache see: https://ssmusoke.com/2012/03/25/doctrine2-day-3-proxies-associations-relationships/
This is because in the background Doctrine2 uses proxy classes which cannot be unserialized by the Zend auto loader, which will cause you a great deal of grief ...
If you are to cache anything, cache the output not the models.
I'm using SugarCRM 6.4.4 and php 5.3.10 and trying to add a contact to a target list using a soap call...I've also tried using the ProspectList class directly but I can't get it to work. I know the SOAP calls are getting through because I can create the Contact just fine, but the problem is coming when I try to create the relationship between the Contact and the Target List (PropsectList). What am I doing wrong?
Here's the SOAP call I tried. I'm getting an error that says: "Call to a member function add() on a non-object in /data/servers/thesite.com/web/administrator/sales/soap/SoapSugarUsers.php on line 1350"...Here's the code around line 1350
if (!empty($key)) {
$mod->load_relationship($key);
$mod->$key->add($module2_id);
return $error->get_soap_array();
}
Here's the full code for the SOAP:
// set up options array
$options = array(
"location" => 'http://www.thesite.com/sales/soap.php',
"uri" => 'http://www.sugarcrm.com/sugarcrm',
"trace" => 1
);
//user authentication array
$user_auth = array(
"user_name" => 'theuser',
"password" => MD5('thepass'),
"version" => '.01'
);
// connect to soap server
$client = new SoapClient(NULL, $options);
// Login to SugarCRM
$response = $client->login($user_auth,'test');
$session_id = $response->id;
$user_id = $client->get_user_id($session_id);
//Add Contact to SugarCRM
$response = $client->set_entry($session_id, 'Contacts', array(
array('name' => 'first_name','value' => 'roneFirst2'),
array('name' => 'last_name','value' => 'roneLast2'),
array('name' => 'account_name','value' => 'jdam Props'),
array('name' => 'email1','value' => 'roneEmail2#gmail.com'),
array('name' => 'work_phone','value' => '1.888.888.8888'),
array('name' => 'description','value' => 'This is the message'),
array('name' => 'assigned_user_id','value' => $user_id)
));
$contact_id = $response->id;
//Add Contact to TARGET LIST
$relationship = array(
'module1' => 'Contacts',
'module1_id' => "$contact_id",
'module2' => 'ProspectLists',
'module2_id' => "24053784-f8d3-22a4-5e99-4fc6a7d5159f" //Note: this id was pulled from the table in the database directly for the target list we want to link the contact with.
);
//call set_relationship()
$response = $client->set_relationship($session_id, $relationship);
been googleing and trying to figure this out for 3 days now...Thanks for any help you are willing to give.
The following ZendCasts cast, shows a way to use doctrine 2 in a zend framework environment.
Using this configuration, how can I make the connection use a utf-8 charset so the magic of "SET NAMES 'utf8'" will happen ?
What I'm really searching for is a way to configure it using the application.ini file.
If that's not possible using this configuration, how can this be done by code ? an _initDoctrine method in the Bootstratp file ?
Thank you.
UPDATE
It appears there's a post connect event which handles this, but I don't see how can I set it up via application.ini (if possible at all).
If not, can I set it up via a bootstrap method ? Will the bootstrap method run before any other doctrine connection code run, when relying on the Bisna library ?
If you are not using Bisna, you could simply do something like the following:
Pass the config stuff directly to EntityManager's connection options
(although driverOptions is not documented)
// $options is a simple array to hold your data
$connectionOptions = array(
'driver' => $options['conn']['driv'],
'user' => $options['conn']['user'],
'password' => $options['conn']['pass'],
'dbname' => $options['conn']['dbname'],
'host' => $options['conn']['host'],
'charset' => 'utf8',
'driverOptions' => array(
1002 => 'SET NAMES utf8'
)
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
I'm using the following custom bootstrap resource to initialize doctrine therefore $options is in application.ini and is accessible there by $this->getOptions();
// \library\My\Application\Resource\Doctrine.php
class My_Application_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
$options = $this->getOptions();
$config = new \Doctrine\ORM\Configuration();
//doctrine autoloader, config and other initializations
...
$connectionOptions = array(
.... //see above
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$registry = Zend_Registry::getInstance();
$registry->em = $em;
return $em;
}
}
It will bootstrap automatically if you put in application.ini
resources.doctrine.conn.host = '127.0.0.1'
resources.doctrine.conn.user = '...'
resources.doctrine.conn.pass = '...'
....
works fine for me
resources.doctrine.dbal.connections.default.parameters.driverOptions.1002 = "SET NAMES 'UTF8'"
1002 is the integer value of PDO::MYSQL_ATTR_INIT_COMMAND:
Command to execute when connecting to the MySQL server. Will
automatically be re-executed when reconnecting.
Note, this constant can only be used in the driver_options array when constructing a new
database handle.
this worked for me. config/autoload/doctrine.local.php
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => '...',
'password' => '...',
'dbname' => '...',
'driverOptions' => array(
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
)
),
)
)
)
);
It is possible to add it via application.ini, provided you use ZendX_Doctrine2 (at https://github.com/mridgway/ZendX_Doctrine2) with MySQL.
Then here's the line you need in application.ini:
resources.entitymanagerfactory.connectionOptions.driverOptions.1002 = "SET NAMES utf8"
(1002 == PDO::MYSQL_ATTR_INIT_COMMAND)
Don't forget to correctly set
default-character-set=utf8
in your my.cnf
Since this is for Doctrine 2, and ZendCasts is using Bisna, I believe you can just add this to your configuration.ini file
resources.doctrine.dbal.connections.default.parameters.driverOptions.charset = "utf8"
I'm not exactly sure how to test if it is sticking or not but let us know.
You could set the default table charset like that to utf8:
// Create new Doctrine Manager instance
$doctrineManager = Doctrine_Manager::getInstance();
// Set charset to UTF8
$doctrineManager->setAttribute(
Doctrine_Core::ATTR_DEFAULT_TABLE_CHARSET,
'utf8'
);
Quote:
an _initDoctrine method in the Bootstratp file ?
Yes.
For LoSo library and Doctrine 2 and MySQL add
resources.doctrine2.connection.driverOptions.1002 = "SET NAMES 'UTF8'"
to your application.ini
I have this in my bootstrap:
protected function _initDoctrineLibrary()
{
require_once('Doctrine/Doctrine.php');
$this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine', 'autoload'),'Doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE
);
$config = $this->getOption('doctrine');
$conn = Doctrine_Manager::connection($config['dsn'],'doctrine');
$conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
return $conn;
}
where in the application.ini you see
doctrine.dsn = "mysql://user:password#host/databasename"
I think you can do something similar
Is it possible to generate Doctrine 2 entities, with the relevant docblock annotations, from an existing database schema?
I had to made these changes for the above code to work..
<?php
use Doctrine\ORM\Tools\EntityGenerator;
ini_set("display_errors", "On");
$libPath = __DIR__; // Set this to where you have doctrine2 installed
// autoloaders
require_once $libPath . '/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
// config
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities'));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionParams = array(
'path' => 'test.sqlite3',
'driver' => 'pdo_sqlite',
);
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$cmf->setEntityManager($em);
$classes = $driver->getAllClassNames();
$metadata = $cmf->getAllMetadata();
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
print 'Done!';
?>
and mysql connection configuration like :
$connectionParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'database',
'charset' => 'utf8',
);
Yes it possible though RDBMS data types are not fully supported, so you might have to play with your code a bit before using it in your project. It's not straight forward as Doctrine 1.x used to be but still rather easy. Here some sample code I used myself (create folders properly before using it)
use Doctrine\ORM\Tools\EntityGenerator;
ini_set("display_errors", "On");
$libPath = __DIR__ . '/../lib/doctrine2';
// autoloaders
require_once $libPath . '/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', $libPath);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Entities', __DIR__);
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__);
$classLoader->register();
// config
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities'));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$connectionParams = array(
'dbname' => 'xx',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config);
// custom datatypes (not mapped for reverse engineering)
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
// fetch metadata
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver(
$em->getConnection()->getSchemaManager()
);
$classes = $driver->getAllClassNames();
foreach ($classes as $class) {
//any unsupported table/schema could be handled here to exclude some classes
if (true) {
$metadata[] = $cmf->getMetadataFor($class);
}
}
$em->getConfiguration()->setMetadataDriverImpl($driver);
$cmf = new \Doctrine\ORM\Tools\DisconnectedClassMetadataFactory($em);
$generator = new EntityGenerator();
$generator->setUpdateEntityIfExists(true);
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, __DIR__ . '/Entities');
print 'Done!';
I have implemented new command to achieve that https://github.com/umpirsky/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesDbCommand.php
Just add it like this:
$cli->addCommands(array(
// DBAL Commands
new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(),
// ORM Commands
new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(),
new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(),
new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(),
new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesDbCommand(),
new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(),
new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(),
new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(),
new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(),
));
$cli->run();
As of https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php , generating entities is already supported by Doctrine's default CLI