Symfony2 same form, different entities NOT related

Posted by user1381537 on Stack Overflow See other posts from Stack Overflow or by user1381537
Published on 2013-08-02T08:23:39Z Indexed on 2013/08/02 15:37 UTC
Read the original article Hit count: 163

Filed under:
|
|
|
|

I'm trying to write one form for submitting against MySQL DB, but I can't get it working, I've tried a lot of things (separate forms, create an ->add('foo', new foo()) to a field, and trying to parse plain SQL with a normal HTML form is my only solution, which is obviously not the best.

This is my DB structure:

enter image description here

As you can see I need to insert the comments textarea to ticketcomments among the user who wrote it, etc.

On crmentity the description field.

Then on ticketcf the fields that I need to submit from form, are this (because you wont know if I don't tell you because of the field names):

tcf.cf594 AS Type,
tcf.cf675 AS Suscription,
tcf.cf770 AS ID_PRODUCT,
tcf.cf746 AS NotificationDate,
tcf.cf747 AS ResponseDate,
tcf.cf748 AS ResolutionDate,

And, of course, every table needs to have the same ticketid id for the submitted form, so we can retrieve it with one simple query.

It will be easy to do with plain SQL instead of using DQL and Symfony2 forms, but is not a good way to do it.

Also, here's my "Ticket list" query, if you need it to have it more clear...

SELECT 
t.ticketNo AS Ticket,
t.title AS Asunto,
t.status AS Estado,
t.updateLog AS LOG,
t.hours AS Horas,
t.solution AS Solucion,
t.priority AS Prioridad,
tcf.cf594 AS Tipo,
tcf.cf675 AS Suscripcion,
tcf.cf770 AS IDPROD,
tcf.cf746 AS F_Noti,
tcf.cf747 AS F_Resp,
tcf.cf748 AS F_Reso,
CONCAT (cd.firstname, cd.lastname) AS Contacto,
crm.description AS Descripcion,
crm.crmid AS id
FROM WbsGoclientsBundle:VtigerTroubletickets t
INNER JOIN WbsGoclientsBundle:VtigerTicketcf tcf
WITH t.ticketid = tcf.ticketid
INNER JOIN WbsGoclientsBundle:VtigerContactdetails cd
WITH t.parentId = cd.contactid
INNER JOIN WbsGoclientsBundle:VtigerCrmentity crm
WITH t.ticketid = crm.crmid
WHERE t.parentId IN (
    SELECT cd1.contactid
    FROM WbsGoclientsBundle:VtigerContactdetails cd1
    WHERE cd1.accountid = (
        SELECT cd2.accountid 
        FROM WbsGoclientsBundle:VtigerContactdetails cd2
        WHERE cd2.contactid = :contactid))
AND t.status <> \'Closed\'

And also "Ticket details" query (which is not in DQL format yet, only SQL) is so simple, it only retrieve the comments field and createdtime from ticketcomments appended to this query so we have all the fields...

Thank you.

This is a test form, using troubletickets and ticketcomments, it's returning errores because I can't set a comments field because troubletickets doesn't has it, but I need that field to be submitted to ticketcomments ...

VtigerTicketcommentsType

<?php
namespace WbsGo\clientsBundle\Form\Type;
use Symfony\Component\Form\AbstractType, Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class VtigerTicketcommentsType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
        ->add('ticketid')
        ->add('comments')
        ->add('ownerid')
        ->add('ownertype')
        ->add('createdtype')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'WbsGo\clientsBundle\Entity\VtigerTicketcomments'
        ));
    }
    public function getName() {
        return 'comments';
    }
}

OpenTicketType.php

<?php 

namespace WbsGo\clientsBundle\Form;

use
    Symfony\Component\Form\AbstractType,
    Symfony\Component\Form\FormBuilderInterface
;
use WbsGo\clientsBundle\Form\Type\VtigerTicketcommentsType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class OpenTicketType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('priority')
            ->add('solution')
            ->add('comments', 'collection', array(
                'type' => new VtigerTicketcommentsType()
        ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'WbsGo\clientsBundle\Entity\VtigerTroubletickets'
        ));
    }

    public function getName()
    {
        return 'ticket';
    }
}

TicketController.php

<?php

namespace WbsGo\clientsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use WbsGo\clientsBundle\Entity\VtigerTroubletickets;
use WbsGo\clientsBundle\Entity\VtigerTicketcomments;

use WbsGo\clientsBundle\Form\OpenTicketType;
use Symfony\Component\HttpFoundation\Request;



class TicketController extends Controller {
    public function indexAction() {
        $em = $this->getDoctrine()->getManager();

        $tickets = $em
                ->getRepository('WbsGoclientsBundle:VtigerTroubletickets')
                ->findAllOpenByCustomerId($this->getUser()->getId());
        $userdata = $this->getDoctrine()->getManager()
                ->getRepository('WbsGoclientsBundle:VtigerContactdetails')
                ->findContact($this->getUser()->getId());
        return $this
                ->render('WbsGoclientsBundle:Ticket:index.html.twig',
                        array('tickets' => $tickets, 'userdata' => $userdata));
    }

    public function addAction() {
        $assets = $this->getDoctrine()->getManager()
                ->getRepository('WbsGoclientsBundle:VtigerAssets')
                ->findAssetByAccountId($this->getUser()->getId());

        $assetlist = array();
        foreach ($assets as $key => $v) {
            $assetlist[$key] = $key;
        }

        $form = $this->createForm(new OpenTicketType(), new VtigerTroubletickets());

        return $this
                ->render('WbsGoclientsBundle:Ticket:add.html.twig',
                        array('form' => $form->createView(),
                                'assets' => $assets,));
    }
}

This is the error Symfony2 is returning

Neither the property "comments" nor one of the methods "getComments()", "isComments()", "hasComments()", "_get()" or "_call()" exist and have public access in class "WbsGo\clientsBundle\Entity\VtigerTroubletickets".

EDIT 2 This code is actually rendering my forms, but I need help in order to submit each XXXType form to its corresponding table.

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('descripcion')
            ->add('prioridad')
            ->add('solucion')
            ->add('comment',
                    new VtigerTicketcommentsType()
            )
            ->add('contacto')
            ->add('suscripcion')
            ->add('producto', 
                    'entity',
                    array(
                            'class' => 'WbsGo\clientsBundle\Entity\VtigerAssets',
                            'property' => 'assetname',
                            'empty_value' => '--SELECT--',
                            'query_builder' => function(\WbsGo\clientsBundle\Entity\VtigerAssetsRepository $repository)
                            {
                                //return $repository->findAssetByAccountId($this->customerId);
                                return $repository->createQueryBuilder('a')
                                ->select('a')
                                ->where('a.account = (SELECT cd.accountid FROM WbsGoclientsBundle:VtigerContactdetails cd WHERE cd.contactid = ?1)')
                                ->setParameter(1, $this->customerId);
                            }
                    )
            )
            ->add('hardware')
            ->add('backup')
            ->add('web')
            ->add('restore')
            ->add('customerId')
        ;
    }

I also removed ->add('ticketid') from VtigerTicketcommentsType.php because it has relationship and is not needed. it's auto_incremental and must be generated once everything is submitted.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql