im trying to safe the serials from various products already bought, so i made a relation between the invoice table, serials table and my products table, the relations are shown below.
i already made a form to add the relations between the product_table and invoice_table with the serial_table, the form is adding the relations and the serials, but the user shouldn't be available to choose the relations between the tables, it should find the idproduct and id invoice from the purchase made and store the serials and send it to the database without the user changing it, below is the picture of my form.
this is the formtype from the form above
class serialType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('serial','integer')
->add('idPedido')
->add('idProducto')
->add('agregar serial','submit')
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'sava\InventarioBundle\Entity\TblProductosSeriales'
// 'inherit_data' => true
));
}
/**
* #return string
*/
public function getName()
{
return 'sava_inventariobundle_serial';
}
}
i imported my orm files with the php app/console doctrine:mapping:import command from the tables shown above.
this is my product orm.
sava\InventarioBundle\Entity\TblProductos:
type: entity
table: tbl_productos
fields:
idProduct:
id: true
type: integer
unsigned: false
nullable: false
column: id_product
generator:
strategy: IDENTITY
lifecycleCallbacks: {
}
my serial key table orm.
sava\InventarioBundle\Entity\TblProductosSeriales:
type: entity
table: tbl_productos_seriales
fields:
idProductoSerial:
id: true
type: integer
unsigned: false
nullable: false
column: id_producto_serial
generator:
strategy: IDENTITY
serial:
type: string
length: 80
fixed: false
nullable: false
manyToOne:
idProduct:
targetEntity: TblProductos
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
id_product:
referencedColumnName: id_product
orphanRemoval: false
idinvoice:
targetEntity: TblPedidos
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
id_pedido:
referencedColumnName: id_invoice
orphanRemoval: false
lifecycleCallbacks: { }
this is my invoice orm.
sava\InventarioBundle\Entity\TblPedidos:
type: entity
table: tbl_pedidos
fields:
idinvoice:
id: true
type: integer
unsigned: false
nullable: false
column: id_invoice
generator:
strategy: IDENTITY
lifecycleCallbacks: { }
If you don't want the user to be able to choose the relation why are you adding them to the form ? You can set them server side, in the controller :
so :
get the data of your form
query the database with the id from your form to get the two other ones
hydrate the object ( formObject->setStuff($id) )
persist
To give you an idea of what to do : for example if you have a serial and want to get your product id from it
$serial = new Serial;
$form = $this->createForm(new SerialType, $serial);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
// if form is valid
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
// get the data sent from your form
$data = $form->getData();
$idSerial = $data->getSerial();
// find your ids according to the serial you just got from the form
$repository = $this->getDoctrine()->getManager()->getRepository('AcmeBundleBundle:Products');
$idProduct = $repository->findBySerial($idSerial);
// hydrate the $serial
$serial->setProduct($idProduct);
$em->persist($serial);
$em->flush();
return ....
}
}
if you have the ids in your template do
<form role="form"
action="{{ path('your_route', { 'idProduct': idProduct, 'idInvoice': idInvoice }) }}"
...
</form>
And then you can just get them in your controller like so
UpdateSerialAction($idProduct, $idInvoice)
{
and hydrate the form object with those two arguments
Related
I've the following entity Category defined in YML with bi-directional many-to-many relationship. When i try to load the fixtures data in the corresponding database via doctrine:fixtures:load i receive a PDO Exception error 1048 about integrity violation that the 'name' field can't be null
# src/tuto/JobeetBundle/Resources/config/doctrine/Category.orm.yml
tuto\JobeetBundle\Entity\Category:
type: entity
repositoryClass: tuto\JobeetBundle\Repository\CategoryRepository
table: category
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
slug:
type: string
length: 255
unique: true
oneToMany:
jobs:
targetEntity: Job
mappedBy: category
manyToMany:
affiliates:
targetEntity: Affiliate
mappedBy: categories
lifecycleCallbacks:
prePersist: [setSlugValue]
preUpdate: [setSlugValue]
/**
* #var string
*/
private $slug;
public
function setSlug($slug) {
$this - > slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string
*/
public
function getSlug() {
return $this - > slug;
}
/**
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public
function setSlugValue() {
$sl = new Slugify();
$this - > slug = $sl - > slugify($this - > getName());
}
/**
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public
function prePersist() {
$this - > slug = '';
}
May you find the fixture here
You have a typo $qualityManager object on line 20:
Here:
$qualityManager = new Category();
$technician->setName("Quality Manager"); // <-- Wrong object
Try this:
$qualityManager = new Category();
$qualityManager->setName("Quality Manager");
Hope this help
Hi I am facing an that i cant not find the solution of it, so for a help.
I have two entities: Cast and Artists. In cast from i have actor, actress which will be field by Artist table, I used this code:
for that :
namespace Bbd\MyAppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CastType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('actor', 'entity', array(
'class' => 'BbdMyAppBundle:Artist',
'property' => 'name',
'multiple' => true,
'label' => 'Artist',
'required' => false,
))
->add('actress')
->add('content')
;
}
there can be multiple actor or actress. so in db it saves like:
Doctrine\Common\Collections\ArrayCollection#000000006f69bd7b000000001772666a
in the actor field. i dont why, it should save the id or name.
here is the cast orm:
Bbd\MyAppBundle\Entity\Cast:
type: entity
repositoryClass: Bbd\MyAppBundle\Repository\CastRepository
table: cast
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
actor:
type: text
nullable: true
actress:
type: text
nullable: true
oneToOne:
content:
targetEntity: Content
inversedBy: cast
joinColumn:
name: content_id
referencedColumnName: id
onDelete: CASCADE
Artist ORM
Bbd\MyAppBundle\Entity\Artist:
type: entity
repositoryClass: Bbd\MyAppBundle\Repository\ArtistRepository
table: artist
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
bangla_name:
type: string
length: 255
unique: true
priority:
type: integer
birth:
type: date
sex:
type: string
length: 6
bio_english:
type: text
bio_bangla:
type: text
Thanks for help..
According to your scenario i can suggest you have a ManyToMany association between your Cast and Artist entity each cast have many artists and each artist can appear in morethan one cast
Your Artist entity will look like
use Doctrine\ORM\Mapping as ORM;
/** #Entity **/
class Artist
{
/**
* #ORM\ManyToMany(targetEntity="Cast", inversedBy="artists")
* #JORM\oinTable(name="cast_artists")
**/
private $cast;
public function __construct() {
$this->cast = new \Doctrine\Common\Collections\ArrayCollection();
}
}
And Cast entity will have a mapping like
use Doctrine\ORM\Mapping as ORM;
/** #Entity **/
class Cast
{
/**
* #ORM\ManyToMany(targetEntity="Artist", mappedBy="cast")
**/
private $artists;
public function __construct() {
$this->artists = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addArtist($artists) {
$this->artists[] = $artists;
return $this;
}
public function removeArtist($artists) {
$this->artists->removeElement($artists);
}
public function getArtists() {
return $this->artists;
}
}
Once you have added all artists record you can create a cast record by selecting multiple artists whether its actor/actress
I have used Symfony's entity generation facilities to create Entities (e.g., Person and Destination) as well as custom Entity Repositories (e.g., PersonRepository and DestinationRepository). The Entities were generated against orm.yml files such as Person.orm.yml and Destination.orm.yml. The DestinationRepository class works great, but my PersonController can never seem to find any methods in the PersonRepository class. The error I'm getting is:
UndefinedMethodException: Attempted to call method "findMe" on class "Me\MyBundle\Entity\Person" in C:\xampp55\htdocs\symtran2\src\Me\MyBundle\Controller\PersonController.php line 124.
I put a little "findMe()" method in the Person entity, and it works, but when I move it to PersonRepository.php, I get the above error. This is driving me crazy, and Google informs me that I'm not the only person to have this problem.
Here is my Destination.org.yml file:
Ginsberg\TransportationBundle\Entity\Destination:
type: entity
repositoryClass: Ginsberg\TransportationBundle\Entity\DestinationRepository
table: destination
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
unique: true
nullable: false
is_active:
type: boolean
nullable: true
manyToOne:
program:
targetEntity: Program
inversedBy: destinations
joinColumn:
name: program_id
referencedColumnName: id
oneToMany:
reservations:
targetEntity: Reservation
mappedBy: destination
Here is my Person.orm.yml file:
Ginsberg\TransportationBundle\Entity\Person:
type: entity
repositoryClass: Ginsberg\TransportationBundle\Entity\PersonRepository
table: person
id:
id:
type: integer
generator:
strategy: AUTO
fields:
firstName:
type: string
length: 100
lastName:
type: string
length: 100
uniqname:
type: string
length: 25
unique: true
phone:
type: string
length: 20
nullable: true
status:
type: string
length: 100
nullable: true
dateApproved:
type: datetime
nullable: true
isTermsAgreed:
type: boolean
nullable: true
hasUnpaidTicket:
type: smallint
nullable: true
created:
type: datetime
modified:
type: datetime
nullable: true
oneToMany:
reservations:
targetEntity: Reservation
mappedBy: person
manyToOne:
program:
targetEntity: Program
inversedBy: persons
joinColumn:
name: program_id
referencedColumnName: id
nullable: false
lifecycleCallbacks:
prePersist: [ setCreatedValue ]
preUpdate: [ setModifiedValue ]
Here is my DestinationRepository file:
<?php
namespace Ginsberg\TransportationBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* DestinationRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class DestinationRepository extends EntityRepository
{
public function findByProgramsSortedByProgram($param)
{
$dql = 'SELECT d, p FROM GinsbergTransportationBundle:Destination d JOIN d.program p ORDER BY p.name ASC, d.name ASC';
$query = $this->getEntityManager()->createQuery($dql);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $ex) {
return null;
}
}
}
And here is my PersonRepository.php file:
<?php
namespace Ginsberg\TransportationBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* PersonRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PersonRepository extends EntityRepository
{
public function findMe() {
print_r("Here");
}
public function findByPendingSortedByCreated($fakeParam)
{
$dql = "SELECT p, prog FROM GinsbergTransportationBundle:Person p JOIN d.program prog WHERE p.status = 'pending' ORDER BY p.created ASC";
$query = getEntityManager()->createQuery($dql);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $ex) {
return null;
}
}
}
I'm using YAML for Doctrine but annotations in my controllers.
Here's the controller method that tries to call the findMe() method:
/** Finds and displays a Person entity.
*
* #Route("/{id}", name="person_show")
* #Method("GET")
* #Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('GinsbergTransportationBundle:Person')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Person entity.');
}
$entity->findMe();
//$entity->testMe();
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
I would be beyond grateful for any assistance in sorting this out.
you are calling findMe on the entity found by id, you have to call it on the repository
$repository = $em->getRepository('GinsbergTransportationBundle:Person');
$found = $repository->findMe();
These are the entities:
Objective.php
namespace Foo\BSCBundle\Entity;
class Objective{
protected $id;
protected $name;
protected $perspective;
...
}
Objective.orm.yml
Foo\BSCBundle\Entity\Objective:
type: entity
table: master.objectives
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
manyToOne:
perspective:
targetEntity: Pespective
inversedBy: objectives
joinColumn:
name: perspective_id
referencesColumn: id
Perspective.php
use Doctrine\Common\Collections\ArrayCollection;
namespace Foo\BSCBundle\Entity;
class Perspective {
protected $id;
protected $objectives;
...
}
Perspective.orm.yml
Foo\BSCBundle\Entity\Perspective:
type: entity
table: perspectives
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 100
oneToMany:
objectives:
targetEntity: Objective
mappedBy: Perspective
DefaultController.php
public function indexAction($name)
{
$perspective = $this->getDoctrine()
->getRepository('FooBSCBundle:Objective')
->find(1);
$params = array('name' => $name, 'perspective' => $perspective);
return $this->render('FooBSCBundle:Default:index.html.twig', $params);
}
In my browser I get the error:
The target-entity Foo\BSCBundle\Entity\Pespective cannot be found
in 'Foo\BSCBundle\Entity\Objective#perspective'. 500 Internal
Server Error - MappingException
What am I doing wrong?
I have 3 entities where the mapping is causing some issues.
The two problems im facing are:
When i use doctrine command line schema update, it removes the column "item_type" in the item_type_field table. It looks like doctrine is not seeing this column in the YML file, while it is there.
In the Symfony application the mapping between TypeField and FieldType is not working. When i dump the FieldType of a field it returns null.
Am i missing something?
Entities and mappings:
class ItemType
{
protected $id;
protected $title;
protected $description;
protected $fields;
public function __construct(){
$this->fields = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function getDescription()
{
return $this->description;
}
public function addField(\Aveqcms\CoreBundle\Entity\TypeField $field)
{
$field->setItemType($this);
$this->fields[] = $field;
return $this;
}
public function removeField(\Aveqcms\CoreBundle\Entity\TypeField $field)
{
$this->fields->removeElement($field);
}
public function getFields()
{
return $this->fields;
}
}
class TypeField
{
private $id;
private $item_type;
private $field_type;
public function getId()
{
return $this->id;
}
public function setItemType($itemType)
{
$this->item_type = $itemType;
return $this;
}
public function getItemType()
{
return $this->item_type;
}
public function setFieldType($fieldType)
{
$this->field_type = $fieldType;
return $this;
}
public function getFieldType()
{
return $this->field_type;
}
}
class FieldType
{
private $id;
private $title;
private $fields;
public function __construct()
{
$this->fields = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
}
Mapping files:
Acme\CoreBundle\Entity\ItemType:
type: entity
table: item_type
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
title:
type: string
length: 255
description:
type: string
length: 255
oneToMany:
fields:
targetEntity: TypeField
mappedBy: item_type
cascade: [persist]
Acme\CoreBundle\Entity\TypeField:
type: entity
table: item_type_field
id:
id:
type: integer
generator: { strategy: AUTO }
manyToOne:
field_type:
targetEntity: FieldType
inversedBy: fields
joinColumn:
name: field_type
referencedColumnName: id
manyToOne:
item_type:
targetEntity: ItemType
inversedBy: fields
joinColumn:
name: item_type
referencedColumnName: id
Acme\CoreBundle\Entity\FieldType:
type: entity
table: field_type
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: text
oneToMany:
fields:
targetEntity: TypeField
mappedBy: field_type
In TypeField you have manyToOne repeated twice. The second one overrides the first one whcih is why doctrine is not seeing it.
Acme\CoreBundle\Entity\TypeField:
type: entity
table: item_type_field
id:
id:
type: integer
generator: { strategy: AUTO }
manyToOne:
field_type:
targetEntity: FieldType
inversedBy: fields
joinColumn:
name: field_type
referencedColumnName: id
#manyToOne: *** Get rid of this ***
item_type:
targetEntity: ItemType
inversedBy: fields
joinColumn:
name: item_type
referencedColumnName: id
This may or may not fix all your issues. And it certainly does not address the issue of your very confusing naming conventions.