cypher86 Posted March 25, 2013 Share Posted March 25, 2013 hi all,i'm absolutely new to symfony.i'm trying to create a form for class "Causale" which has in it a combobox refering a "Nominativo".i created a "Nominativo" repository and i'm trying to get all the value on the referenced table in order to populate the combobox on the "Causale" form. CODE: SELECT ALL class CausaleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $nominativi=new NominativoRepository(); $builder->add('data','date',array('widget' => 'single_text', 'format' => 'yyyy-MM-dd')); $builder->add('descrizione'); $builder->add('onorario','number'); $builder->add('spese','number'); $builder->add('rimborsi','number'); $builder->add('nominativo','choice', $nominativi=getNominativi(null)); $builder->add('tipoCausale','choice',array('choices' => array('0' => 'Fattura', '1' => 'Causale'), 'required' => true)); } public function getName() { return 'causale'; } } CODE: SELECT ALL class NominativoRepository extends EntityRepository { public function getNominativi($limit = null) { $qb = $this->createQueryBuilder('b') ->select('b') ->addOrderBy('b.nominativo', 'ASC'); if (false === is_null($limit)) $qb->setMaxResults($limit); return $qb->getQuery() ->getResult(); } }unfortunately i get this error CODE: SELECT ALL Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::__construct(), calledwhere am i wrong?thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/ Share on other sites More sharing options...
gristoi Posted March 25, 2013 Share Posted March 25, 2013 you should be using the entity type for this: $builder->add('nominativo', 'entity', array( 'class' => 'YourBundlenameHere:Nominativo')) This will automatically pull in all of your options Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1420918 Share on other sites More sharing options...
cypher86 Posted March 31, 2013 Author Share Posted March 31, 2013 (edited) hello, as you said i added an entity object as follow: $builder->add('data','date',array('widget' => 'single_text', 'format' => 'yyyy-MM-dd')); $builder->add('descrizione'); $builder->add('onorario','number'); $builder->add('spese','number'); $builder->add('rimborsi','number'); $builder->add('nominativo','entity', array('class' => 'FatturazioneFatturazioneBundle:Nominativo:getNominativi')); $builder->add('tipoCausale','choice',array('choices' => array('0' => 'Fattura', '1' => 'Causale'), 'required' => true)); but i get this error: A "__toString()" method was not found on the objects of type "Fatturazione\FatturazioneBundle\Entity\Nominativo" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path. Edited March 31, 2013 by cypher86 Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422043 Share on other sites More sharing options...
cypher86 Posted March 31, 2013 Author Share Posted March 31, 2013 (edited) i edited as follows: $builder->add('nominativo','entity', array('class' => 'Fatturazione\FatturazioneBundle\Entity\Repository\NominativoRepository', 'property' => 'getNominativi')); the class NominativoRepository: <?php namespace Fatturazione\FatturazioneBundle\Entity\Repository; use Doctrine\ORM\EntityRepository; class NominativoRepository extends EntityRepository { public function getNominativi($limit = null) { $qb = $this->createQueryBuilder('b') ->select('b.id, b.nominativo') ->addOrderBy('b.nominativo', 'ASC'); if (false === is_null($limit)) $qb->setMaxResults($limit); return $qb->getQuery() ->getResult(); } } ?> but i get the following error Class "Fatturazione\FatturazioneBundle\Entity\Repository\NominativoRepository" seems not to be a managed Doctrine entity. Did you forget to map it? Edited March 31, 2013 by cypher86 Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422045 Share on other sites More sharing options...
gristoi Posted March 31, 2013 Share Posted March 31, 2013 Your close. You need to read the documentation though. You are trying to populate the dropdown from the repository, this is incorrect, that is why you are getting the not mapped error. Any entity that you use in a form has to have a __toString method in it , this allows the form builder to map the data accordingly. The value returned by the _toString method needs to be whatever you want to show in the dropdown,i.e: $builder->add('nominativo','entity', array('class' => 'FatturazioneFatturazioneBundle:Nominativo')); // then in your Nominativo Entity public function __toString(){ //if your entity has a $name value return $this->getName(); } Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422047 Share on other sites More sharing options...
cypher86 Posted March 31, 2013 Author Share Posted March 31, 2013 (edited) i don't understand. should i put the method getNominativi inside the entity Nominativo and create a method that returns the name of the class? it doesnt make sense to me ...... can you please have the patience to do me an example? thx Edited March 31, 2013 by cypher86 Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422106 Share on other sites More sharing options...
cypher86 Posted March 31, 2013 Author Share Posted March 31, 2013 my need is to create a combobox that takes its values from a table (or generally a query) Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422112 Share on other sites More sharing options...
gristoi Posted March 31, 2013 Share Posted March 31, 2013 ok, we will use 2 entities , with a many to many relationship . We will use article and category, so many articles can have many categories. <?php namespace name\space\here\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="article") */ class Article{ /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue (strategy = "AUTO") */ private $id; /** * @ORM\ManyToMany(targetEntity="Category", inversedBy="articles") * @ORM\JoinTable(name="articles_categories") **/ private $categories; /** * Constructor */ public function __construct() { $this->categories = new \Doctrine\Common\Collections\ArrayCollection(); } public function addCategorie(\your\namespace\here\Entity\Category $categories) { $this->categories[] = $categories; return $this; } public function removeCategorie(\your\namespace\here\Entity\Category $categories) { $this->categories->removeElement($categories); } /** public function getCategories() { return $this->categories; } } Category entity: <?php namespace your\namespace\here\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Entity * @ORM\Table(name="categories") */ class Category { /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue (strategy = "AUTO") */ private $id; /** * * @ORM\Column(name="name", type="string", length=255) */ private $name; /** * @ORM\ManyToMany(targetEntity="Article", mappedBy="categories") **/ private $articles; /** * Constructor */ public function __construct() { $this->articles = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return Category */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Add articles * * @param \your\namespace\Entity\Article $articles * @return Category */ public function addArticle(\your\namespace\Entity\Article $articles) { $this->articles[] = $articles; return $this; } /** * Remove articles * * @param \your\namespace\Entity\Article $articles */ public function removeArticle(\your\namespace\Entity\Article $articles) { $this->articles->removeElement($articles); } /** * Get articles * * @return \Doctrine\Common\Collections\Collection */ public function getArticles() { return $this->articles; } public function __toString(){ // seeing as i want the name column of my entity to be displayed i return getname() return $this->getName(); } } then the formtype: ->add('categories', 'entity', array( 'class' => 'yourNamespaceHere:Category', 'expanded'=>false, 'multiple'=>false, )) Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422119 Share on other sites More sharing options...
cypher86 Posted March 31, 2013 Author Share Posted March 31, 2013 ok i'm totally confused... what am i doing wrong???? Nominativo entity: <?php namespace Fatturazione\FatturazioneBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Length; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="nominativo") * @ORM\HasLifecycleCallbacks */ class Nominativo { /** * @ORM\id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") */ protected $nominativo; /** * @ORM\Column(type="string") */ protected $indirizzo; /** * @ORM\Column(type="string") */ protected $pi; /** * @ORM\Column(type="string") */ protected $cf; /** * @ORM\Column(type="string") */ protected $tel; /** * @ORM\Column(type="string") */ protected $fax; /** * @ORM\Column(type="string") */ protected $email; public function getNominativo() { return $this->nominativo; } public function setNominativo($nominativo) { $this->nominativo=$nominativo; } public function getIndirizzo() { return $this->indirizzo; } public function setIndirizzo($indirizzo) { $this->indirizzo=$indirizzo; } public function getPi() { return $this->pi; } public function setPi($pi) { $this->pi=$pi; } public function getCf() { return $this->cf; } public function setCf($cf) { $this->cf=$cf; } public function getTel() { return $this->tel; } public function setTel($tel) { $this->tel=$tel; } public function getFax() { return $this->fax; } public function setFax($fax) { $this->fax=$fax; } public function getEmail() { return $this->email; } public function setEmail($email) { $this->email=$email; } public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('nominativo', new NotBlank()); $metadata->addPropertyConstraint('nominativo', new Length(array('max'=>50, 'maxMessage'=>'Testo massimo di 50 caratteri'))); $metadata->addPropertyConstraint('indirizzo', new NotBlank()); $metadata->addPropertyConstraint('indirizzo', new Length(array('max'=>50, 'maxMessage'=>'Testo massimo di 50 caratteri'))); $metadata->addPropertyConstraint('pi', new NotBlank()); $metadata->addPropertyConstraint('pi', new Length(array('max'=>20, 'maxMessage'=>'Testo massimo di 20 caratteri'))); $metadata->addPropertyConstraint('cf', new NotBlank()); $metadata->addPropertyConstraint('cf', new Length(array('max'=>20, 'maxMessage'=>'Testo massimo di 20 caratteri'))); $metadata->addPropertyConstraint('tel', new NotBlank()); $metadata->addPropertyConstraint('tel', new Length(array('max'=>20, 'maxMessage'=>'Testo massimo di 20 caratteri'))); $metadata->addPropertyConstraint('fax', new NotBlank()); $metadata->addPropertyConstraint('fax', new Length(array('max'=>20, 'maxMessage'=>'Testo massimo di 20 caratteri'))); $metadata->addPropertyConstraint('email', new Email()); } public function getNominativi($limit = null) { $qb = $this->createQueryBuilder('b') ->select('b.id, b.nominativo') ->addOrderBy('b.nominativo', 'ASC'); if (false === is_null($limit)) $qb->setMaxResults($limit); return $qb->getQuery() ->getResult(); } public function __toString() { return getNominativi(); } } ?> causale entity: <?php namespace Fatturazione\FatturazioneBundle\Entity; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Date; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="causale") * @ORM\HasLifecycleCallbacks */ class Causale { /** * @ORM\id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="datetime") */ protected $data; /** * @ORM\Column(type="string") */ protected $descrizione; /** * @ORM\Column(type="number") */ protected $onorario; /** * @ORM\Column(type="number") */ protected $spese; /** * @ORM\Column(type="number") */ protected $rimborsi; /** * @ORM\ManyToOne(targetEntity="Nominativo", inversedBy="causale") * @ORM\JoinColumn(name="nominativo", referencedColumnName="id") * @ORM\Column(type="integer") */ protected $nominativo; protected $tipoCausale; public function getId() { return $this->id; } public function setId($id) { $this->id=$id; } public function getData() { return $this->data; } public function setData($data) { $this->data=$data; } public function getDescrizione() { return $this->descrizione; } public function setDescrizione($descrizione) { $this->descrizione=$descrizione; } public function getOnorario() { return $this->onorario; } public function setOnorario($onorario) { $this->onorario=$onorario; } public function getSpese() { return $this->spese; } public function setSpese($spese) { $this->spese=$spese; } public function getRimborsi() { return $this->rimborsi; } public function setRimborsi($rimborsi) { $this->rimborsi=$rimborsi; } public function getNominativo() { return $this->nominativo; } public function setNominativo($nominativo) { $this->nominativo=$nominativo; } public function getTipoCausale() { return $this->tipoCausale; } public function setTipoCausale($tipoCausale) { $this->tipoCausale=$tipoCausale; } public function __toString() { return $this->getNominativo; } public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('data', new NotBlank()); $metadata->addPropertyConstraint('data', new Date(array('message'=>'Data non valida'))); $metadata->addPropertyConstraint('descrizione', new Length(array('max'=>200, 'maxMessage'=>'Testo massimo di 200 caratteri'))); } } ?> form: <?php namespace Fatturazione\FatturazioneBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Doctrine\ORM\EntityRepository; class CausaleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('data','date',array('widget' => 'single_text', 'format' => 'yyyy-MM-dd')); $builder->add('descrizione'); $builder->add('onorario','number'); $builder->add('spese','number'); $builder->add('rimborsi','number'); $builder->add('nominativo','entity', array('class' => 'FatturazioneFatturazioneBundle:Causale', 'expanded' => 'false', 'multiple' => 'false')); $builder->add('tipoCausale','choice',array('choices' => array('0' => 'Fattura', '1' => 'Causale'), 'required' => true)); } public function getName() { return 'causale'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422134 Share on other sites More sharing options...
gristoi Posted March 31, 2013 Share Posted March 31, 2013 your casuale class dosent have @ORM\Entity at the top of the class Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422137 Share on other sites More sharing options...
cypher86 Posted April 1, 2013 Author Share Posted April 1, 2013 ok thx. i add the @ORM\Entity and repalce the datetime doctrine type with date. now a strange thing append.... when i refresh the page it gives me a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422220 Share on other sites More sharing options...
gristoi Posted April 1, 2013 Share Posted April 1, 2013 are you running the site from / or app_dev.php? sounds like your getting a fatal error,. have you cleared the cache and updated the schema? php app/console cache:clear --env=dev php app/console doctrine:schema:update --force also look in the app/logs/dev log to see if there is any error being thrown Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422227 Share on other sites More sharing options...
cypher86 Posted April 1, 2013 Author Share Posted April 1, 2013 nothing append. the last log seems to refer to 9:15 am while i load the page now... Quote Link to comment https://forums.phpfreaks.com/topic/276121-symfony-2-creating-combobox/#findComment-1422235 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.