Skip to content

Instantly share code, notes, and snippets.

@dator-zz
Created December 29, 2010 12:17
Show Gist options
  • Save dator-zz/758477 to your computer and use it in GitHub Desktop.
Save dator-zz/758477 to your computer and use it in GitHub Desktop.
Custom user form for UserBundle
<?php
namespace Application\MyBundle\Document;
use Application\FOS\UserBundle\Model\User;
/**
* @mongodb:Document(
* collection="company",
* repositoryClass="Application\MyBundle\Document\CompanyRepository"
* )
*/
class Company
{
/**
* @var string
* @mongodb:Id(strategy="none")
*/
protected $id;
/**
* @var string
* @mongodb:Field(type="string")
*/
protected $name;
/**
* @mongodb:ReferenceMany(targetDocument="User")
*/
protected $users;
/**
* Set id
* @return null
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get id
*/
public function getId()
{
return $this->id;
}
/**
* Set name
* @return null
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*/
public function getName()
{
return $this->name;
}
/**
* Get users
*/
public function getUsers()
{
return $this->users;
}
}
I get an "Warning: spl_object_hash() expects parameter 1 to be object, array given in /Users/clement/Sites/myapp/src/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php line 603" when i submit the form? Any idea to get pass this ?
Thanks a lot :)
<?php
namespace Application\FOS\UserBundle\Document;
use Bundle\FOS\UserBundle\Document\User as BaseUser;
/**
* @mongodb:Document(
* repositoryClass="Application\FOS\UserBundle\Document\UserRepository",
* collection="user"
* )
*/
class User extends BaseUser
{
/**
* @mongodb:ReferenceOne(targetDocument="Application\MyBundle\Document\Company")
*/
protected $company;
/**
* @var string
* @mongodb:Field(type="string")
*/
protected $firstname;
/**
* @var string
* @mongodb:Field(type="string")
*/
protected $lastname;
public function getCompany()
{
return $this->company;
}
public function setCompany($company)
{
$this->company = $company;
}
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
public function getFirstname()
{
return $this->firstname;
}
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
public function getLastname()
{
return $this->lastname;
}
}
<?php
namespace Application\FOS\UserBundle\Form;
use Bundle\FOS\UserBundle\Form\UserForm as BaseUserForm;
use Symfony\Component\Form\TextField;
use Symfony\Component\Form\FieldGroup;
class UserForm extends BaseUserForm
{
public function configure()
{
parent::configure();
$this->add(new TextField('firstname'));
$this->add(new TextField('lastname'));
$group = new FieldGroup('company');
$group->add(new TextField('name'));
$this->add($group);
}
}
@docteurklein
Copy link

try to add a

public function __construct() 
{
    $this->users = new \Doctrine\Common\ArrayCollection;
}

in your Company class.

But if you have the full Xdebug stack trace, it could be cool.

@dator-zz
Copy link
Author

I tried, again the same problem,

i tried to add

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

in my custom User.php, no error, but the company is not saved when persist :s

Here is the symfony2 error page

Stack Trace
in /Users/clement/Sites/myapp/src/vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php line 58 »
}
if (error_reporting() & $level && $this->level & $level) {
throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line));
}
return false;
at ErrorHandler->handle('2', 'spl_object_hash() expects parameter 1 to be object, array given', '/Users/clement/Sites/myapp/src/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php', '603', array('class' => object(ClassMetadata), 'document' => object(User), 'oid' => '0000000042dae6930000000012763f46', 'actualData' => array('username' => 'hahaha', 'usernameLower' => 'hahaha', 'email' => 'dator@test.com', 'enabled' => true, 'algorithm' => 'sha512', 'salt' => 'dz2124b9mps0ko0k8sk4goos4co8wc8', 'password' => '586312ea60ca3a3a40379e7bed213a19b31eb42429e9863df0aa3ce8177b0300f4aa611e8d61f22d5213cbe4cd3bbd3dd5da01679be96a8a261fecb72ab89f01', 'createdAt' => object(DateTime), 'updatedAt' => object(DateTime), 'lastLogin' => null, 'locked' => false, 'expired' => false, 'expiresAt' => null, 'confirmationToken' => 'jdenufn44gg8ss0gcwss04sc8scw4go', 'roles' => array(), 'company' => array('name' => 'Tester'), 'firstname' => 'clem', 'lastname' => 'job'), 'isNewDocument' => true, 'changeSet' => array('username' => array(null, 'hahaha'), 'usernameLower' => array(null, 'hahaha'), 'email' => array(null, 'dator@test.com'), 'enabled' => array(null, true), 'algorithm' => array(null, 'sha512'), 'salt' => array(null, 'dz2124b9mps0ko0k8sk4goos4co8wc8'), 'password' => array(null, '586312ea60ca3a3a40379e7bed213a19b31eb42429e9863df0aa3ce8177b0300f4aa611e8d61f22d5213cbe4cd3bbd3dd5da01679be96a8a261fecb72ab89f01'), 'createdAt' => array(null, object(DateTime)), 'updatedAt' => array(null, object(DateTime)), 'lastLogin' => array(null, null), 'locked' => array(null, false), 'expired' => array(null, false), 'expiresAt' => array(null, null), 'confirmationToken' => array(null, 'jdenufn44gg8ss0gcwss04sc8scw4go'), 'roles' => array(null, array()), 'company' => array(null, array('name' => 'Tester')), 'firstname' => array(null, 'clem'), 'lastname' => array(null, 'job')), 'propName' => 'lastname', 'actualValue' => 'job', 'mapping' => array('fieldName' => 'company', 'type' => 'one', 'reference' => true, 'targetDocument' => 'Application\MyBundle\Document\Company', 'discriminatorField' => null, 'discriminatorMap' => null, 'name' => 'company', 'nullable' => false, 'options' => array(), 'value' => null, 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false, 'isCascadeCallbacks' => false, 'association' => '1'), 'value' => array('name' => 'Tester'), 'values' => array(array('name' => 'Tester')), 'obj' => array('name' => 'Tester')))
at spl_object_hash(array('name' => 'Tester'))
in /Users/clement/Sites/myapp/src/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php line 603 »
at UnitOfWork->computeChangeSet(object(ClassMetadata), object(User))
in /Users/clement/Sites/myapp/src/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php line 627 »
at UnitOfWork->computeChangeSets()
in /Users/clement/Sites/myapp/src/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php line 366 »
at UnitOfWork->commit(array())
in /Users/clement/Sites/myapp/src/vendor/doctrine-mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentManager.php line 495 »
at DocumentManager->flush()
in /Users/clement/Sites/myapp/src/Bundle/FOS/UserBundle/Controller/UserController.php line 349 »
at UserController->saveUser(object(User))
in /Users/clement/Sites/myapp/src/Application/FOS/UserBundle/Controller/UserController.php line 40 »
at UserController->createAction()
at call_user_func_array(array(object(UserController), 'createAction'), array())
in /Users/clement/Sites/myapp/app/cache/dev/classes-a430b.php line 1029 »
at HttpKernel->handleRaw(object(Request), '1')
in /Users/clement/Sites/myapp/app/cache/dev/classes-a430b.php line 1002 »
at HttpKernel->handle(object(Request), '1', true)
in /Users/clement/Sites/myapp/src/vendor/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 181 »
at Kernel->handle(object(Request))
in /Users/clement/Sites/myapp/web/index_dev.php line 14 »

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment