A może i tak. Widocznie z innych przyczyn.
A na formularz z możliwością wyboru innej encji jaki miałbyś pomysł?
Chcę dodawać posty do różnych tematów. Jest kilka tematów głównych i jak dodaję posta to wybieram z listy dostępnych tematów którego tematu ma dotyczyć.
ManyToMany poniewać post może też być wyświetlany na stronach różnych tematów.
Could not determine access type for property "tematy" in class "App\Entity\Post": The property "tematy" in class "App\Entity\Post" can be defined with the methods "addTematy()", "removeTematy()" but the new value must be an array or an instance of \Traversable, "App\Entity\Temat" given.
Kod
class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('tematy', EntityType::class, [
'label' => 'Tematy',
'class' => Temat::class,
'choice_label' => 'nazwa',
])
->add('tytul', EntityType::class, [
'class' => Tytul::class,
/*'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('t')
->orderBy('t.tresc', 'ASC');
},*/
/*'choice_label' => function($tytul) {
return $tytul->getTresc();
},*/
'multiple' => false,
'expanded' => false
]);
}
Kod
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Akapit", mappedBy="post")
*/
private $akapity;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Tabela", mappedBy="post")
*/
private $tabele;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Temat", inversedBy="posty")
* @ORM\JoinTable(name="post_temat")
*/
private $tematy;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Tytul", inversedBy="posty", cascade={"persist"})
*/
private $tytul;
Kod
/**
* @Route("/new", name="post_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$post = new Post();
$form = $this->createForm(PostType::class, $post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($post);
$entityManager->flush();
return $this->redirectToRoute('post_index');
}
return $this->render('post/new.html.twig', [
'post' => $post,
'form' => $form->createView(),
]);
}
Kod
temat
id
nazwa
|||
|
|||
post
id
elementy
/ | \
--------- | -------------
/ | \
/ | \
/ | \
/// | \\\
---\
wiersz ---------- akapit --------- tytuł ------------- tabela
id ---/ id id id
id_akapitu id_posta id_posta
treść treść
odstępg odstępg odstępg
odstępp odstępp odstępp
odstępd odstępd odstępd
odstępl odstępl odstępl
czcionka czcionka czcionka
rozmiar rozmiar rozmiar
pochylenie pochylenie pochylenie
pogrubienie pogrubienie pogrubienie