Pomm2 bundle for Symfony / how to generate Getters and Setters?

113 views
Skip to first unread message

Monpetit

unread,
Mar 30, 2015, 12:57:00 PM3/30/15
to pommp...@googlegroups.com
Hi,

The following generate commands create only empty FlexibleEntity classes:

app/console pomm:generate:schema-all -v -d src -a 'MyBundle\Model' my_db

app/console pomm:generate:relation-all --force -v -d src -a 'MyBundle\Model' my_db my_relation

app/console pomm:generate:entity --force -v -d src -a 'MyBundle\Model' my_db my_relation


Do we have to write Getters and Setters ourselves in the FlexibleEntity classes or is it possible to generate them?

Thanks for your help (and sorry if I misunderstood the documentation).
Jules

Grégoire Hubert

unread,
Mar 30, 2015, 1:08:45 PM3/30/15
to pommp...@googlegroups.com
On Mon, Mar 30, 2015 at 6:57 PM, Monpetit <monpet...@gmail.com> wrote:
Hi,

The following generate commands create only empty FlexibleEntity classes:

[...]
 
app/console pomm:generate:entity --force -v -d src -a 'MyBundle\Model' my_db my_relation


Do we have to write Getters and Setters ourselves in the FlexibleEntity classes or is it possible to generate them?

Hi,

Since you are dealing with a FlexibleEntity, there are no need to generate getters and setters, they are present. If an attribute 'name' is loaded in your entity ->getName() will return its value. This method can be overloaded manually.

All getters can be triggered using array access $entity['name'] will call ->getName().

Cheers,
--
Grégoire HUBERT
Pomm : http://www.pomm-project.org

Monpetit

unread,
Mar 31, 2015, 12:33:19 PM3/31/15
to pommp...@googlegroups.com
Hi,

Thank you very much for your reply.

Sorry, I am a bit confused.

I would like to create forms (with FormBuilder) and I thought I could do it as follows:

namespace MyBundle\Controller;

use MyBundle\Model\MyDb\PublicSchema\Foo;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class SearchController extends Controller
{  
   
public function searchAction()
   
{
        $foo
= new Foo();
       
        $formBuilder
= $this->get('form.factory')->createBuilder('form', $foo);

        $formBuilder
           
->add('name', 'text')
           
->add('picture', 'text')
           
->add('price', 'text')
           
->add('save', 'submit')
       
;
       
        $form
= $formBuilder->getForm();
       
       
return $this->render('MyBundle::search.html.twig',
            array
('form' => $form->createView())
       
);
   
}
}

Name, picture, price are fields for the relation foo in MyDb and are present in the structure definition (MyBundle\Model\MyDb\PublicSchema\AutoStructure\Foo.php).


But I get this error:

No such key 'name'.
 
500 Internal Server Error - ModelException


Stack trace:

in vendor/pomm-project/model-manager/sources/lib/Model/FlexibleEntity.php at line 66  -

               
if ($this->has($var)) {
                   
return $this->container[$var];
               
} elseif (static::$strict === true) {
                   
throw new ModelException(sprintf("No such key '%s'.", $var));
               
}
           
} elseif (is_array($var)) {
               
return array_intersect_key($this->container, array_flip($var));

That's why I thought I had to add getters and setters to the FlexibleEntity (MyBundle\Model\MyDb\PublicSchema\Foo).

I would be very grateful to you if you would point me to the right direction.
Thank you very much.
Jules

Grégoire Hubert

unread,
Apr 1, 2015, 5:47:11 AM4/1/15
to pommp...@googlegroups.com
Hello Jules,


On Tue, Mar 31, 2015 at 6:33 PM, Monpetit <monpet...@gmail.com> wrote:
Hi,

Thank you very much for your reply.

Sorry, I am a bit confused.

I would like to create forms (with FormBuilder) and I thought I could do it as follows:

namespace MyBundle\Controller;

use MyBundle\Model\MyDb\PublicSchema\Foo;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class SearchController extends Controller
{  
   
public function searchAction()
   
{
        $foo
= new Foo();
       
        $formBuilder
= $this->get('form.factory')->createBuilder('form', $foo);

        $formBuilder
           
->add('name', 'text')
           
->add('picture', 'text')
           
->add('price', 'text')
           
->add('save', 'submit')
       
;
       
        $form
= $formBuilder->getForm();
       
       
return $this->render('MyBundle::search.html.twig',
            array
('form' => $form->createView())
       
);
   
}
}

Name, picture, price are fields for the relation foo in MyDb and are present in the structure definition (MyBundle\Model\MyDb\PublicSchema\AutoStructure\Foo.php).


Why do you need to pass an empty entity ? Have a look at http://www.pomm-project.org/news/use-form-with-pomm-in-silex.html the structure class is enough. Hydrate an entity only if the form is valid.

Monpetit

unread,
Apr 3, 2015, 1:40:33 PM4/3/15
to pommp...@googlegroups.com
Hello Grégoire,

Thank you very much for your message and sorry for my late reply.
I am going to try what you advised about forms.

Thank you so much.
Jules
Reply all
Reply to author
Forward
0 new messages