After much of a struggle I managed to install MongoDB as a service and WAMP. Then on start I got a fatal error saying these would not work:
$m = new Mongo(...);
$m = new MongoClient(...);
In some previous questions on SO people mentioned using a new class called MongoDB/Driver/Manager. I also came across something called MongoDB/Client.
As a beginner to MongoDB I now stand rather confused about how to use/connect to a DB and collection.
I guess I will use:
$m = new MongoDB/Driver/Manager(...);
However,
$db = $m->$dbname; // Seems to cause -> Notice: Undefined Property
$collection = $db->shows; // dito
So all in all what are the difference between MongoDB/Driver/Manager and MongoDB/Client ? And with these new classes how would I correctly connect to a DB or Collection as shown in the previous snippet ? I can't seem to find many examples explaining how to use these new classes, or an up to date correct way of using the new classes for basic functionality.
Thanks,
I think I understand what I am confusing.
Using the MongoDB/Driver/Manager class and others, are part of the basic tools available with the PHP MongoDB Driver. I am guessing it is not recommended using them unless you know what you are doing, or you want something relatively customized.
A more recommendable alternative is to install "mongodb/mongodb-^1.x.x" with a PHP installer such as Composer, which will give you a MongoDB Library. This will give you classes such as the MongoDB/Client class.
Once the library has been installed you can connect like so:
<?php
require 'vendor/autoload.php';
$client = new MongoDB/Client('mongodb://localhost:27017');
// Add URI of MongoDB here
$mydb = $client->mydb; // Add the new DB name or existing DB name here
$collection = $mydb->createCollection('userCollection');
...
?>
Related
Good Day, my friends.
I want to use the doctrine ORM with the Migrations.
The issue is next: I want to place the migration configuration file in the specific folder. For example: 'config/doctrine-migrations.php'.
Everything working fine when I follow the official documentation and place the migrations.php file in the root folder, but when I try to place it in the specific folder system is not working.
My cli-config.php content is:
<?php
require_once "app/bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($container->get(\Doctrine\ORM\EntityManager::class));
Well, I can change this file in a next way:
<?php
require_once "app/bootstrap.php";
return \Doctrine\Migrations\DependencyFactory::fromEntityManager(
new \Doctrine\Migrations\Configuration\Migration\PhpFile(BP . '/config/doctrine-migrations.php'),
new \Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager($container->get(\Doctrine\ORM\EntityManager::class))
);
After this, Doctrine Migration working fine, but Doctrine ORM stop working with the next error:
Argument #1 ($helperSet) must be of type Symfony\Component\Console\Helper\HelperSet, Doctrine\Migrations\DependencyFactory given
If someone knows how to solve my issue and use a specific config file please clarify a possible solution.
Best Regards, Mavis.
I may be a bit late, but i ran into the same problem as you.
You can use the same cli-config for migrations and orm. For that, you need add
the orm commands manually to the cli-config.php.
My cli config looks like this:
$em = getEntityManager();
$config = new PhpFile('migrations.php');
$dependencyFactory = DependencyFactory::fromEntityManager($config, new ExistingEntityManager($em));
$migrationCommands = [
new Command\DumpSchemaCommand($dependencyFactory),
new Command\ExecuteCommand($dependencyFactory),
new Command\GenerateCommand($dependencyFactory),
new Command\LatestCommand($dependencyFactory),
new Command\ListCommand($dependencyFactory),
new Command\MigrateCommand($dependencyFactory),
new Command\RollupCommand($dependencyFactory),
new Command\StatusCommand($dependencyFactory),
new Command\SyncMetadataCommand($dependencyFactory),
new Command\VersionCommand($dependencyFactory),
];
$customCommands = [];
$commands = array_merge($migrationCommands, $customCommands);
ConsoleRunner::run(new SingleManagerProvider($em), $commands);
All my import statements, are not included in this example, but you can
get them from the docrtine documentation
Looks like I found an answer.
I can add custom integration in my application by this guide: https://www.doctrine-project.org/projects/doctrine-migrations/en/3.0/reference/custom-integration.html
In a custom file, I can configure whatever I want.
Hope that this solution will help somebody else.
I'm not very experienced with php projects structure, I found this awesome and simple tutorial: https://arjunphp.com/creating-restful-api-slim-framework/ how to create simple slim rest app.
This is actually PHP SLIM's official project structure, my question is what is best and proper way to add and use RedBean php ORM, I dont want on every route to include something like this
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=mydatabase', 'myusername', 'mypassword)
and then
$book = R::load( 'book', $id );
And then use ReadBean for my db stuff. Im wondering how to include RedBeans into project and then just use it where i need it. This is my project structure https://github.com/iarjunphp/creating-restful-api-slim-framework3.
Note: i added red beans via composer like its described here https://github.com/gabordemooij/redbean
You can put the code for setting up your libraries in any file that is going to be included on each request, so assuming you're using slim/slim-skeleton, src/dependencies.php is probably the place you want to add these two lines:
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=njux_db', 'root', '');
Then you can use ReadBeans in your route callbacks but you also need to add the use \RedBeanPHP\R as R; statement to your src/routes.php as well (or any file that is going to use this class)
If you use a MVC framework (which I recommend) like codeigniter it's pretty easy.
You only have to copy your rb.php to the application/third_party folder.
Then create a file called application/libraries/rb.php containing a code like this one.
<?php
class Rb {
function __construct() {
include(APPPATH.'/config/database.php');
include(APPPATH.'/third_party/rb.php');
$host = $db[$active_group]['hostname'];
$user = $db[$active_group]['username'];
$pass = $db[$active_group]['password'];
$db = $db[$active_group]['database'];
R::setup("mysql:host=$host;dbname=$db", $user, $pass);
}
}
?>
...and vĂ´ila. RedBean will read your database configuration from CodeIgniter's standard file application/config/database.php and you will be able to use any R:: command from anywhere in your code. No includes, not additional code required :-)
I have memcache 2.2.7 installed (using PECL on localhost) and currently using the PHP Memcache(d) class. I am trying to delete multiple keys in one function call using one of the documented methods:
simplified code:
$memcache = new \Memcache;
$memcache->deleteMulti($key_list);
However, I get an error:
"Call to undefined method Memcache::deleteMulti()"
The argument $key_list is an array of keynames as required. Does anyone know the reason for this? Has this method been recalled? Is it possible I am not actually using the Memcached class, but Memcache instead? How can I tell? If it is not possible to use this method, is there an alternative that will allow me to delete up to a few hundred keys at once, and do it FAST?
The Memcache ext does not have deleteMulti method, this is the reason of this error.
You can use Memcached:
$memcache = new \Memcached;
$memcache->deleteMulti($key_list);
I am not certain if I am maybe misunderstanding, something in the examples I see, but any time I try to make sure of nsComplexObject, I get an error that it does not exist.
I am specifically trying to create a sales order. I set up my array of values, but when I try to do the following, I get an error:
<?php
require_once '../PHPToolkit/NetSuiteService.php';
$service = new NetSuiteService();
$salesOrder = new nsComplexObject('SalesOrder');
?>
I think you're looking at older Toolkit examples.
The newer versions (I think since 2012_2) use a different method for instantiating objects, such as:
$salesOrder = new SalesOrder();
You can still use the setFields method to populate object properties, but you can also populate them directly now:
$salesOrder->entity = $someRecordRefObject;
I'm trying to understand the difference between regular Memcache and Doctrine's MemcacheCache.
In my bootstrap I instantiate Memcache like this:
$memcache = new Memcache;
$memcache->connect('localhost', 11211);
\Zend_Registry::set("pdc_memcache_core", $memcache);
I then have the following code for instantiating Doctrine - this is not the full code as I'm just looking at the cachine:
$doctrineConfig = new \Doctrine\ORM\Configuration();
$memcacheCore = \Zend_Registry::get("pdc_memcache_core");
$cache = new \Doctrine\Common\Cache\MemcacheCache;
$cache->setMemcache($memcacheCore);
$cache->setNamespace("PDC_");
\Zend_Registry::set("pdc_memcache", $cache);
$doctrineConfig->setMetadataCacheImpl($cache);
$doctrineConfig->setQueryCacheImpl($cache);
I then access the cache like this:
if (\Zend_Registry::get("pdc_memcache")->contains($cacheKey)) {
...
}
I'm not trying to store Doctrine entities in Memcache - I'm just storing strings and serialised arrays.
Now... is this the correct way to use Memcache with Doctrine? I can see that you use setMemcache to assign the Memcache instance to Doctrine's MemcacheCache... but would I use MemcacheCache for my own stuff? Or should I use the Memcache instance directly?
Would really appreciate some thoughts on this, as I'm confused by how this is supposed to link together.
It is perfectly ok to use the Memcache Cache of Doctrine Common, thats also the correct way and you also have some abstraction that allows you to consume any Doctrine\Common\Cache\Cache instance. I'm not quite sure you would need that anyway, as you're working with Zend Framework, and you probably could use the CacheManager or it's associated CacheManager resource, which both would give you the advantage to make your caching mechanism configurable.