doctrine/mongodb-odm — Issue #267: References on references not working?
Author: madhatter42
Created: 2012-03-05 02:01:29+00:00
State: open

We have two classes, User and Company -- a user belongs to one company, and a company can have one or more users (see the following class definitions).

/**
* @ODM\Document(collection="users")
*/
class User extends Document
{
/**
* @ODM\ReferenceOne(name="company", targetDocument="Documents\Company", discriminatorMap={"seller"="Documents\SellerCompany", "buyer"="Documents\BuyerCompany"}, inversedBy="users")
* @var Document\Company
*/
protected $company;

public function __construct()
{
$this->flags = array();
$this->roles = array();
$this->creatives = array();
}

public function getCompany()
{
return $this->company;
}

public function setCompany(Company $company)
{
$this->company = $company;
}
}

/**
* @ODM\Document(collection="companies")
* @ODM\InheritanceType("SINGLE_COLLECTION")
* @ODM\DiscriminatorField(fieldName="type")
* @ODM\DiscriminatorMap({"seller"="Documents\SellerCompany", "buyer"="Documents\BuyerCompany"})
*/
class Company extends Document
{

/**
* @ODM\ReferenceMany(targetDocument="Documents\User", mappedBy="company")
* @var User
*/
protected $users;

/**
* Get users
* @return User
*/
public function getUsers($sort = false, $activeOnly = false)
{
if($sort) {
$users = array();
foreach($this->users as $user) {
if($activeOnly) {
if($user->isActive()) {
$users[] = $user;
}
} else {
$users[] = $user;
}
}
usort($users, array($this, "userNameCmp"));
return $users;
}

return $this->users;
}

private function userNameCmp($userA, $userB)
{
return strcasecmp($userA->getName(), $userB->getName());
}

}

Given a user object ($user), I'm making the following call:

$user->getCompany()->getUsers()

Which is returning an empty collection. The company that is returned by the getCompany() call is correct, but for some reason, the "nested reference" doesn't return expected results. Am I doing something wrong here, or is this a bug?