I have an application with products and attributes. In the backoffice I would like to track the changes on the attribute values. I started using loggable extension by gedmo and it works fine but now I'd like to add a new column in the log entry table for the product id.
I know how to create a custom entity for the logs by adding the annotation in the attribute table:
#Gedmo\Loggable(logEntryClass="App\Entity\LogEntry")
But I don't know if is somehow possible to extend the Gedmo listener to also log the product_id in the log entry table
I found the solution:
https://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/advanced.html#overriding-the-listeners
With this its easy to use a custom logger
Related
I have a Symfony app using multiple entities.
A third-party analytic tool plugs to my database to create reportings.
What I would like to achieve, is being able to update the Symfony entity from the frontend in order to add new fields to the database tables (in order to get the new fields showing up in the reporting tool).
Anyone has a idea on how to achieve that?
Thanks in advance.
If i understand correctly, you wan't to be able to add fields to your entity dynamicaly.
I don't know if this is doable and if so, it would probably be messy and unsecure.
What you can do however is using sub-entities with dynamic key => values fields.
You should have one main entity, an entity with the list of your dynamic fields, a many to many relation between your main entity and your fields entities and a third entity with the actual values from those fields.
I followed this question to create timestamps that save on create and update maintained by the database: Doctrine2 - Type timestamp - Default value
Problem is that now I get a "value cannot be null" when saving a record using Doctrine. I see in the generated insert that it's trying to save the timestamps as null. Is there a way to avoid Doctrine setting a column on INSERT/UPDATE.
Why do you want letting DB to do Doctrine job?
Doctrine won't set a column at all, if you don't map it. But would you take a look to Timestampable extension?
Another approach is using Doctrine entity listeners. So you can make your own, on PrePersist and PreUpdate and set the time there.
If you're worrying about setters for changing dates, you can at least use dynamic mapping for that fields (just map datetime properties on-the-fly when you really need to change them). Not sure that you can use reflection though, because your entity can be behind the proxy class.
If you still want keeping business-logic in Database, you can map only fields you really want to change (not datetime fields in your case), then write custom Doctrine hydrator (just extend their AbstractHydrator class) that will populate all the fields you need (include datetime). Also you can configure your new hydrator as default (i.e. in Doctrine configuration section in config.yml), so it will work without any adjustments.
a customer has an existing database. The schema is often changed within the database itself (e.g. he adds a new column).
My task is to develop an admin area with symfony that automatically reacts on table schema changes without modifying the application code. E.g. the customer adds a new column to table "MyEntity", and the application automatically generates a new column in the accordingly list view.
My approach is to dynamically map the table columns to the Entity class so that ALL Attributes and ALL Getters/Setters are generated dynamically from the table schema.
So is it possible to map the table columns in a Doctrine Entity without the use of Annotations or XML Files.
Something like:
class MyEntity{
public function generateMappingFromSchema($sTableName){...}
}
Please don't do that. Doctrine was not designed for such use case.
There is a library though you should check https://github.com/laravel-doctrine/fluent which basically is a mapping driver that allows you to manage your mappings in an Object Oriented approach. And there are other tools:
http://crud-admin-generator.com/
http://crudkit.com/
http://www.grocerycrud.com/
which are maybe better for that, I don't know.
But again, please don't do that. Do not allow the customer to modify the database schema or give them e.g. a phpMyAdmin which was designed for that.
How to update an existing entity in nopCommerce 2.5 (MVC)?
I did makes changes to class under Nop.Data > Mapping folder. But it does not created the column to database after I run the application. Is there anything else I need to do?
This is the resource created by the Nop team to guide you through updating the solution/entity
http://www.nopcommerce.com/docs/73/updating-an-existing-entity-how-to-add-a-new-property.aspx
add new property in Nop.Core\Domain folder.
Note: Nop.Data > Mapping is for mapping between your table and your entity classes.
Add new column in your table manually.
Manually add column in table is necessary.
Change your Controller and View as per your requirement.
If you want to get to more detail about Add Custom Field In NopCommerce Table
I'm setting up a blog using Asp.net MVC3 and Entity framework 4. Most of the properties of the blog post already exist in the database, and therefore can easily be linked to the blog post during the creation.
One of the last element I've added are sources, these sources have a direct link to my BlogPost and have to be added during the creation of the BlogPost. My idea was to use the entity property Sources of the BlogPost entity, like:
blogPost.Sources.Add(new Source() { Name = source.Value, Url = source.Key.ToString(), Type = "source" });
Unfortunately I'm not allowed to create and add an Source this way, I'm getting the error: "Cannot insert explicit value for identity column in table 'Source' when IDENTITY_INSERT is set to OFF."
I guess this is cause of the ID column which has is an Identity with Identity increment turned on, but I don't set any ID myself. I tried to make id = null, but since its not a null-able it's not allowed either.
I believe turning this feature off let's me add records with my own ID's, but this is not what I want. The database should create the ID's.
Is there a way to add these kinds of properties during creation?
I solved the problem by adding the Source entity again from the database, apparently the entity framework model was generated before the field was set to Identity Increasing and therefore wanted to insert it's own ID's.