symfony embed Ajax field in form - ajax

In symfony I have a Cliente entity that can have N Reservas:
class Cliente {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="Reserva", mappedBy="cliente")
*
*/
private $reservas;
....
}
class Reserva
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Cliente", inversedBy="reservas")
*
*/
private $cliente;
...}
In the ReservaType I have:
class ReservaType extends AbstractType {
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('cliente')
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'partes\EscuelaBundle\Entity\Reserva'
));
}
/**
* #return string
*/
public function getName() {
return 'partes_escuelabundle_reserva';
}
}
All that when I create a new reserva show my the typical select option with the list of all customers. I would change that for a ajax input type to select the customer. Any idea how to build it.
Thanks you!

fix it, with a ajax call that bring a list of links. Once clicking in the link it creates the new Reserva.

Related

Doctrine in laravel, getting all user's roles

I have a simple project in laravel 5.4 with Doctrine 2.0. I have three tables: users, roles, user_roles. Screenshot from tables schemas from HEIDISQL I show below:
users:
roles:
user_roles:
I have three Entities classes for each table of course:
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="users")
*/
class User
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*
*/
private $id;
/**
*
* #ORM\Column(type="string")
*
*/
private $name;
/**
*
* #ORM\Column(type="string")
*
*/
private $email;
/**
* #ORM\OneToMany(targetEntity="Task", mappedBy="user", cascade={"persist"})
* #var ArrayCollection|Task[]
*/
private $tasks;
/**
* #ORM\OneToMany(targetEntity="UserRole", mappedBy="user")
*/
protected $user_roles;
/**
* User constructor
* #param #name
* #param #email
* #param $password
*/
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
$this->tasks = new ArrayCollection();
$this->user_roles = new ArrayCollection();
}
/**
*
* #return mixed
*
*
*/
public function getName()
{
return $this->name;
}
/**
* #return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* #return mixed
*/
public function getTasks()
{
return $this->tasks;
}
public function addTask(Task $task)
{
if(!$this->tasks->contains($task)) {
$task->setUser($this);
$this->tasks->add($task);
}
}
public function getId(){
return $this->id;
}
public function getUserRoles(){
return $this->user_roles;
}
}
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="roles")
*
*/
class Role{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string")
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #ORM\OneToMany(targetEntity="UserRole", mappedBy="role",cascade={"persist"})
*/
protected $user_roles;
public function getId(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function getDescription(){
return $this->description;
}
}
<?php
namespace TodoList\Http\Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="user_roles")
*/
class UserRole
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #ORM\ManyToOne(targetEntity="User", inversedBy="user_roles")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
*/
private $user;
/**
*
* #ORM\ManyToOne(targetEntity="Role", inversedBy="user_roles")
* #ORM\JoinColumn(name="role_id", referencedColumnName="id")
*/
private $role;
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \NVC\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set recipe
*
* #param \NVC\RecipeBundle\Entity\Recipe $recipe
* #return UserRecipeAssociation
*/
public function setRole(Role $role)
{
$this->role = $role;
return $this;
}
/**
* Get recipe
*
* #return \NVC\RecipeBundle\Entity\Recipe
*/
public function getRole()
{
return $this->role;
}
}
I have a problem when I'm trying retrieve roles of particular user by doctrine Entity Managar. I'm trying to do that in that way:
public function showUserRoles(EntityManagerInterface $em){
$userRoles = $em->getRepository('\TodoList\Http\Entities\UserRole');
$userRoles->findBy(['user' => 2]);
//count($userRole);
}
There is an error from method findBy
Error message is:
(1/1) FatalErrorException
Doctrine\Common\Proxy\AbstractProxyFactory::getProxyDefinition():
Failed opening required
'C:\xampp\htdocs\Project\storage\proxies__CG__TodoListHttpEntitiesRole.php'
(include_path='C:\xampp\php\PEAR')
Is anything wrong with my Entities? I don't know how could I solve that problem. Could someone help me with that?
I would be very greateful
Best regards ;)
You need to set folder C:\xampp\htdocs\Project\storage\proxies\ as writable and then you need to generate proxies by console command:
doctrine:generate:proxies

Symfony: Only one file entity and different validation rules for each association?

In my current project, I decided to create only one translatable File entity and reuse it for all image/document properties I have. For the translations, I uses Knp Doctrine Behaviors Translatable. So here is the code.
The file class:
class File
{
use ORMBehaviors\Translatable\Translatable;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __toString()
{
return (string)$this->id;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
The translatable file class:
class FileTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $name;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* #Assert\File()
*/
private $file;
/*
* Non tracked parameter
*/
public $folder;
/**
* Set name.
*
* #param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name.
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path.
*
* #param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path.
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set folder
*
* #param string $folder
*
* #return File
*/
public function setFolder($folder)
{
$this->folder = $folder;
return $this;
}
/**
* Get folder
*
* #return File
*/
public function getFolder()
{
return $this->folder;
}
}
And then an example of how it's used in another entity (User) to create an image property:
class User
{
/**
* #ORM\OneToOne(targetEntity="File", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=true)
* #Assert\Valid()
*/
private $image;
}
Nothing new. I just followed Symfony/Knp documentation and it works fine. However, now I want to add different validation rules each time I create a new property like $image for different entities. What is the best strategy here?
Each time I try to add a validation rule related to file in the $image property, for instance, it says it cannot find a file.
you can have specific validator for each entity:
/**
* vérification des constraintes
* #Assert\Callback
*/
public function validate(ExecutionContextInterface $context)
{
var_dump($this->image);// do your check here
}
For those who have the same problem, I finally did what #sylvain said and I created my custom validators:
http://symfony.com/doc/current/cookbook/validation/custom_constraint.html
This is a great tutorial, btw:
https://knpuniversity.com/screencast/question-answer-day/custom-validation-property-path
It works fine. However, I still think the #valid constraint should have worked and I don't understand why it didn't.

Symfony2: How to validate an input field is not-blank, only when checkbox is true?

Within Symfony2 how to validate an inputfield is not-blank, only when the value of a checkbox is 1 (True) - otherwise blank is allowed?
To be more precise, I have a form with a checkbox and an input field with type text. On the Entity in Symfony there should be a check that when the value of the checkbox is True (1) / checked, the value of the input can't be blank. I am using annotations within the Entity.
Any advise would be much appreciated.
UPDATE / SOLUTION - based on GeLo's remark:
<?php
// src/ZeroSpace/Invoicing/Entity/Customer.php
namespace ZeroSpace\InvoicingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="customers")
* #UniqueEntity("billing_id")
* #UniqueEntity("billing_name")
*/
class Customer {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer", length=6, unique=true)
* #Assert\Length(min="6", max="6")
*/
protected $billing_id;
/**
* #ORM\Column(type="string", length=100, unique=true)
*/
protected $billing_name;
/**
* #ORM\Column(type="string", nullable=true, length=100)
*/
protected $billing_consignee;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_street;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_address;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_zip;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_city;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_country;
/**
* #ORM\Column(type="string", length=100)
* #Assert\Email()
*/
protected $billing_email;
/**
* #ORM\Column(type="boolean")
*/
protected $billing_debit=false;
/**
* #ORM\Column(type="string", nullable=true, length=100)
* #Assert\NotBlank(groups={"iban_required"})
* #Assert\Iban(message = "This is not a valid International Bank Account Number (IBAN).")
*/
protected $billing_iban;
protected $locations;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set billing_id
*
* #param integer $billingId
* #return Customer
*/
public function setBillingId($billingId)
{
$this->billing_id = $billingId;
return $this;
}
/**
* Get billing_id
*
* #return integer
*/
public function getBillingId()
{
return $this->billing_id;
}
/**
* Set billing_name
*
* #param string $billingName
* #return Customer
*/
public function setBillingName($billingName)
{
$this->billing_name = $billingName;
return $this;
}
/**
* Get billing_name
*
* #return string
*/
public function getBillingName()
{
return $this->billing_name;
}
/**
* Set billing_consignee
*
* #param string $billingConsignee
* #return Customer
*/
public function setBillingConsignee($billingConsignee)
{
$this->billing_consignee = $billingConsignee;
return $this;
}
/**
* Get billing_consignee
*
* #return string
*/
public function getBillingConsignee()
{
return $this->billing_consignee;
}
/**
* Set billing_street
*
* #param string $billingStreet
* #return Customer
*/
public function setBillingStreet($billingStreet)
{
$this->billing_street = $billingStreet;
return $this;
}
/**
* Get billing_street
*
* #return string
*/
public function getBillingStreet()
{
return $this->billing_street;
}
/**
* Set billing_address
*
* #param string $billingAddress
* #return Customer
*/
public function setBillingAddress($billingAddress)
{
$this->billing_address = $billingAddress;
return $this;
}
/**
* Get billing_address
*
* #return string
*/
public function getBillingAddress()
{
return $this->billing_address;
}
/**
* Set billing_zip
*
* #param string $billingZip
* #return Customer
*/
public function setBillingZip($billingZip)
{
$this->billing_zip = $billingZip;
return $this;
}
/**
* Get billing_zip
*
* #return string
*/
public function getBillingZip()
{
return $this->billing_zip;
}
/**
* Set billing_city
*
* #param string $billingCity
* #return Customer
*/
public function setBillingCity($billingCity)
{
$this->billing_city = $billingCity;
return $this;
}
/**
* Get billing_city
*
* #return string
*/
public function getBillingCity()
{
return $this->billing_city;
}
/**
* Set billing_country
*
* #param string $billingCountry
* #return Customer
*/
public function setBillingCountry($billingCountry)
{
$this->billing_country = $billingCountry;
return $this;
}
/**
* Get billing_country
*
* #return string
*/
public function getBillingCountry()
{
return $this->billing_country;
}
/**
* Set billing_email
*
* #param string $billingEmail
* #return Customer
*/
public function setBillingEmail($billingEmail)
{
$this->billing_email = $billingEmail;
return $this;
}
/**
* Get billing_email
*
* #return string
*/
public function getBillingEmail()
{
return $this->billing_email;
}
/**
* Set billing_debit
*
* #param boolean $billingDebit
* #return Customer
*/
public function setBillingDebit($billingDebit)
{
$this->billing_debit = $billingDebit;
return $this;
}
/**
* Get billing_debit
*
* #return boolean
*/
public function getBillingDebit()
{
return $this->billing_debit;
}
/**
* Set billing_iban
*
* #param string $billingIban
* #return Customer
*/
public function setBillingIban($billingIban)
{
$this->billing_iban = $billingIban;
return $this;
}
/**
* Get billing_iban
*
* #return string
*/
public function getBillingIban() {
return $this->billing_iban;
}
}
<?php
// src/Blogger/BlogBundle/Form/CustomerType.php
namespace ZeroSpace\InvoicingBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;
class CustomerType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('billing_id');
$builder->add('billing_name');
$builder->add('billing_consignee');
$builder->add('billing_street');
$builder->add('billing_address');
$builder->add('billing_zip');
$builder->add('billing_city');
$builder->add('billing_country');
$builder->add('billing_email', 'email');
$builder->add('billing_debit', 'checkbox');
$builder->add('billing_iban');
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'ZeroSpace\InvoicingBundle\Entity\Customer',
'validation_groups' => function(FormInterface $form) {
$data = $form->getData();
if($data->getBillingDebit() == 1) {
return array('Default', 'iban_required');
}
else {
return array('Default');
}
},
));
}
public function getName() {
return 'customer';
}
}
?>
<?php
namespace ZeroSpace\InvoicingBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use ZeroSpace\InvoicingBundle\Entity\Customer;
use ZeroSpace\InvoicingBundle\Form\CustomerType;
class CustomerController extends Controller {
public function createAction(Request $request) {
$form = $this->createForm(new CustomerType(), new Customer());
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($data);
$em->flush();
return $this->redirect($this->generateUrl('customer_list'));
// return $this->redirect($this->generateUrl('task_success'));
}
return $this->render('InvoicingBundle:Page:customers.html.twig', array(
'form' => $form->createView()
));
}
public function listAction() {
$customers = $this->getDoctrine()->getManager()
->createQuery('SELECT c FROM InvoicingBundle:Customer c')
->execute();
return $this->render(
'InvoicingBundle:Customer:list.html.twig',
array('customers' => $customers));
}
public function showAction($id) {
$customer = $this->get('doctrine')
->getManager()
->getRepository('InvoicingBundle:Customer')
->find($id);
if (!$post) {
// cause the 404 page not found to be displayed
throw $this->createNotFoundException();
}
return $this->render(
'InvoicingBundle:Page:customers.html.twig',
array('customer' => $customer)
);
}
}
?>
I think this is the solution:
<?php
namespace ZeroSpace\InvoicingBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use ZeroSpace\InvoicingBundle\Entity\Customer;
use ZeroSpace\InvoicingBundle\Form\CustomerType;
class CustomerController extends Controller {
public function createAction(Request $request) {
$form = $this->createForm(new CustomerType(), new Customer());
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($data);
$em->flush();
return $this->redirect($this->generateUrl('customer_list'));
// return $this->redirect($this->generateUrl('task_success'));
}
return $this->render('InvoicingBundle:Page:customers.html.twig', array(
'form' => $form->createView()
));
}
public function listAction() {
$customers = $this->getDoctrine()->getManager()
->createQuery('SELECT c FROM InvoicingBundle:Customer c')
->execute();
return $this->render(
'InvoicingBundle:Customer:list.html.twig',
array('customers' => $customers));
}
public function showAction($id) {
$customer = $this->get('doctrine')
->getManager()
->getRepository('InvoicingBundle:Customer')
->find($id);
if (!$post) {
// cause the 404 page not found to be displayed
throw $this->createNotFoundException();
}
return $this->render(
'InvoicingBundle:Page:customers.html.twig',
array('customer' => $customer)
);
}
}
?>
<?php
// src/Blogger/BlogBundle/Form/CustomerType.php
namespace ZeroSpace\InvoicingBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;
class CustomerType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('billing_id');
$builder->add('billing_name');
$builder->add('billing_consignee');
$builder->add('billing_street');
$builder->add('billing_address');
$builder->add('billing_zip');
$builder->add('billing_city');
$builder->add('billing_country');
$builder->add('billing_email', 'email');
$builder->add('billing_debit', 'checkbox');
$builder->add('billing_iban');
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'ZeroSpace\InvoicingBundle\Entity\Customer',
'validation_groups' => function(FormInterface $form) {
$data = $form->getData();
if($data->getBillingDebit() == 1) {
return array('Default', 'iban_required');
}
else {
return array('Default');
}
},
));
}
public function getName() {
return 'customer';
}
}
?>
<?php
// src/ZeroSpace/Invoicing/Entity/Customer.php
namespace ZeroSpace\InvoicingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="customers")
* #UniqueEntity("billing_id")
* #UniqueEntity("billing_name")
*/
class Customer {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="integer", length=6, unique=true)
* #Assert\Length(min="6", max="6")
*/
protected $billing_id;
/**
* #ORM\Column(type="string", length=100, unique=true)
*/
protected $billing_name;
/**
* #ORM\Column(type="string", nullable=true, length=100)
*/
protected $billing_consignee;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_street;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_address;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_zip;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_city;
/**
* #ORM\Column(type="string", length=100)
*/
protected $billing_country;
/**
* #ORM\Column(type="string", length=100)
* #Assert\Email()
*/
protected $billing_email;
/**
* #ORM\Column(type="boolean")
*/
protected $billing_debit=false;
/**
* #ORM\Column(type="string", nullable=true, length=100)
* #Assert\NotBlank(groups={"iban_required"})
* #Assert\Iban(message = "This is not a valid International Bank Account Number (IBAN).")
*/
protected $billing_iban;
protected $locations;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set billing_id
*
* #param integer $billingId
* #return Customer
*/
public function setBillingId($billingId)
{
$this->billing_id = $billingId;
return $this;
}
/**
* Get billing_id
*
* #return integer
*/
public function getBillingId()
{
return $this->billing_id;
}
/**
* Set billing_name
*
* #param string $billingName
* #return Customer
*/
public function setBillingName($billingName)
{
$this->billing_name = $billingName;
return $this;
}
/**
* Get billing_name
*
* #return string
*/
public function getBillingName()
{
return $this->billing_name;
}
/**
* Set billing_consignee
*
* #param string $billingConsignee
* #return Customer
*/
public function setBillingConsignee($billingConsignee)
{
$this->billing_consignee = $billingConsignee;
return $this;
}
/**
* Get billing_consignee
*
* #return string
*/
public function getBillingConsignee()
{
return $this->billing_consignee;
}
/**
* Set billing_street
*
* #param string $billingStreet
* #return Customer
*/
public function setBillingStreet($billingStreet)
{
$this->billing_street = $billingStreet;
return $this;
}
/**
* Get billing_street
*
* #return string
*/
public function getBillingStreet()
{
return $this->billing_street;
}
/**
* Set billing_address
*
* #param string $billingAddress
* #return Customer
*/
public function setBillingAddress($billingAddress)
{
$this->billing_address = $billingAddress;
return $this;
}
/**
* Get billing_address
*
* #return string
*/
public function getBillingAddress()
{
return $this->billing_address;
}
/**
* Set billing_zip
*
* #param string $billingZip
* #return Customer
*/
public function setBillingZip($billingZip)
{
$this->billing_zip = $billingZip;
return $this;
}
/**
* Get billing_zip
*
* #return string
*/
public function getBillingZip()
{
return $this->billing_zip;
}
/**
* Set billing_city
*
* #param string $billingCity
* #return Customer
*/
public function setBillingCity($billingCity)
{
$this->billing_city = $billingCity;
return $this;
}
/**
* Get billing_city
*
* #return string
*/
public function getBillingCity()
{
return $this->billing_city;
}
/**
* Set billing_country
*
* #param string $billingCountry
* #return Customer
*/
public function setBillingCountry($billingCountry)
{
$this->billing_country = $billingCountry;
return $this;
}
/**
* Get billing_country
*
* #return string
*/
public function getBillingCountry()
{
return $this->billing_country;
}
/**
* Set billing_email
*
* #param string $billingEmail
* #return Customer
*/
public function setBillingEmail($billingEmail)
{
$this->billing_email = $billingEmail;
return $this;
}
/**
* Get billing_email
*
* #return string
*/
public function getBillingEmail()
{
return $this->billing_email;
}
/**
* Set billing_debit
*
* #param boolean $billingDebit
* #return Customer
*/
public function setBillingDebit($billingDebit)
{
$this->billing_debit = $billingDebit;
return $this;
}
/**
* Get billing_debit
*
* #return boolean
*/
public function getBillingDebit()
{
return $this->billing_debit;
}
/**
* Set billing_iban
*
* #param string $billingIban
* #return Customer
*/
public function setBillingIban($billingIban)
{
$this->billing_iban = $billingIban;
return $this;
}
/**
* Get billing_iban
*
* #return string
*/
public function getBillingIban() {
return $this->billing_iban;
}
}
An efficient approach can be to use the validation group which can be determined on the fly. Then, given the checkbox is not checked, you use the Default validation group whereas if the checkbox is checked, you use the Default group and a custom group which will check if your field is not empty.
See this part of the documentation for more details: http://symfony.com/doc/current/book/forms.html#groups-based-on-the-submitted-data
could you do something like this ?
/**
* #Assert\True()
*/
public function customValidate()
{
return (!$this->checkboxField) or (strlen($this->inputField) > 0);
}

Doctrine 2. How to force entity from proxy

I have 3 entities:
/**
* #ORM\Entity
* #ORM\Table(name="table_a")
*/
class A
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* ORM\OneToMany(targetEntity="B", mappedBy="entityA")
*/
protected $entitiesB;
/**
* ORM\OneToMany(targetEntity="C", mappedBy="entityA")
*/
protected $entitiesC;
/**
* #ORM\Column(type="string")
*/
protected $name;
}
/**
* #ORM\Entity
* #ORM\Table(name="table_b")
*/
class B
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* ORM\ManyToOne(targetEntity="A", inversedBy="entitiesB")
*/
protected $entityA;
/**
* #ORM\Column(type="date")
*/
protected $date;
}
/**
* #ORM\Entity
* #ORM\Table(name="table_c")
*/
class C
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* ORM\ManyToOne(targetEntity="A", inversedBy="entitiesC")
*/
protected $entityA;
/**
* #ORM\Column(type="string")
*/
protected $description;
}
And I have the following situation:
$eB = $repositoryB->find(1);
$eA = $eB->getEntityA(); // $eA will be a proxy
$eC = new C();
$eC->setDescription('XXXXXXXXXX')
->setEntityA($eA);
This will generate an error because $eA is a proxy not an entity. Even if I try:
$eB = $repositoryB->find(1);
$eA = $repositoryA->find(1);
$eC = new C();
$eC->setDescription('XXXXXXXXXX')
->setEntityA($eA);
Will still get an error because once you have fetched a B entity it will automatically fetch a proxy of A entity. And when you try to fetch the A entity with the same identifier as the proxy Doctrine will return the proxy object from the Identity Map because you can not have two objects (one proxy and one Entity) for the same db record.
So is there a way to force retrieving an entity from its proxy? Or another way to set an association, by id not by entity?

integrating doctrine 2 in codeignitier 2

Currently i am trying to integrate doctrine 2 in codeigniter 2 and i am doing all this by following the below tutorial.
http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2-the-right-way/
According to the tutorial i had
1)Doctrine 2 in library
2)Doctrine.php file in library
3)doctrine-cli.php in root folder
4)yaml file inside applications/models/mappings and also entities and proxies inside the same location
According to the tutorial from the command prompt i had made the entities as
User entitiy
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* Entities\User
*/
class User
{
/**
* #var integer $id
*/
private $id;
/**
* #var string $password
*/
private $password;
/**
* #var string $firstName
*/
private $firstName;
/**
* #var string $lastName
*/
private $lastName;
/**
* #var string $email
*/
private $email;
/**
* #var string $website
*/
private $website;
/**
* #var datetime $created
*/
private $created;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set password
*
* #param string $password
* #return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set firstName
*
* #param string $firstName
* #return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* #return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* #param string $lastName
* #return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* #param string $email
* #return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set website
*
* #param string $website
* #return User
*/
public function setWebsite($website)
{
$this->website = $website;
return $this;
}
/**
* Get website
*
* #return string
*/
public function getWebsite()
{
return $this->website;
}
/**
* Set created
*
* #param datetime $created
* #return User
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return datetime
*/
public function getCreated()
{
return $this->created;
}
}?>
also the article entitiy as
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* Entities\Article
*/
class Article
{
/**
* #var integer $id
*/
private $id;
/**
* #var string $title
*/
private $title;
/**
* #var text $content
*/
private $content;
/**
* #var datetime $created
*/
private $created;
/**
* #var Entities\User
*/
private $user;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Article
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* #param text $content
* #return Article
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return text
*/
public function getContent()
{
return $this->content;
}
/**
* Set created
*
* #param datetime $created
* #return Article
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set user
*
* #param Entities\User $user
* #return Article
*/
public function setUser(\Entities\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return Entities\User
*/
public function getUser()
{
return $this->user;
}
}
i had done every thing as explained in that tutorial but when i try to insert the value in database table i got the error as
Fatal error: Class 'Entities' not found in D:\xampp\htdocs\ci_doctrine\application\controllers\welcome.php on line 32
while i use welcome controller to insert the data and my controller is
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct() {
parent::__construct();
// $this->load->library('Doctrine.php');
//$this->em = $this->doctrine->em;
}
public function index()
{
// create a new user object
$user = new Entities/User;
$user->setFirstName('Joel');
$user->setLastName('Verhagen');
$user->setPassword(md5('Emma Watson'));
$user->setEmail('joel#joelverhagen.com');
$user->setWebsite('http://www.joelverhagen.com');
$user->setCreated(new DateTime());
// standard way in CodeIgniter to access a library in a controller: $this->library_name->member->memberFunction()
// save the object to database, so that it can obtain its ID
$this->doctrine->em->persist($user);
// create a new article object
$article = new Entities/Article;
$article->setTitle('Emma Watson is extremely talented, no?');
$article->setContent('By talented, I mean good at lookin\' good.');
$article->setCreated(new DateTime());
// the user object you pass must be persisted first!
$article->setUser($user);
// save the article object to the database
$this->doctrine->em->persist($article);
$this->doctrine->em->flush();
echo "<b>Success!</b>";
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
what is going on i can't figure out .... any suggestion and help..??
You don't use the fully qualified namespace but a relative one.
http://php.net/manual/en/language.namespaces.basics.php
That's why.
Try
$article = new \Entities\Article;
in your controller.
EDIT: Oh even better. You were trying to divide Entities with Article.
You were using / instead of \.

Resources