Witam, staram się uczyć symfony2 :) Tym razem mam problem z dodawaniem rekordów. Mianowicie stworzyłem dwie tabele : ogłoszenia, kategorie.
Chciałbym je połączyć relacją "wiele do jednego" - czyli jedna kategoria może zawierać N ogłoszeń.
Oto co spłodziłem do tej pory :
Kategorie
<?php
//src/Demo/AoneBundle/Entity/kategorie.php
namespace Demo\AoneBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="kategorie")
* @ORM\Entity(repositoryClass="Demo\AoneBundle\Repository\KategorieRepository")
*/
class Kategorie{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=50)
*/
protected $nazwa;
/**
* @ORM\Column(type="string", length=75)
*/
protected $nazwa_seo;
/**
* @ORM\Column(type="integer")
*/
protected $waga;
..... set i gety ....
}
Ogłoszenia
<?php
//src/Demo/AoneBundle/Entity/Ogloszenia.php
namespace Demo\AoneBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="ogloszenia")
* @ORM\Entity(repositoryClass="Demo\AoneBundle\Repository\OgloszeniaRepository")
*/
class Ogloszenia
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $idogl;
/**
* @ORM\Column(type="integer")
* @ORM\ManyToOne(targetEntity="Kategorie", inversedBy="ogloszenia")
* @ORM\JoinColumn(name="idkat", referencedColumnName="id")
*/
protected $idkat;
/**
* @ORM\Column(type="string", length=45)
*/
protected $tytul;
/**
* @ORM\Column(type="string", length=65)
*/
protected $tytul_seo;
/**
* @ORM\Column(type="datetime")
*/
protected $data;
/**
* @ORM\Column(type="integer")
*/
protected $okres;
/**
* @ORM\Column(type="boolean")
*/
protected $promo;
/**
* @ORM\Column(type="string", length=45)
*/
protected $kontakt;
/**
* @ORM\Column(type="string", length=45)
*/
protected $mail;
.... sety i gety ....
}
i kontroler :
public function showAction(){
$category = new Kategorie();
$category->setNazwa("Testowa1");
$category->setNazwaSeo("testowa1");
$category->setWaga(5);
$ogloszenie = new Ogloszenia();
$ogloszenie->setIdkat($category);
$ogloszenie->setTytul("TestOgloszenia");
$ogloszenie->setTytulSeo("test_ogloszenia");
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->persist($ogloszenie);
$em->flush();
return $this->render('DemoAonebundle:Post:post.html.twig', array(
'id_one'=> $category->getId(),
'id_two'=> $ogloszenie->getIdogl(),
'id_three'=>$ogloszenie->getIdkat()
));
}
Czy mógłby mi ktoś napisać dlaczego dostaję błąd w linii
$em = $this->getDoctrine()->getManager();
Fatal error: Call to undefined method Symfony\Bundle\DoctrineBundle\Registry::getManager() in C:\wamp\www\mOgloszenia\src\Demo\AoneBundle\Controller\PostController.php on line 32
wzorowałem się na dokumentacji : http://symfony.com/doc/current/book/doctrine.html#saving-related-entities
Bardzo proszę o pomoc :) Bo zaczyna się to robić już baaardzo frustrujące :/