Storing and retrieving a serialized array in a Symfony2 form - php

In my database there is a table, which has a column of the type text. This column holds a serialized array. This array is read and stored by another application, and I cannot change its format.
The serialized array holds a selection of database names, table names and column names in two different languages.
I would like to write a controller, entity, form, etc. in Symfony2 that is able to modify this serialized array.
There is a script that I can use that can provide an array of all possible db names, table names and column names that each serialized array may contain.
The goal is to present a list of check boxes where users can select db's, tables and columns. Next, they can do a translation of the names.
Since all data is so volatile, I am not sure whether this is even possible in Symfony2.
An alternative is to make the following entities: { database, table, column } and do it fully OO. And then I could export a selection in a serialized array, to the external application that expects it that way...
Can you guys follow my reasoning? Am I overlooking a strategy here...?
Added:
The array is a nested array up to the fifth degree. Databases contain tables, which contain columns. And every item has an original name and a translated name.

I think you answered your own guestion:
An alternative is to make the following entities: { database, table, column } and do it fully OO.
And then I could export a selection in a serialized array, to the external application that
expects it that way...
You would start with a master entity mapped to your table.
class SomeEntity
{
protected $serializedInfo;
public getDatabases()
{
// Process serializedInfo into an array of database objects and return
You then pass SomeEntity to SomeEntityFormType which in turn uses a collection of DatabaseFormTypes. The DatabaseFormType then has a collection of TableFormTypes and so on.
Eventually your form would be posted and SomeEntity would be updated. You then serialize before posting. Should be straight forward. Might be a bit more challenging if you want users to add information but even then it is doable.

I know it's really late but I was really busy with university so I couldn't answer sooner
This is what I think is the best to do
Imagine that the table that contains the column which contains your array is called foo
So you make an entity which is called Foo and contains a field(type text) that has the name you like
Now the tricky part is to make an object called Database that contains all the relations you need(To a Table object and Table objects to column Objects)
So even though I told you to make the field type as text you will pas the object Database to this field
So how it's going to work
The Database object will have a __string method that will return the serialized array of the object the way you want
This way when doctrine2 tries to save the Database object in the text field it will be saved as the string that __string method returns
And you will have getDatabase that will converts the serialized array to the database object
This is the idea that I have and not sure if it suits you or not

Related

Read from one database and write to another php/laravel

I'm learning Laravel and would know howto read data from a db and write it automatically to a second db.
First I read from db1 and it works:
$paciente_q = Pacientes::on('db1')->find($id);
Then I wish to move the data to an identical table on db2 (assigned in the configuration)
Pacientes::create($paciente_q);
The error is that I pass an object and "::create" wants an array. I converted it to an array but didn't work. The only option that I can find is to create an array with the data and then make the ::create. But I think that there should be an easier way. I'm talking about 10 columns.
What could I do if we talk about hundreds of columns?
Your approach didn't work probably because by default mass assignment is prevented for security reasons; you need to manually set the model's fields that are mass assignable in the fillable property of the model (that should be an array) - if you do not care about that security or are sure that you'll never directly mass-assign user input to your models you can make all the fields mass assignable by setting the guarded property of the model to an empty array.
Once that's done, your code is mostly correct, just convert the model to an array and don't forget to select the second database when creating the model, like so :
// the model to insert, converted to an array - get() would also work but first() ensures we get only one record even if the primary key is messed up and there are multiple values with the same ID
$paciente_q = Pacientes::on("db1")->find($id)->first()->toArray();
// create the same model on the second database
Pacientes::on("db2")->create($paciente_q);
Now, if you want to do it occasionally for a few rows then the above approach is suitable, otherwise you may look at bulk insertion, here's an example for copying the entire table from your first database to the second one :
// an array with all the rows
$patients = Pacientes::on("db1")->all()->toArray();
// get the model's table name
$table = with(new Pacientes)->getTable();
// bulk insert all these rows into the second database
DB::connection("db2")->table($table)->insert($patients);
Note that here we're not using Eloquent for inserting them, so we must first get the table's name from an instance of the model; if the table's name on the second database is different from the first then adjust the $table variable accordingly.
The solution was to change the get() to first() because we were searching for one item. I read wrong the first solution from #André... sorry! Should learn to read instead of Laravel!
$paciente_q = Pacientes::on('db1')->where('numerohistoria',$numerohistoria)->first()->toArray();
Pacientes::create($paciente_q);
Now it works!! Thanks to all and specially to #André !

Database design using Symfony2 and Doctrine for lookup tables

I have an entity that contains several choice fields and to normalize the database, the best way is to have these fields linked to a lookup table. The lookup tables are two columns with the first as the primary key as an integery type and the second is the lookup value, usually a string of several words.
To display an entity object, I need to query each lookup table to get the values. Is this the standard wy of doing it or does anyone else have another method? Should there be only one lookup table or would I need a different lookup for each field? I think I need one for each field since I have to allow a user to choose the field that applies to them and only want to show the appropiate choices for each field.
Everything is stored in doctrine and the database, correct? No arrays or simple lookup objects stored only in Symfony/php?
I am using this as a reference for my naming and creating queries
doctrine join multiple tables
In the end, I decided to add a lookup table to my database and add another entity to my Symfony project. The form to display the choice selection uses the entity type, and when I need to display the underlying selection in twig, I added a data transformer according to the docs from Symfony http://symfony.com/doc/current/cookbook/form/data_transformers.html

What Database model should I use to store this data format?

I basically have a bunch of objects with the fields;
a string identifier of this object instance id : "banana"
a dictionary of string keys and values data : {}
an array of ids of instances related to this one friends: ["apple", "orange" ]
an array of related (in a different way) instance ids. followers: ["grapefuit"]
You may have deduced this data will model a Twitter network.
Over the life of my data (which will extend beyond the life of any particular program execution), the number of instances and the data stored in the fields will change (expectedly grow), so I would like to store all this data in a MySQL database to be interfaced by a PHP script.
I'm a complete rookie in the world of databases, but I somewhat understand the table model and query structure.
How should I structure the database for this particular problem?
In working with a MySQL-like database, one thing to remember is that you should avoid having things like dicts and arrays in a particular column. The reason is that this makes querying on these values horrible.
What I would do is have, for example, a "Friends" schema with two columns, FriendA and FriendB. For every friend pair you have, this will be a row in this database. You can do the same with with "Followers", have a Follower and a Followee column.
Now querying across these tables just requires a join, and more importantly if two friends get unfriended or someone decides to unfollow someone else, this is just one delete instead of two (and no arrays!)
And of course, unless you're planning on having unstructured key/value data, expand that to a full schema.

Saving an Arraycollection field to mysql/php using actionscript

How do you save an arraycollection field in an object to a mysql database. I usually would have used a for each item in array collection field then pass a unique key to it so it track it back later, but thats getting cumbersome now.
Is there a way to save an arraycollection that also allows easy update of the arraycollection item.
Thanks in advance
Johnny
If you woun't update some elements of collection separately, you may store it in one row (values of array separated with ',' in one string). Use blob fields if the string will be long. If you need to update some part in that collection you may tre to derive out that part in separate row. But it's not good practice, so if you want to update something putting each element of collection in a separate field is probably the best solution.

Inserting into joined tables in CodeIgniter

I just got a joined table set up and running in CodeIgniter; I am retrieving data from the DB very easily using ActiveRecord. The next obstacle though, seems like the hardest: I am new to joins and I have no idea how I'm going to insert data into my joined tables. The data should go in looking the same as it comes out (meaning actual data, not row ids) but short of hard-coding arrays of associations and transforming the data before save, I don't know how I'm going to do that. But there has to be a way, right?
Right?
I actually don't know CodeIgniter, but I can imagine how it should work based on other PHP ORMs.
$something = new ModelObject();
$something->setSomeProperty($someVal);
$something->save();
$related = new RelatedObject();
$related->setModelId($something->getId());
$related->save();
In other words, once you create the object represented by one table, that object's property corresponding to the generated identifier is populated for you. Now you can use it when you create an object in the related ("join") table to set the foreign key property before saving.

Categories