php - Symfony Form: OneToMany and CollectionType Field -
i got stucked building form symfony 3.
i defined entity 'news' containing attribute 'newsarticle', has onetomany relation entity 'newsarticle', holding translations of attributes 'headline', 'subheader , 'bodytext'. goal provide form holds on 1 hand fields attributes of 'news' , on other hand fields create 'newsarticle' entry default language.
entity news (excerpt):
/** * @orm\entity * @orm\table(name="news") */ class news { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\onetomany(targetentity="newsarticle", mappedby="news", cascade={"persist", "remove"}) */ private $newsarticle; /** * constructor */ public function __construct() { $this->newsarticle = new \doctrine\common\collections\arraycollection(); } }
entity newsarticle (excerpt):
/** * @orm\entity * @orm\table(name="news_article") */ class newsarticle { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\manytoone(targetentity="news") * @orm\joincolumn(name="news_id", referencedcolumnname="id") */ private $news; /** * @orm\column(type="string", length=150, unique=false, nullable=false) * @assert\notblank() */ private $headline; /** * @orm\column(type="string", length=150, nullable=true) */ private $subheadline; /** * @orm\column(type="string", length=65536, nullable=false) * @assert\notblank() */ private $bodytext; /** * @orm\manytoone(targetentity="language") * @orm\joincolumn(name="language_id", referencedcolumnname="id") */ private $languageid; }
the controller (excerpt):
public function addaction(request $request) { $lang = $this->getdoctrine() ->getrepository('bundle:language') ->findoneby(array('deleted' => 0, 'fallback' => 1)); $news = new news(); //$news->newsarticle->setlanguageid($lang); $form = $this->createform(newstype::class, $news); $form->handlerequest($request); if ($form->issubmitted() && $form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($news); $em->flush(); } return $this->render('bundle:news:add.html.twig', array( 'form' => $form->createview(), 'news' => $news, )); }
twig template (excerpt):
{{ form_start(form) }} <div class="form-group"> {{ form_label(form.headline) }} <div class="col-sm-8 col-md-6"> {{ form_errors(form.headline) }} {{ form_widget(form.headline) }} </div> </div> <div class="form-group"> {{ form_label(form.subheadline) }} <div class="col-sm-8 col-md-6"> {{ form_errors(form.subheadline) }} {{ form_widget(form.subheadline) }} </div> </div> <div class="form-group"> {{ form_label(form.bodytext) }} <div class="col-sm-8 col-md-6"> {{ form_errors(form.bodytext) }} {{ form_widget(form.bodytext) }} </div> </div> {{ form_end(form) }}
newstype (excerpt):
class newstype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('newsarticle', collectiontype::class, array( 'entry_type' => newsarticletype::class, 'allow_add' => true, 'allow_delete' => true, )) } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'data_class' => 'bundle\entity\news', )); } }
newsarticletype (excerpt):
class newsarticletype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('headline', texttype::class, array( 'required' => false, )) ->add('subheadline', texttype::class, array( 'required' => false, )) ->add('bodytext', ckeditortype::class, array( 'required' => false, )) ; } public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array( 'data_class' => 'bundle\entity\newsarticle', )); } }
unfortunately error message...
neither property "headline" nor 1 of methods "headline()", "getheadline()"/"isheadline()" or "__call()" exist , have public access in class "symfony\component\form\formview" in bundle:_partials:form/news.html.twig @ line 27
... , have no idea, how access attributes of related entity? hints?
edit #1:
the complete 'child'-entity including getters/ setters:
/** * @orm\entity * @orm\table(name="news_article") */ class newsarticle { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\manytoone(targetentity="news", inversedby="newsarticle") * @orm\joincolumn(name="news_id", referencedcolumnname="id") */ private $news; /** * @orm\column(type="string", length=150, unique=false, nullable=false) * @assert\notblank() */ private $headline; /** * @orm\column(type="string", length=150, nullable=true) */ private $subheadline; /** * @orm\column(type="string", length=65536, nullable=false) * @assert\notblank() */ private $bodytext; /** * @orm\manytoone(targetentity="language") * @orm\joincolumn(name="language_id", referencedcolumnname="id") */ private $languageid; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set headline * * @param string $headline * * @return newsarticle */ public function setheadline($headline) { $this->headline = $headline; return $this; } /** * headline * * @return string */ public function getheadline() { return $this->headline; } /** * set subheadline * * @param string $subheadline * * @return newsarticle */ public function setsubheadline($subheadline) { $this->subheadline = $subheadline; return $this; } /** * subheadline * * @return string */ public function getsubheadline() { return $this->subheadline; } /** * set bodytext * * @param string $bodytext * * @return newsarticle */ public function setbodytext($bodytext) { $this->bodytext = $bodytext; return $this; } /** * bodytext * * @return string */ public function getbodytext() { return $this->bodytext; } /** * set news * * @param \bundle\entity\news $news * * @return newsarticle */ public function setnews(\bundle\entity\news $news = null) { $this->news = $news; return $this; } /** * news * * @return \bundle\entity\news */ public function getnews() { return $this->news; } /** * set languageid * * @param \bundle\entity\language $languageid * * @return newsarticle */ public function setlanguageid(\bundle\entity\language $languageid = null) { $this->languageid = $languageid; return $this; } /** * languageid * * @return \bundle\entity\language */ public function getlanguageid() { return $this->languageid; } }
the getters , setters attributes exist, wonder why error message misses method getheadline() instead of getheadline()? be, problem wrong syntax in twig: {{ form_errors(form.headline) }} ? i've tried {{ form_errors(form.newsarticle.headline) }} too, error message remains same.
any hints, please?
i think problem setters getters
as try command :
php bin/console doctrine:generate:entities youbundle
for generate getter/setter or write it
and forgot inversedby in entity newsacticle
like :
/** * @orm\manytoone(targetentity="news", inversedby="newsarticle" ) * @orm\joincolumn(name="news_id", referencedcolumnname="id") */ private $news;
Comments
Post a Comment