Doctrine Mongodb references() - mongodb

Is it possible with doctrine mongodb createquerybuilder() to add multiple references to a document ?
Here's an example of what I want to do:
I have 2 collections : Users and Movements in a 1:n relation so a User has multiple movements and a movement refers to a user.
To get the movements from a user, I can do
$user->getMovements();
I can also call doctrine createQueryBuilder like this:
$query->createQueryBuilder('Movement');
$query->field('user')->references($user);
Both give me the expected results. But what if I want to fetch the movement of 2 or 3 users in one query ?
Is it possible to do something like (which I tried but did not work)
$q->field('user')->references($user1);
$q->field('user')->references($user2);
// etc.
I stuck with that kind of query. Thanks for help !
Colzak.

Ok, So I found a solution that may not be the best one but it works.
Instead of doing
$q->field('user')->references($user);
You can do
$->field('user.$id')->equals(new \MongoId($user->getId());
So if you have an array of user, you can do something like
$userIds = array();
foreach ($users as $user) {
$userIds[] = new \MongdoId($user->getId());
}
And then the query:
$q->field('user.$id')->in($userIds);
Hope it'll help someone !

Related

Dynamic query in EF

I have already searched in other questions for a solution, but didn't find it.
So, my problem is the following:
I have a page where the user can mount an expression. For example, if they want some professors with course 1 and course 2 then, they create an expression like this:
(course 1 AND course 2) in the page.
But when I use EF, if I put the "AND", I get no professor... if I change to "OR" I get some professors with 1 or 2 and maybe one of then have two courses.
I need the professors who have always the two courses (course 1 AND course 2)
How can I accomplish this?
(If my explanation get too confusing, let me know, I'll try in a other way!)
I tried to understand your explanation, Try something as following and let usknow if is the logic you are looking for? or you want something other result.
from x in db.professors.Where(x => x. professorId == professorId && (x.courseid == 'course1' && x.ukat == 'course2'))
Maybe:
var result = db.Professors.Where(p =>
p.Cursos.Count(c => searchedCourses.Contains(c.CourseId)) == searchedCourses.Count());
This way you get all the professors, filter their courses to match that in the specified search, and get only the professors with the same amount of filtered courses and the searched ones.

Retrieving two records with the same name

I have to get two different names pulled from this query, the Account Name and the Opportunity Name. Both are called "Name" under each object.
I am able to use a query to retrieve both of them, but I am unable to decipher between the two in order to actually echo/print or use them.
My Query is:
$query = "SELECT Name,Opportunity.Account.Name from Opportunity ";
$response = $mySforceConnection->query($query);
foreach ($response->records as $record) {
echo $record->Name ."<br/>\n";
//echo $record->Opportunity.Account.Name ."<br/>\n";
echo "<br/>\n"; }
The only Name that is displayed is the opportunity Name (when trying different methods, I know the code above will only echo that)
I have made two seperate queries one from account and one from opportunity to ensure both are infact different things, and they are.
I have attempted to echo two "Name" records, both are just the opportunity name it doesn't recognize the account name.
And obviously what is commented out above as "Opportunity.Account.Name" isn't echoing the result, i am recieving an error instead.
I know using an alias is not supported in salesforce so that obviously didn't and won't work, by that I mean trying to do this:
Select Name as OppName
I am unable to find a different way to echo the records, I have done a lot of googling on the subject. Any help would be appreciated or a point in the right direction.
In my schema there is no Name field in the Opportunity object. There is Account Name and it is a lookup on Account Name in the Account object. You can view the schema builder by going to Setup > Schema Builder (it is under App Setup).
To be honest, I don't know why this worked. I'm new to SOAP and Salesforce, so I was just trying different things to get it working. But if anyone else has this problem, this is how I fixed it.
I displayed the account name by using this to echo it:
echo $record->Account->Name ;
The query is still the same as in the question.

How to compare 2 mongodb collections?

Im trying to 'compare' all documents between 2 collections, which will return true only and if only all documents inside 2 collections are exactly equal.
I've been searching for the methods on the collection, but couldnt find one that can do this.
I experimented something like these in the mongo shell, but not working as i expected :
db.test1 == db.test2
or
db.test1.to_json() == db.test2.to_json()
Please share your thoughts ! Thank you.
You can try using mongodb eval combined with your custom equals function, something like this.
Your methods don't work because in the first case you are comparing object references, which are not the same. In the second case, there is no guarantee that to_json will generate the same string even for the objects that are the same.
Instead, try something like this:
var compareCollections = function(){
db.test1.find().forEach(function(obj1){
db.test2.find({/*if you know some properties, you can put them here...if don't, leave this empty*/}).forEach(function(obj2){
var equals = function(o1, o2){
// here goes some compare code...modified from the SO link you have in the answer.
};
if(equals(ob1, obj2)){
// Do what you want to do
}
});
});
};
db.eval(compareCollections);
With db.eval you ensure that code will be executed on the database server side, without fetching collections to the client.

Zend db table find just like fetchRow

I´m using find() to retrieve a value from the database, but It returns an array with the objects, I would like that it return to me just the object like fetchRow returns, is there any change or similar thing to do?
Thanks, and best regard´s.
Well, It was so simple, for those that are in doubt, the solution is:
Zend_Loader::loadClass('News');
$db = new News();
$row = $db->find($id)->current();
That´s it, thanks.

Symfony: Model Translation + Nested Set

I'm using Symfony 1.2 with Doctrine. I have a Place model with translations in two languages. This Place model has also a nested set behaviour.
I'm having problems now creating a new place that belongs to another node. I've tried two options but both of them fail:
1 option
$this->mergeForm(new PlaceTranslationForm($this->object->Translation[$lang->getCurrentCulture()]));
If I merge the form, what happens is that the value of the place_id field id an array. I suppose is because it is waiting a real object with an id. If I try to set place_id='' there is another error.
2 option
$this->mergeI18n(array($lang->getCurrentCulture()));
public function mergeI18n($cultures, $decorator = null)
{
if (!$this->isI18n())
{
throw new sfException(sprintf('The model "%s" is not internationalized.', $this->getModelName()));
}
$class = $this->getI18nFormClass();
foreach ($cultures as $culture)
{
$i18nObject = $this->object->Translation[$culture];
$i18n = new $class($i18nObject);
unset($i18n['id']);
$i18n->widgetSchema['lang'] = new sfWidgetFormInputHidden();
$this->mergeForm($i18n); // pass $culture too
}
}
Now the error is:
Couldn't hydrate. Found non-unique key mapping named 'lang'.
Looking at the sql, the id is not defined; so it can't be a duplicate record (I have a unique key (id, lang))
Any idea of what can be happening?
thanks!
It looks like the issues you are having are related to embedding forms within each other, which can be tricky. You will likely need to do things in the updateObject/bind methods of the parent form to get it to pass its values correctly to its child forms.
This article is worth a read:
http://www.blogs.uni-osnabrueck.de/rotapken/2009/03/13/symfony-merge-embedded-form/comment-page-1/
It gives some good info on how embedding (and mergeing) forms work. The technique the article uses will probably work for you, but I've not used I18n in sf before, so it may well be that there is a more elegant solution built in?