Create new column in query using doctrine2 - symfony

I'm working on symfony2 project using doctrine2, I have an issue about creating a new button for user connected to a form to add 10 new fields with 10 values of course and 10 columns in DB:
that means when a user click "add new" button so he can adding one field X with XV value and creating one colomun "X" too in database , then second field Y with YV... until 10.Also XV AND YV are the same type of field for example "decimal".
I don't have problem with adding a new field in form using ajax,but my questions are :
1- How to create a new column in a table in DB with doctrine2 and insert value too?
2- how to update Class Entity and Class form too automatically in the same time when a new column is created?

First of all I have to admit I did not understand the goal you are trying to achieve completely. But I am almost positive, that there is a conceptual mistake in your design.
You have to distinguish between the following two concepts:
DML (Data Manipulation Language), such as INSERT, UPDATE or DELETE data in/from the database, meaning that you create new records (INSERT).
DDL (Data Definition Language), such as CREATE TABLE (to create a new database relation) or ALTER TABLE (to add new columns to the database).
You are trying to use DDL (ALTER TABLE statements according to your description) to dynamically add new columns to your database, which is not effective and not good practice at all. You should have an extremely good reason to do it that way (e.g. if the system you are building is a database administration tool or something like this...).
I would heavily recommend you to completely rethink and redesign your architecture and use DML to achive your goal. I am sure your problem can be solved by designing a flexible data structure and not adding new columns, but inserting new records in a dynamic database.
After you have redesigned your database structure it will be very easy to implement your task with Symfony and Doctrine.

Related

What is the proper way to add records to a V2 odata model with two way binding in SAP UI5?

I have a SAP UI5 V2 odata model with a two way binding to a table and to a form.
The table is displaying all records, and when clicking on a record, I am using setBindingContext to bind the selected table record to the form for editing. When typing in the form, the values dynamically update in the table (because of the two way binding). model.submitChanges() writes the change back to the server.
So displaying the list, and editing records in the list are working just fine.
Now for the problem How to create records with the same form?
I also want to use the same form for adding new records, but I cannot figure out how to unbind the form from a previously selected record, or otherwise create a new blank entry in the data model to be sent to the server.
All of the tutorials I have been able to find on doing UI5 Odata CRUD operations don't really address this problem.
I discovered this example in the documentation which pointed me in the correct direction.
In summary, you use the oModel.createEntry method to create a new entry in the oData Model. You then have to bind this new entry to your form with setBindingContext - this is the part I was missing.

Symfony Mapping in Doctrine without Annotations or XML Files

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.

Entity Framework update issue

I am not sure if it's a bug, but when i add a new view or a new stored procedure to the model it updates all the tables that exist. So my question is should it work like this and if it should how can i add some new procedure without updating the whole model?
Yes, this is the correct functionality when using the "Update Model" function for EntityFramework. It looks at every database object and updates the EF Model to match what it finds in the database. This is, in part, because the designer does not let you specifically choose which tables or view to update, so it verifies any changes in the database. This allows the model to proactively ensure that when it connects to the database there won't be an error based on the database changing.

Adding table from another database to ASP.NET Dynamic Data + Entity Framework

I have a table in another database I would like to scaffold via ASP.NET Dynamic Data and incorporate into my existing Entity Model - is there anyway to do this? (eg using a view or other mechanism or customize the view, edit or insert operations via ad-hoc SQL or stored procedures?)
I don't want to replicate the entire DynamicData sub-folder structure and create another entity model for just one table
I was able to solve this by manually creating an entity in the SSDL and CSDL sections of the .edmx file by using a DefiningQuery and then defining the EntitySets for my entity class
I also added insert / update / delete Function elements to the SSDL with inline SQL using the CommandText property
At this point I had enough to let the Designer map the CRUD methods to these inline SQL functions I defined
It's a little tricky but it works and the general approach opens up many possibilities I had not thought about

Mapping an entity to 2 tables

I have a position class that i want to be able to write to the database. I currently have a entity for the class that is mapped to the database. I want to because to have the class insert data into one table and update entries in the other table. One table is for current positions and the other is for historical positions. Is it possible to map an entity to 2 tables and have it update 1 table and insert into the other?
Wouldn't it be easier to have a Trigger on the Update that Insert automatically a copy of the data in your history table?
Just don't do this. Use a database insert trigger.
I would make 2 different entities for this. If you want this to map to a single entity in your application you should write a data access class that (based on the data of your single entity) determines whether to do an update or an insert.
And I would personally never ever use a trigger for such a thing as this is business logic to me that needs to be exposed in the application so it can be tested.

Resources