Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 2 - actions to save data with one to one relationship
    primarykey
    data
    text
    <p>I am new to Symfony 2. So pardon me if this is a simple problem but I just can't figure it out how to deal with it.</p> <p>I have 2 entities called Book and Page.</p> <p>This is the snippet of Book entity code</p> <pre><code>/** * Book * * @ORM\Table(name="`book`") */ class Book { /** * @var integer * * @ORM\Column(name="`id`", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="`name`", type="string", length=255, nullable=false) */ private $name; ... } </code></pre> <p>And this is the snippet for my Page entity</p> <pre><code>/** * Page * * @ORM\Table(name="`page`") */ class Page { /** * @var integer * * @ORM\Column(name="`id`", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\OneToOne(targetEntity="Book") * @ORM\JoinColumn(name="`book_id`", referencedColumnName="`id`") */ private $book; /** * @var string * * @ORM\Column(name="`page_number`", type="bigint", nullable=false) */ private $pageNumber; ... } </code></pre> <p>I am currently using PosgreSQL as my RDBMS.</p> <p>Creating and saving data into Book table is fine. But when it comes to saving data for the Page, I need to make it reference to a Book by book id. The book is already stored in the database.</p> <p>I know that the old school way is to pass the book id as hidden field into the Page form. I am just wondering, is this the right way to do in Symfony?</p> <p>What I have tried is that I pass a GET request to the page create action. Something along the line of </p> <pre><code>http://sites/page/create?bookId=123 </code></pre> <p>And the appropriate action on the Page controller is:</p> <pre><code>class PageController extends Controller { public function createAction(Request $request) { // Check if bookId has been passed $bookId = $request-&gt;get('bookId'); // Find the book by its $bookId $book = $this-&gt;getDoctrine()-&gt;getRepository("MyBundle:Book")-&gt;find($bookId); // Create new page $page = new Page(); // Assign the book to the page $page-&gt;setBook($book); // Create the page form $pageForm = $this-&gt;createForm( new PageType(), $page ); $pageForm-&gt;handleRequest($request); if ($pageForm-&gt;isValid()) { $pageData = $pageForm-&gt;getData(); // SAVE DATA HERE // Redirect to index page return new RedirectResponse($this-&gt;generateUrl('index_page')); } // Then render to the template return $this-&gt;render( 'MyBundle:Page:index.html.twig', array('form' =&gt; $pageForm-&gt;createView()) ); } } </code></pre> <p>And my page type is:</p> <pre><code>class PageType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('page_number', 'text', array( 'label' =&gt; 'Page Number', 'required' =&gt; true, ) ); $builder-&gt;add('content', 'textarea', array( 'label' =&gt; 'Page Content', 'required' =&gt; false, ) ); $builder-&gt;add('save', 'submit', array( 'label' =&gt; 'Save', 'attr' =&gt; array('class' =&gt; 'btn') )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver-&gt;setDefaults(array( 'data_class' =&gt; 'MyBundle\Entity\Page' )); } ... } </code></pre> <p>My issue is that:</p> <ol> <li>Is this the right way to deal with creating entities that has one to one relationship with another entity?</li> <li>When I did my way above, it seems like when I submit the form, I don't get the book entity anymore. What is wrong?</li> </ol> <p>I would really appreciate it if someone could point me in the right direction regarding creating an entity that has one to one relationship with another entity. And if there is anything wrong with the code, could someone please point it out?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload