I have a problem Sonata Admin and storing a reference in a MongoDB document. I set up Sonata Admin correct (without reference it works well).
But if i try to store a user-reference for the cinema-document, i get the following error.
Error
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[user_id] = 5511710289d39769378b4567
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "user_id": The choice "5511710289d39769378b4567" does not exist or is not unique
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
The choice "5511710289d39769378b4567" does not exist or is not unique
Cinema.php
namespace Cinemato\AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Cinemato\UserBundle\Document\User as User;
/**
* #MongoDB\Document
*/
class Cinema
{
/**
*
* #var integer
* #MongoDB\Id
*/
private $id;
/**
*
* #var string
* #MongoDB\String
*/
private $title;
/**
* #MongoDB\ReferenceOne(targetDocument="Cinemato\UserBundle\Document\User")
*/
private $user_id;
/**
* Get id
*
* #return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* #param Cinemato\AppBundle\Document\User $userId
* #return self
*/
public function setUserId(\Cinemato\AppBundle\Document\User $userId)
{
$this->user_id = $userId;
return $this;
}
/**
* Get userId
*
* #return Cinemato\AppBundle\Document\User $userId
*/
public function getUserId()
{
return $this->user_id;
}
}
To solve the problem the CinemaAdmin.php must be edited - sonata_type_model_list must be added in configureFormFields.
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('user_id', 'sonata_type_model_list')
;
}
Related
there is the situation :
I would like to send information to my REST API (working on Symfony), but when i tried to send this line, i got some errors that i don't understand, like : This one .
I really tried to resolve it alone, but nothing helped me on forums.
I think about an attribute than can't probably be set to editiion, or the reverse : an attribute which is not in edition and has to be.
This is what i want : be able to modify "dateCommande" (attribute for Purchase Date) , "statut" (used for see the statement of the purchase) and "idUsager" (used for put an id for a user); and finally be able to create automatically an id ("$id").
Thanks beforehand to help me ! ^^
Code of my class "Categorie.php" :
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Commande
*
* #ORM\Table(name="commande", indexes={#ORM\Index(name="IDX_6EEAA67DCF034CDB", columns={"id_usager_id"})})
* #ORM\Entity
* #ApiResource(normalizationContext={"groups"={"commande"},"enable_max_depth"=true},denormalizationContext={"groups"={"commandeecriture"},"enable_max_depth"=true})
*/
class Commande
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #Groups({"commande"})
* #NotNull(message="L'id ne peut être null")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="date_commande", type="date", nullable=false)
* #Groups({"commande","commandeecriture"})
* #NotBlank(message="Date non renseignée")
*/
private $dateCommande;
/**
* #var string
*
* #ORM\Column(name="statut", type="string", length=255, nullable=false)
* #Groups({"commande","commandeecriture"})
* #NotBlank(message="Statut non renseigné")
*/
private $statut;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_usager_id", referencedColumnName="id")
* })
* #Groups({"commande", "commandeecriture"})
* #NotNull(message="L'id ne peut être null")
*/
private $idUsager;
/**
* Constructor
*/
public function __construct()
{
$this->idArticle = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* #return \DateTime
*/
public function getDateCommande(): \DateTime
{
return $this->dateCommande;
}
/**
* #param \DateTime $dateCommande
*/
public function setDateCommande(\DateTime $dateCommande): void
{
$this->dateCommande = $dateCommande;
}
/**
* #return string
*/
public function getStatut(): string
{
return $this->statut;
}
/**
* #param string $statut
*/
public function setStatut(string $statut): void
{
$this->statut = $statut;
}
/**
* #return User
*/
public function getIdUsager(): User
{
return $this->idUsager;
}
/**
* #param User $idUsager
*/
public function setIdUsager(User $idUsager): void
{
$this->idUsager = $idUsager;
}
}
api-platform uses IRI instead of entity ids, try to replace idUsager="1" by idUsager="/usager/1". I'm guessing usager is the name of the related entity but if it's not then you just need to replace the actual name in the snippet in general it must be /related-entity-name/id
I have a simple symfony 2 setup with Doctrine ORM and a db with some underscore seperated field names (for instance "error_page"). Querying this never gives a result (getTitle does give a result, getErrorPage is always empty) and symfony gives me an error:
Method "error_page" for object "My\CmsBundle\Document\Website" does not exist in MyCmsBundle:Default:dashboard.html.twig at line 5
I can't figure out why... My Document looks like this:
<?php
// src/My/CmsBundle/Document/Website.php
namespace My\CmsBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* #MongoDB\Document(
* collection="websites"
* )
*/
class Website
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
*/
protected $slug;
/**
* #MongoDB\Field(type="string", name="error_page")
*/
protected $error_page = "";
/**
* #MongoDB\String
*/
protected $title;
/**
* #MongoDB\String(name="seo_title")
*/
protected $seo_title;
/**
* #MongoDB\String
*/
protected $seo_description;
/**
* #MongoDB\Collection
*/
protected $url = array();
/**
* Get id
*
* #return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set slug
*
* #param string $slug
* #return self
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string $slug
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set title
*
* #param string $title
* #return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string $title
*/
public function getTitle()
{
return $this->title;
}
/**
* Set errorPage
*
* #param string $errorPage
* #return self
*/
public function setErrorPage($errorPage)
{
$this->error_page = $errorPage;
return $this;
}
/**
* Get errorPage
*
* #return string $errorPage
*/
public function getErrorPage()
{
return $this->error_page;
}
/**
* Set url
*
* #param collection $url
* #return self
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return collection $url
*/
public function getUrl()
{
return $this->url;
}
/**
* Set seoTitle
*
* #param string $seoTitle
* #return self
*/
public function setSeoTitle($seoTitle)
{
$this->seo_title = $seoTitle;
return $this;
}
/**
* Get seoTitle
*
* #return string $seoTitle
*/
public function getSeoTitle()
{
return $this->seo_title;
}
/**
* Set seoDescription
*
* #param string $seoDescription
* #return self
*/
public function setSeoDescription($seoDescription)
{
$this->seo_description = $seoDescription;
return $this;
}
/**
* Get seoDescription
*
* #return string $seoDescription
*/
public function getSeoDescription()
{
return $this->seo_description;
}
}
Document creation via this document works fine by the way. The field name is also set to error_page as expected... I'm at a loss here :S
Actually, Doctrine+Symfony2 assume camel case variable naming. Twig using the getter method names should be obvious, how should it access protected/private variables? It needs a name for something public : the getter. You're probably wondering why "get" is ignored; it is a simplification for designers as they normally shouldnt know about what "getters" are and the difference between methods and variables.
so in your twig file ,change :
{{document.error_page}}
to
{{document.errorPage}}
this would helpful.
I want to create a form for a simple entry management.
I got the entities Entry, EntryUser, and User.
Tables in database: entry, entry_user and user.
I think i am close to be successful, but i got some problems here.
In my form, the author can check the users he want to add to the entry via checkboxes. Symfony/Doctrine should do the relation work for me and add rows into entry_user. One row for one selected checkbox.
Entry.php
/**
* #ORM\Entity
* #ORM\Table(name="entry")
*/
class Entry {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="EntryUser", mappedBy="entry", cascade={"all"}, orphanRemoval=true)
*/
protected $users;
function __construct() {
$this->users = new ArrayCollection();
}
/**
* Add user
*
* #param User $user
* #return $this
*
*/
public function addUser(User $user)
{
if (!$this->users->contains($user)) {
$this->users->add($user);
}
return $this;
}
/**
* Remove user
*
* #param User $user
* #return $this
*/
public function removeUser(User $user)
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
}
return $this;
}
/**
* #return array
*/
public function getUsers()
{
return $this->users->toArray();
}
/**
* Returns the true ArrayCollection of Users.
* #return Doctrine\Common\Collections\ArrayCollection
*/
public function getUsersCollection()
{
return $this->users;
}
}
User.php
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User implements AdvancedUserInterface, EquatableInterface, \Serializable {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*/
protected $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
// Many other methods for user sign in, roles and so on...
}
EntryUser.php
/**
* #ORM\Entity
* #ORM\Table(name="entry_user")
*/
class EntryUser {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer
*
* #ORM\Column(name="user_id", type="integer", nullable=true)
*/
protected $user_id;
/**
* #var integer
*
* #ORM\Column(name="entry_id", type="integer", nullable=true)
*/
protected $entry_id;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User", cascade={"persist"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $user;
/**
* #var Entry
*
* #ORM\ManyToOne(targetEntity="Entry", inversedBy="users")
* #ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $entry;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user_id
*
* #param integer $userId
* #return EntryUser
*/
public function setUserId($userId)
{
$this->user_id = $userId;
return $this;
}
/**
* Get user_id
*
* #return integer
*/
public function getUserId()
{
return $this->user_id;
}
/**
* Set entry_id
*
* #param integer $entryId
* #return EntryUser
*/
public function setEntryId($entryId)
{
$this->entry_id = $entryId;
return $this;
}
/**
* Get entry_id
*
* #return integer
*/
public function getEntryId()
{
return $this->entry_id;
}
/**
* Set user
*
* #param User $user
* #return EntryUser
*/
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return User
*/
public function getUser()
{
return $this->user;
}
/**
* Set entry
*
* #param Entry $entry
* #return EntryUser
*/
public function setEntry(Entry $entry = null)
{
$this->entry = $entry;
return $this;
}
/**
* Get entry
*
* #return Entry
*/
public function getEntry()
{
return $this->entry;
}
}
I use ManyToOne relationships here because i want to use a file named EntryUser.php to add some custom fields later. I need this because i must store some additional data there in entry_user.
Because:
And it's the right thing to do. Create a new entity with the new fields, and if you need it, create a custom repository to add the methods you need.
A <--- Many to many with field ---> B
would become
A --One to many--> C (with new fields) <-- One to many--B
and of course, C has ManyToOne relationships with both A and B.
See the comments in: ManyToMany relationship with extra fields in symfony2 orm doctrine
My EntryType.php form definition includes the following, to create the checkboxes for the template:
$builder->add('users', 'entity', array(
'class' => 'MyCompMyAppBundle:User',
'multiple' => true,
'expanded' => true,
'property' => 'name',
'label' => 'Freunde',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')->select('a')
->from('MyComp\MyAppBundle\Entity\User', 'a')
->where('EXISTS (
SELECT b
FROM MyComp\MyAppBundle\Entity\UserFriend b
WHERE b.created_by = :my_user_id
AND b.friend_user_id = a.id
)')
->andWhere('EXISTS (
SELECT c
FROM MyComp\MyAppBundle\Entity\UserFriend c
WHERE c.created_by = a.id
AND c.friend_user_id = :my_user_id
)')
->setParameter('my_user_id', $this->user->getId());
},
'required' => true,
));
As you can see, i load User objects here for the form field (type: entity).
UserFriend is another entity (table: user_friend). A friend list is saved there. Here all the friends gets loaded. They will be shows as the checkboxes.
Now, if i go to my form in my browser, and check some users for the entry und if i submit the form, i get this error:
ORMException: Found entity of type MyComp\MyAppBundle\Entity\User on association MyComp\MyAppBundle\Entity\Entry#friends, but expecting MyComp\MyAppBundle\Entity\EntryUser
So it is very confusing.
How can i make this work?
How can i make symfony and doctrine work to insert data automatically into entry and entry_user?
Important: I want to make it possible that the author can edit the entry later. So the checkboxes should be selected by default as it is saved in entry_user.
I just try to understand this and got a little bit confused. But i dont know how to make this work.
I found an awesome solution in the web. I was searching for many days now, but this guy made my day! :-)
You can read how it works and learn from an awesome example here!
Happy coding!
For this case you should check "form collections" in Symfony: http://symfony.com/doc/current/cookbook/form/form_collections.html
With this technique you will add a form type to crate a single "EntryUser" and after that you can add a collection of that form to the parent form. Quite easy and well explained in the liked article.
i am using zfcuser, bjyauthorize et roleuserbridge.
i want to add a field in the form registration. I followed this tutorial step :
http://resoftsol.com/adding-custom-fields-to-zfcuser-register-form/
in the module front i have added :
- the directory entity with files user et userinterface:
namespace Front\Entity;
interface UserInterface
{
/**
* Get id.
*
* #return int
*/
public function getId();
/**
* Set id.
*
* #param int $id
* #return UserInterface
*/
public function setId($id);
/**
* Get username.
*
* #return string
*/
public function getUsername();
/**
* Set username.
*
* #param string $username
* #return UserInterface
*/
public function setUsername($username);
/**
* Get email.
*
* #return string
*/
public function getEmail();
/**
* Set email.
*
* #param string $email
* #return UserInterface
*/
public function setEmail($email);
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName();
/**
* Set displayName.
*
* #param string $displayName
* #return UserInterface
*/
public function setDisplayName($displayName);
/**
* Get password.
*
* #return string password
*/
public function getPassword();
/**
* Set password.
*
* #param string $password
* #return UserInterface
*/
public function setPassword($password);
/**
* Get state.
*
* #return int
*/
public function getState();
/**
* Set state.
*
* #param int $state
* #return UserInterface
*/
public function setState($state);
/**
* Get role.
*
* #return string
*/
public function getRole();
/**
* Set role.
*
* #param string $role
* #return UserInterface
*/
public function setRole($role);
}
++++++++++++++++++++
namespace Font\Entity;
class User implements UserInterface
{
/**
* #var int
*/
protected $id;
/**
* #var string
*/
protected $username;
/**
* #var string
*/
protected $email;
/**
* #var string
*/
protected $displayName;
/**
* #var string
*/
protected $password;
/**
* #var int
*/
protected $state;
/**
* #var string
*/
protected $role;
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param int $id
* #return UserInterface
*/
public function setId($id)
{
$this->id = (int) $id;
return $this;
}
/**
* Get username.
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set username.
*
* #param string $username
* #return UserInterface
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get email.
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email.
*
* #param string $email
* #return UserInterface
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* Set displayName.
*
* #param string $displayName
* #return UserInterface
*/
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
return $this;
}
/**
* Get password.
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set password.
*
* #param string $password
* #return UserInterface
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get state.
*
* #return int
*/
public function getState()
{
return $this->state;
}
/**
* Set state.
*
* #param int $state
* #return UserInterface
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get role.
*
* #return string
*/
public function getRole()
{
return $this->role;
}
/**
* Set role.
*
* #param string $role
* #return UserInterface
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
}
++++++++++++++++++++++++++
also i have add the mapp directory.
i had the following error :
Catchable fatal error: Argument 1 passed to ZfcUser\Validator\AbstractRecord::setMapper()
must be an instance of ZfcUser\Mapper\UserInterface, instance of Front\Mapper\User given,
called in C:\wamppp\www\projet\vendor\zendframework\zendframework\library\Zend\Validator
\AbstractValidator.php on line 139 and defined in C:\wamppp\www\projet\vendor\zf-commons
\zfc-user\src\ZfcUser\Validator\AbstractRecord.php on line 65
I just had the problem myself and was able to solve it... finally.
Copy the ZfcUser\Validator folder into your module.
Adapt the namespace and change the expected Object type of the method AbstractRecord::setMapper to your user entity / interface, whatever you have there now.
Also, in the code you provided the namespaces arent identical. Yout got "Front" and "Font" there.
Edit: forgot the important part xD
After you have done that you need the following code in the config file (my module is called User):
'zfcuser_register_form' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$form = new ZfcUser\Form\Register(null, $options);
//$form->setCaptchaElement($sm->get('zfcuser_captcha_element'));
$form->setInputFilter(new ZfcUser\Form\RegisterFilter(
new User\Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'email'
)),
new User\Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'username'
)),
$options
));
return $form;
},
I hope this helps.
You should not create your own interface(and if you do, your new interface should extend ZfcUser\Entity\UserInterface), instead just make your user entity extend the ZfcUser\Entity\UserInterface.
I have a symfony2 Form CategoryType with a buildForm:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('language', 'entity', array(
'class' => 'Evr\HomeBundle\Entity\Language',
'property' => 'language'
)
)
->add('category', 'text', array('label' => 'category.category', 'required' => true));
}
As you can expect, I have two entities Category and Language, of which Category is a child of Language (One language can have many categories, and one category belongs to 1 or 0 language)
Category
<?php
namespace Evr\HomeBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* #ORM\Table(name="ev_category")
* #ORM\Entity
*/
class Category
{
/**
* #var integer
*
* #ORM\Column(name="category_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\ManyToOne(targetEntity="Language",inversedBy="categories")
* #ORM\JoinColumn(name="language_id",referencedColumnName="language_id")
*/
private $language;
/**
* #var string
*
* #ORM\Column(name="category", type="string", length=255)
*/
private $category;
/**
* #ORM\OneToMany(targetEntity="Subcategory", mappedBy="category")
*/
protected $subcategories;
public function __construct(){
$this->subcategories=new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set language
*
* #param integer $language
* #return Category
*/
public function setLanguage($language) {
$this->language = $language;
return $this;
}
/**
* Get language
*
* #return integer
*/
public function getLanguage() {
return $this->language;
}
/**
* Set category
*
* #param \string $category
* #return Category
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \string
*/
public function getCategory()
{
return $this->category;
}
}
Language
<?php
namespace Evr\HomeBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Language
*
* #ORM\Table(name="ev_language")
* #ORM\Entity
*/
class Language
{
/**
* #var integer
*
* #ORM\Column(name="language_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="language", type="string", length=128)
*/
private $language;
/**
* #var string
*
* #ORM\Column(name="code", type="string", length=10)
*/
private $code;
/**
* #var boolean
*
* #ORM\Column(name="direction", type="boolean")
*/
private $direction;
/**
* #ORM\OneToMany(targetEntity="Category", mappedBy="language")
*/
protected $categories;
/**
* #ORM\OneToMany(targetEntity="Country", mappedBy="language")
*/
protected $countries;
public function __construct(){
$this->categories=new ArrayCollection();
$this->countries=new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set language
*
* #param string $language
* #return Language
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* #return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Set code
*
* #param string $code
* #return Language
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* #return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set direction
*
* #param boolean $direction
* #return Language
*/
public function setDirection($direction)
{
$this->direction = $direction;
return $this;
}
/**
* Get direction
*
* #return boolean
*/
public function getDirection()
{
return $this->direction;
}
}
When editing a category, I need to display the current values in a form, so that the user can modify them and save.
Here I have a controller editAction(), whose mission is to display the edition form:
public function editAction($id) { //id of the category to modify
$category = $this->getDoctrine()->getRepository('EvrHomeBundle:Category')->find($id); //return the category with id
$categoryForm = $this->createForm(new CategoryType(),$category); //building the form
return $this->render('EvrAdminBundle:Categories:edit.html.twig', array('categoryForm' => $categoryForm->createView(), 'category' => $category));
}//render the form
Remember that the CategoryType has an element which type : entity, which loads the languages in A select box.
But when trying to populate the form CategoryType with the current data (Category and Language) , Symfony returns an Error : Entity Not Found
Symfony doesn't specify in which line the error occures, but I think it's around this line :
$categoryForm = $this->createForm(new CategoryType(),$category); //building the form
Because when I remove the second argument of createForm : $category, it displays an empty form (just like an add category form
Is there a solution for this issue? And how can I create a form with current data from the database, considering the fact that it contains an entity element.
Thank you
If you have a database without referential integrity enforced, then you can delete a record even though another record is pointing to it (they should be linked through a foreign key). Then when you try to grab the record it seem to work but when you want to access attributes of that entity (this is when the real access to the database occurs) it can't find the entity with that record_id and so "Entity not found".
Ex:
$user=new User();
$car=$user->getCar(); /*The user had a CAR record assigned with Id = 1 but the record was
deleted.
The record USER still has the id of 1.*/
/*No error up till here*/
$carName=$car->getName(); /*Error occurs because it tries to grab the record with id of 1
which was deleted*/
Try to use \ in front of entity namespace:
'class' => '\Evr\HomeBundle\Entity\Language',