Doctrine generate code with other naming conventions - symfony

We are starting with Symfony2 and Doctrine. I need to select some data from tables that already exist. These tables and column names do not use the naming conventions as defined by Doctrine.
I was wondering if I could create my own naming scheme somewhere. Mainly, we use PascalCase table and columnnames, without underscored. This results in Entity properties like $firstpromotiondatetime while the column is FirstPromotionDateTime, so i'd like my property to be firstPromotionDateTime.

You don't necessarily need to use doctrine's entity generator to generate new entities, you can also manually do it. However, you can also generate the entities via the command line, and then open your entity classes and change the property names as you see fit. Just make sure that the doctrine mapping (use the following annotation for instance) is still pointing to the correct column name in your database.
/**
* #var \DateTime $firstPromotionDateTime
*
* #ORM\Column(name="FirstPromotionDateTime", type="datetime")
*/
private $firstPromotionDateTime;
Edit:
If you manually change the property names, also make sure you have correctly modified the setters & getters as well.

Related

Symfony Single Table Inheritance with translated columns in parent/child entity

I have a parent abstract Table Location which is defined as an STI:
/**
* #ORM\Entity(repositoryClass=Location::class)
* #InheritanceType("SINGLE_TABLE")
* #DiscriminatorColumn(name="discr", type="string")
* #ORM\Table(
* name="location",
* uniqueConstraints={
* #ORM\UniqueConstraint(name="assignment_unique", columns={"longitude", "latitude", "discr"})
* }
* )
*/
abstract class Location extends Place
which is conform with the Schema org https://schema.org/.
Now in this use case a Discriminator is used for the different types of Locations (there are about 7 currently, up to 15 possible).
Now there are approx 15 translatable columns defined in Place, which are mapped to Location at runtime. Each discriminator might add itself columns that might be translated too.
We've discussed the possibility of adding a translation table to the Location entity, reducing all the translatable columns to that entity and when querying choosing the correct language.
I can live with the fact that the parent table outsources its translatable columns into a language_table - but what if a discriminator now adds itself some translatable columns? When querying for Type B Children of Location, it is not known which fields apply.
Does anyone know a pattern to apply for multi-language discriminators with childrens brining in translatable columns?
Thanks for any help.
This is what i've modelled by now:
so in the end everything will be stored in the location table. if everything is set up and we load an location by its discr column identifying the correct type, the foreign keys will retrieve the correct translations from their respective translation tables.
Can anyone give me feedback on that solution?

Different table name and entity name in Symfony2

I have an existing database. Lets say that I have a table named "transactions" and I want to create the corresponding Entity named "Transaction". How can I do that?
you can set the name of the table using the #ORM\Table annotation
/**
* #ORM\Entity
* #ORM\Table(name="transactions")
*/
class Transaction
{
If you don't use Annotations, you can find details about other mappings from this link.
BTW, You can also generate Entities from an Existing Database.
You could set name param for mapping
http://symfony.com/doc/current/book/doctrine.html#add-mapping-information

Symfony2 and Doctrine - ManyToOne

I am trying to understand Symfony2, but there is something that is not making sense to me. I reversed engineered an existing database to produce my entities, so maybe thats the problem.
I have a table called availability_alert, nothing special, a few fields including an id (which is the primary key). This table has no link to anything else.
I then have a second table called booking_class, once again nothing special, but it does have the field $availabilityAlert which links to the availability_alerts tables id.
In essence, an Availability Alert can have one or many Booking Class.
Now in my booking class entity, I have the link
/**
* #var \AlertBundle\Entity\AvailabilityAlert
*
* #ORM\ManyToOne(targetEntity="AlertBundle\Entity\AvailabilityAlert")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="availability_alert_id", referencedColumnName="id")
* })
*/
private $availabilityAlert;
So this all looks ok. The setter for it though came out like so
public function setAvailabilityAlert(\AlertBundle\Entity\AvailabilityAlert $availabilityAlert = null)
{
$this->availabilityAlert = $availabilityAlert;
return $this;
}
So that appears to take an AvailabilityAlert Object as a parameter, not the AvailabilityAlert id?
So with the above, I am presuming doing something like this in my controller will not work?
$alert = new AvailabilityAlert();
$bookingClass = new BookingClass();
$bookingClass->setAvailabilityAlert($alert->getId());
Could someone give me some advice on whether things are correct here, or if I should be doing something else? Essentially AvailabilityAlert should be a static table, other tables link to this.
Any advice appreciated.
Thanks
Yes this is correct.
In the Doctrine world, you are working with objects, not with integers or strings, when it comes to relationships between Entities.
You can read more about Doctrine 2 relationships here: http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations
That's correct. You don't use integers, strings. It's because the relationships are given in entity annotations and Doctrine uses them to figure out what is used exactly to reference the one object from the other. This even let you change how objects reference themselves - for example you change the id to a compound primary key in AvailabilityAlert class and your code wouldn't change much except for the annotations.

Symfony2, Doctrine, Empty associative entity

Let' imagine we have several tables: table_item, table_category, table_items_status.
Which is updated by service in single mode (no relations) using their own entity.
Can i, and how, create one entity that will have only relatioship of this tables, for example something like that....
**
* #ORM\OneToOne(targetEntity="table_item")
* #ORM\JoinColumn(name="itemID", referencedColumnName="itemID")
**
private $tableItemIDByItemID
// ... getter\setter
**
* #ORM\Column(type="integer")
**
private $itemID;
// ... getter\setter
In php code i want simply call
$entity->setItemID(123);
$result = $entity->getTableItemIDByItemID();
And will get ArrayCollection() from table_item by itemID.
Main thing that I want create extra entity only with relationships for several tables and only unidirectional. I need this for creating entity without touching another for relationships.
You should look into entity repositories. These provide some basic functionality queries but you can extend them with your own custom queries (eg: getItemIDByItemID). It's called the repository pattern. An entity is just a object representation of your database.
Symfony2 manual:
"It's a good idea to create a custom repository class for your entity and add methods with your query logic there."
Some more information about repositories:
http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes
http://www.zalas.eu/doctrine2-and-symfony2

Specifying index with Symfony2 for Doctrine's PersistentCollection

Can anybody tell me how I can configure Symfony2/Doctrine to index objects within a PersistentCollection, from a specified column in the entity?
For example assume we have two tables; categories and products, with many products to one category. When I use the repository to load a category, and then call getProducts() to load the products, I want them to be returned indexed by the product ID, or whatever column I specify. Currently they're just indexed incrementally from zero.
Is there anyway to do this, or do I need to loop through and manually set the keys myself?
Mapping OneToMany associations allows you to define an indexBy property. With annotation mappings, it would look like following
class Category
{
// ...
/**
* #OneToMany(targetEntity="Products", indexBy="productId", mappedBy="product")
*/
protected $products;
// ...
}
XML, YAML and PHP mappings allow this too, and this works also with ManyToMany.

Resources