I am new to Symfony2 and can't to figure out why I'm getting this error. Perhaps there is something wrong with my Entities? So I need some help with it. An error:
EntityManager#persist() expects parameter 1 to be an entity object, array given.
500 Internal Server Error - ORMInvalidArgumentException
I have the following code:
User.php:
<?php
namespace Acme\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*/
class User
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $password;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
}
UserRegType.php
<?php
namespace Acme\MainBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Length;
class UserRegType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('username', 'text', array(
'constraints'=>new Length(array('min' => 3))
));
$builder->add('password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
'constraints' => new Length(array('min' => 5, 'max' => 8))
));
}
public function getDefaultOptions(array $options)
{
return array('data_class' => 'Acme\MainBundle\Entity\User');
}
public function getName() {
return 'user';
}
}
?>
UserController.php:
<?php
namespace Acme\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\MainBundle\Form\Type\UserRegType;
class UserController extends Controller
{
public function registerAction()
{
$form = $this->createForm(new UserRegType());
return $this->render(
'AcmeMainBundle:User:register.html.twig',
array('form' => $form->createView())
);
}
public function createUserAction()
{
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new UserRegType());
$form->bind($this->getRequest());
$username = $form["username"]->getData();
if ($form->isValid()) {
$reg = $form->getData();
$em->persist($reg);
$em->flush();
$session = $this->get('session');
$session->set('username', $username);
return $this->redirect($this->generateUrl('home'));
}
}
public function loginAction()
{
}
public function logoutAction()
{
}
}