I usually select a collection by using the following method...
assuming the collection name is "fantastic" in a database called "somedb"
$conn = new Mongo();
$fantastic_coll = $conn->somedb->fantastic;
This has worked famously for me for a long time. The number of collections I am using has grown a lot and I'm trying to use dots in the collection names to organise them a little more logically.
eg.
store.items
store.categories
store.coupons
events
events.categories
This seems to work fine in the mongodb shell, but not in php?
if I try....
$conn = new Mongo();
$store_coupons_coll = $conn->somedb->store.coupons;
and then try to save documents into the collection it doesn't like me.
if I instead use...
$conn = new Mongo();
$store_coupons_coll = $conn->somedb->selectCollection('store.coupons');
everything works as expected.
Is this the right way to do it?
If so then I hope this helps anyone having the same trouble.
If not then is there a short way to write the collection name?
Is using dots in the collection name for organisation wrong to begin with?
The preferred way of doing this is with:
$collection = $conn->somedb->selectCollection( 'store.coupons' );
Or if you want:
$collection = $conn->selectCollection( 'somedb', 'store.coupons' );
Related
I am using Propel ORM and I set everything that must be from Propel Documentation. I have tables and when i echo the result from some Table row the result is just NULL, and NULL for everything.
Of course these tables/rows are not empty. It works fine with standart query.
The problem is that there are not errors, too and thats why i can't find the solution and I can'tt explain the problem, like I want.
I am new with Propel and want to use it. Please, if there is someone with expirience to help me.
I am using MySQL.
The code is just standart:
// setup the autoloading
require_once '../vendor/autoload.php';
// setup Propel
require_once '../vendor/bin/generated-conf/config.php';
$author = new Authors();
echo '<pre>';
var_dump($author);
echo '</pre>';
The table is not empty.
http://propelorm.org/Propel/documentation/08-logging.html
You will more info about errors from logs.
$author = new Authors();
Is not retrieving all of the rows in Authors (is the table named author or authors?). For that, you need to use a query:
$q = \AuthorsQuery::create();
$authors = $q->find();
foreach ($authors as $author) {
var_dump($author->toArray());
}
I have common model, which is generated by gii.
3 columns in mySql: id (int, A_I), setting(tinytext, null) and value(tinitext, null).
After this:
$cfg = new Config();
$cfg->setting = "sdd";
$cfg->value = 'dsf';
$cfg->save();
This effect I get even if create absolutely clear, new table and model.
This code runs in defaultAction.
Yii 1.1.4
PHP 5.5
MySQL 5.6.12
Help me, I'm tired search this bug =)
You said that the code runs in defaultAction. Is possible that you are calling it also in some other action. This way, the code runs twice.
I am trying to clone an entity-object to a different table in Symfony 2 / Doctrine. Any idea how to do this?
After retrieving the object from the database I can clone it like this:
$newobject = clone $oldbject;
This gives me a new object, which I can persist as a new record to the same table in the database. Actually I dont want to do this. I want to store the object as it is to a different table in the database. But to do this, I would have to change the parent entity, right? How to achieve this?
But then you're not really cloning an entity. In fact, you want a different entity. What do the two entities look like? Do they have the same fields? You could do something like this:
$oldEntity = $oldEntity;
$newEntity = new NewEntity();
$oldReflection = new \ReflectionObject($oldEntity);
$newReflection = new \ReflectionObject($newEntity);
foreach ($oldReflection->getProperties() as $property) {
if ($newReflection->hasProperty($property->getName())) {
$newProperty = $newReflection->getProperty($property->getName());
$newProperty->setAccessible(true);
$newProperty->setValue($newEntity, $property->getValue($oldEntity));
}
}
This is untested - and may have an error or two, but this should allow all properties to be copied from one object to another (assuming the properties have the same name on both objects).
I'm trying to do some SQL queries using Zend adapter. The code that I'm trying to use is something like this:
$result = $this->$db->getConnection()->exec('CREATE TABLE TEST');
and I know that $db is set and works properly, because I can run other commands such as
$this->$db->listTables(); or
$result = $this->$db->fetchAssoc("SHOW COLUMNS FROM " .$schema);
As I was reading through Zend documentation, it was mentioned that some database transactions that are not prepared should be used through the first example (e.g. exec("...") ), but apparently I have problems running those.
Any thoughts?
This should work ...
$db = Zend_Db_Table::getDefaultAdapter();
$db->query('CREATE TABLE wolf (tag VARCHAR(9))');
The only way I found to do this is:
$mongo->selectDB('new_db')->createCollection('tmp_collection');
$mongo->selectDB('new_db')->dropCollection('tmp_collection');
Doing just $mongo->selectDB('new_db') actually doesn't work.
Got any idea?
You'll need to run at least one command on the Database before it is created ...
This command can be run before you add any Collections ... so you can merely list (the nonexistent) Collections.
<?php
$connection = new Mongo();
$db = $connection->foo;
$list = $db->listCollections();
foreach ($list as $collection) {
echo "$collection </br>";
}
?>
Your new Database should now exist, with no user Collections created yet.
Technically, you don't need to manually create databases or collections in MongoDB due to its schemaless "lazy" way of creating databases and collections.
I understand if you're coming from an SQL world this doesn't make much sense. You may want to ask yourself though, "If it automatically creates a collection or database for me on the fly, is there really a need to define it ahead of time?"