Shanty_Mongo and Zend Framework 1.11 - php

I'm playin' around with zend framework 1.11 and mongo. I've decided to use Shanty_Mongo as a library to easy couple Zend and Mongo, but I'm stuck in this exception:
Can not save documet. Document is not connected to a db and collection
This is the code in the controller:
public function indexAction()
{
try {
$guestbook = new Application_Model_Guestbook();
$guestbook->setComment('Commento di prova')
->setEmail('info#example.net')
->save();
$all_elements = Application_Model_Guestbook::all();
$this->view->entries = $all_elements;
} catch (Exception $exc) {
echo $exc->getMessage();
}
}
This is (part) of the model:
class Application_Model_Guestbook extends Shanty_Mongo_Document
{
protected static $_db = 'test';
protected static $_collection = 'user';
protected $_comment;
.....
Shanty is in my library folder, and in application.ini i've added it:
resources.view[] =
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
autoloaderNamespaces[] = "Shanty"
On Shanty-Mongo docs, it's reported that
"If you are connecting to localhost without any authentication then no need to worry about connections any further. Shanty Mongo will connect automatically on the first request if no connections have previously been added."
but this does not happen.. I really can't guess why.
Obviously, mongo is running, since if i use php Mongo() i can access it and perform insertions, etc...
I'm running the latest version of mongo, zend on php 5.3.6 on osx 10.6.8
Thanks!

Your model should be like this
class Application_Model_Guestbook extends Shanty_Mongo_Document
{
protected static $_db = 'test';
protected static $_collection = 'user';
protected static $_requirements = array('comment'=>'Required')

I think you may want to switch that autoloaderNamespaces[] = "Shanty" line to be:
autoloaderNamespaces[] = 'Shanty_Mongo'
Other than that it looks OK....

That's an odd error message. Notice it doesn't say "unable to connect to MongoDB" or similar. It says that this document isn't connected to a collection. It sounds like a configuration issue to me.
In other areas of your code are you able to connect to the database?
Read from the database?

Allesio,
the element that both you and Adam C have put on the autoloaderNamespaces array are not quite correct. Try the following:
autoloaderNamespaces[] = "Shanty_"
You only need to put the top-level prefix followed by an underscore. Please let me know if this doesn't resolve the situation. Also, I've not seen that error message before. For sure, if you have a local install of mongoDB running, you won't need to specify any authentication parameters.
If the collection doesn't exist, Shanty will create it and if the document doesn't exist, Shanty will create that as well.
What operating system are you using?
I had a number of troubles with the package in the Ubuntu repositories. However, adding the 10gen repository in to apt and installing the latest stable version helped me out. Though even that seems to crash periodically.

Try adding this to Bootstrap.php:
protected function _initMongoDB() {
$connection = new Shanty_Mongo_Connection('mongodb://localhost:27017');
Shanty_Mongo::addMaster($connection);
}

Related

CakePHP 3.0 "thread class not found"

I want to implement the threading concept in CakePHP 3.0 ,
But when I try to extend the thread class, it gives an error of "Thread class not found"
I have also implemented it in core php and its working as expected,
But somehow its not working with cakephp.
Here is the corephp code
<?php
class AsyncOperation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$sleep = rand(1,60);
for ($i=0; $i < 100 ; $i++) {
sleep(1);
echo $this->arg."----------->".$i."<br/>";
}
}
}
}
class CallingClass {
public function runScript($var)
{
print_r("start run script");
$th = new AsyncOperation($var);
$th->start();
print_r("continue running");
}
}
$wow = new AsyncOperation("First");
$wow->start();
$wow2 = new AsyncOperation("Last");
$wow2->start();
?>
And in CakePHP 3
class AsyncOperation extends Thread
You want to learn about namespaces in php. Cake and almost every lib these days uses them. You need to use the use keyword and import the class from another namespace if it does not exist within the namespace your current class is in. Or, not really best practice, provide the absolute namespace.
Also I'm not sure what you try to do, but instead of threads I would recommend to take a look at work queues likes RabbitMQ or ZeroMQ.
Your php version doesn't have the thread class. By default, if you install it on LINUX, you won't have the thread class.
You need to download the php source code, enable the zts and then compile it.
This is how I did on linux:
Enable zts on redhat - pthreads on php
Just add simple line
use Thread;

Memcached (not memcache) PHP extension on Windows

I can't seem to find the MemcacheD extension for PHP.
There are a few compilations of php_memcache.dll, but that's not the same.
The main thing I'm missing is getMulti(), which doesn't exist in Memcache.
So far I found this, but there's no DLL:
http://pecl.php.net/package/memcached
Officially - it does not exist. There are several people who have created their own DLL's though. Here is one person's blog who has created the dll:
http://trondn.blogspot.com/2010/07/libmemcached-on-win32.html
Here is a link to the repository with the source so you can build your own DLL for memcached:
http://bazaar.launchpad.net/~trond-norbye/libmemcached/mingw32/files
I know the memcached has some other features but its interface is nearly identical with that of memcache extension. You can very easily get away with such code and in my case it works perfectly well. If you don't have the memcached loaded create this file:
<?php
class Memcached {
const OPT_LIBKETAMA_COMPATIBLE = true;
const OPT_COMPRESSION = true;
const OPT_NO_BLOCK = true;
//if you code relies on any other constants define them to avoid
//undefined constant notice
//http://www.php.net/manual/en/memcached.constants.php
public $_instance;
public function __construct() {
$this->_instance = new Memcache;
}
public function __call($name, $args) {
return call_user_func_array(array($this->_instance, $name), $args);
}
public function setOption() {}
}
Either include it or configure autoloader to pick it up.
Of course you'll need a properly configured memcache instance and addServer calls but such calls should already be in the code if the codebase assumes Memcached.
I hope it helps someone/

Configure DBAL via Symfony2 to set charset

Does anybody know a way of configuring DBAL/Doctrine2 in a Symfony2 (symfony-reloaded) yml config file to execute a "set names" query? This question has been asked in other places, but I could not find a correct answer.
http://fossplanet.com/f6/%5Bsymfony-users%5D-symfony2-sandbox-database-collation-49626/
If there is no such config option, how can I implement this using PHP? Or better: Where is the right place in a Symfony2 project to do this?
That is not possible yet. I am working on allowing this already, will be possible soonish.
Ok, just for anybody else who might run into this problem. This is what I did:
I ended up subclassing Symfony\Bundle\FrameworkBundle\Controller\Controller and introduced the method getEntityManager:
public function getEntityManager()
{
$em = $this->get('doctrine.orm.entity_manager');
static $utf8_set = false;
if (!$utf8_set) {
$em->getEventManager()->addEventSubscriber(new MysqlSessionInit('utf8','utf8_unicode_ci'));
$utf8_set = true;
}
return $em;
}
So everytime I am want to access the EntityManager or a repository in my controllers (which of course now subclass DoctrineController) I call
$this->getEntityManager()
resp.
$this->getEntityManager()->getRepository('What\Ever\Entity\I\Am\Looking\For')

Zend_Log in application.ini

is there any example how to setup an instance of zend log from application.ini? I have only found an example for logging to an file, but i want to log into an SQLITE database table?
Zend Log resource
Good question. I can't find a way to instantiate the Zend_Log_Writer_Db from a bootstrap config. The writer class requires a Zend_Db_Adapter object. It doesn't accept a string.
The ZF project needs to develop this use case further. They don't even have any unit tests for Zend_Application_Resource_Log that include a Db writer.
The best I can suggest until then is that you Bootstrap class needs to customize the Log resource in an _initLog() method.
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDb()
{
if ($this->hasPluginResource("db")) {
$r = $this->getPluginResource("db");
$db = $r->getDbAdapter();
Zend_Registry::set("db", $db);
}
}
protected function _initLog()
{
if ($this->hasPluginResource("log")) {
$r = $this->getPluginResource("log");
$log = $r->getLog();
$db = Zend_Registry::get("db");
$writer = new Zend_Log_Writer($db, "log", ...columnMap...);
$log->addWriter($writer);
Zend_Registry::set("log", $log);
}
}
}
Here in the manual: you can find an example how to write your log file into the database.Is that what you mean?
This should work - I will test fully later (not at my dev machine now)
Zend_Application_Resource_Log can setup an instance of a Zend_Log from application.ini
resources.log.writerName = "db"
resources.log.writerParams.db.adapter = "PDO_SQLITE"
resources.log.writerParams.db.dbname = APPLICATION_PATH "/../db/logdb.sqlite"
resources.log.writerParams.db.table = "log"
Since ZF 1.10alpha (at least), the following has been true.
// e.g. 1 - works
resources.log.firebug.writerName = "Firebug"
// e.g. 2 - fails
resources.log.writerName = "Firebug"
NOTE: the arbitrary array key 'firebug'. When the Zend_Log factory churns over the resource's log config, example 1 will be passed as an array to Zend_Log->addWriter() (triggering the _constructWriterFromConfig() method), whilst example 2 will simply pass a string (triggering an exception).
(I know this is old, and I'm using a Firebug logger example, but the same applies to all log writers)

Cannot instantiate a PDO object in PHP MVC framework

Can anyone help me with this?
I have this function in a singleton class. The error it is giving me back is that it cannot find the class.
First I thought it had something to do with the autoloader, but I did spl_autoload_unregister('libloader') and it still gives the same error?
The host is running php 5.
public static function getInstantie()
{
if (!self::$instantie)
{
$config = config::getInstantie();
$db_type = $config->config_waarden['database']['db_type'];
$hostnaam = $config->config_waarden['database']['db_hostnaam'];
$dbnaam = $config->config_waarden['database']['db_naam'];
$db_wachtwoord = $config->config_waarden['database']['db_wachtwoord'];
$db_gebruikersnaam = $config->config_waarden['database']['db_gebruikersnaam'];
$db_poort = $config->config_waarden['database']['db_poort'];
self::$instantie = new PDO("$db_type:host=$hostnaam;port=$db_poort;dbname=$dbnaam",$db_gebruikersnaam, $db_wachtwoord);
self::$instantie-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instantie;
}
thanks, Richard
PDO is enabled by default with a set of database drivers:
http://au.php.net/manual/en/pdo.installation.php
But the build of PHP you are working with could have it disabled. Autoloading will have no effect on whether or not the PDO class will be found.
Create a PHP info file and check to see if the PDO section exists. If it doesn't, then your issue is most likely because it wasn't built into your php installation.
is that function inside of a class that extends PDO? If not, can you try to make that function inside of a class that extends PDO and instead of self call the functions using keyword parent?

Categories