I use yii2 framework in my application and I have a table named objects in my SQL database
"objects" ( object_id , object_typeID , ... )
But we have some objects that have special fields and user should be able to add custom fields to objects form
My solution is create a new table named object-type-"index" for each new objects that have custom fields!
But how should I automatically create custom form for each object-type-"index" tables? Can I use gii module to generate automatic forms?
For custom fields you should have two additional tables for all "objects":
[object_field_value]
object_id | field_id | value
2 | 5 | Yes
2 | 6 | Blue
3 | 5 | No
[object_field]
ID | name | [other attributes for field]
5 | Colorful? |
6 | Base color |
That way you can match object with it's all fields
Related
I have two table
Treatment Table - in this table I have:
+--------------+----------------------+
| Treatment_id | Treatment_name |
+--------------+----------------------+
| 1 | Bridges |
| 2 | Root canal Treatment |
| 3 | Filling |
+--------------+----------------------+
Fee Table - in this table:
+--------+--------------+------+
| Fee_id | Treatment_id | Fee |
+--------+--------------+------+
| 1 | 1(Bridges) | 5000 |
| 2 | 2 | 6500 |
+--------+--------------+------+
Note: Here Treatment_id is Treatment_name
Here, i insert data in database then we display data in fee template in laravel
how can display Treatment_name instead of Treatment_id
pls help i am new in laravel
i want display data fee blade template
Like
Fee_id|Treatment_id|Fee
1 |Bridges|5000
instead of Treatment_id Display Treatment_name
it's work fine
but when we update Fee table then it display error
it updated but in Fee table Treatment_id column will updated Treatement_name..
suppose i update above data in Fee table Treatment_id=1=Bridge ,updated it replace Treatment_name insted of Treatment_id ,so that's why it gives error so,how update
You can do this by Laravel relationships
public function treatment()
{
return $this->belongsTo(App\Treatment::class, 'Treatment_id', 'Treatment_id');
}
App\Treatment is Model class
Treatment_id is the fields name that is used to make a relation between the two tables
After this, you can access the treatment table columns in the following way.
$fee->treatment->Treatment_name;
If you are not aware of Laravel relationships then please read about this.
Here is Laravel Relationship Doc:
https://laravel.com/docs/6.x/eloquent-relationships
I am running a project on MySQL 8 and PHP 7.2 on Laravel 6 framework
I have a EAV situation as this:
page table:
id | project_id | page
1 | 1 | about-us
2 | 1 | home
3 | 2 | home
page_attributes table:
id | page_id | attribute | type
1 | 1 | speed | float
2 | 1 | title | text
attribute_values table:
id | page_attribute_id | value
1 | 1 | 5.22
2 | 2 | page title is this
Now my question is that it's better in attribute_values table to have just one column for both float and text types like i did or to have two separated columns one for value_text and another value_float?
And if I had more types like int, boolean,... what about then? which way is beter?
UPDATE:
#strawberry mentioned a way that I didn't thing of it's considered if it's better to have multiple tables for each data type
So it's now like this:
having one column for all data types
having multiple columns for each data types
having multiple tables for each data types
Note: I am dealing with about 50 Million records
I have created a function that is called on the hook add_meta_boxes which creates a new meta type employee (this is just an testing example) which shows inputs for name, department and picture. But I don't want to add just one, I want to also have a button for the admin user to create another employee, so that on the page, in the loop, I can get an array of the meta_key employee.
Is the only way to do this, to insert a script and button, so that on button press, the JavaScript creates all the inputs via the DOM? Or is there a "Wordpress-Official" api/function for doing this?
I want something like:
-------------------------------------------------------------------
| Employee |
-------------------------------------------------------------------
| Name: ____________________ |
| Department: ____________________ |
| Picture: browse... |
| -------- |
| | Save | |
| -------- |
-------------------------------------------------------------------
------------------------
| Add Another Employee |
------------------------
A "cleaner" way to do this, would be to create a number of custom post types, and link them using Posts 2 Posts [1] - then you can do nicer things.
Meta is an easy way to do stuff, but I find you eventually end up regretting the choice to do it that way.
[1] http://wordpress.org/plugins/posts-to-posts/
I'm currently using CakePHP 2.x.
I make all my forms using the form_helper and then the active record way to save the model data. It's really tidy and simple, but I can't find the way to save one particular structure.
I've 2 tables, one for the user basic information and one for the user details, with a lot of different fields: User and User_Detail.
User:
id | name | email
------------------------
1 | John | john#doe.com
2 | Paul | paul#doe.com
User_Detail:
id | user_id | field | value
-----------------------------
1 | 1 | sex | m
2 | 1 | age | 21
3 | 2 | height | 180
What's the best way to make the user fill some fields? What fields to complete can be hardcoded in the app. Is it possible using a form helper and saveRelationship()?
Based on your comments, I would add a new table that contains the name of the "custom fields", lets call it "custom_fields" and this would be its definition
custom_fields:
id
name
Its model is called CustomField and it hasMany UserDetail
Your user_details table will have this structure
user_details:
id
user_id
custom_field_id
value
This model (UserDetail) belongsTo CustomField.
Your model User hasMany UserDetail.
Now, in your users form you might want to have a select with all possible CustomValues, in order to do this you previously did something like this in your controller
$customValues = $this->CustomValues->find('list');
$this->set(compact('customValues'));
So, in your view you will have something like this
echo $this->Form->input('UserDetail.custom_field_id', array('type' => 'select', 'values' => $customValues));
echo $this->Form->input('UserDetail.value')
And of course, after submitting this form you will call Model::SaveAssociated
I hope you have now the idea.
Assuming that I understand what your trying to do ..... If using Form Helper you can construct your input fields as follows:
echo $this->Form->input('User.name');
echo $this->Form->input('User.email');
echo $this->Form->input('User_Detail.sex');
echo $this->Form->input('User_Detail.age');
echo $this->Form->input('User_Detail.height');
Then when you save you should use saveAssociated($this->request-data) and it should work by saving the record into both models and automatically creating the appropriate User_Detail.user_id field.
I'm trying to find out the best way store array of items with details in SQL table.
I have a user account database. The user have multiple input fields to enter multiple details like:
___ : ______ +(get more multiple field field)
User can input any details like
Output1 : Output2
Mobile : 2455...
email : sdf
city : dfs
Other : sf
On an average a user will use around 20 options
Since the fields (mobile, email etc.) are not known to me, I have to store Output1 field with the answer field (output2).
I will be having a very huge user base, so I think it's not better to create separate tables for each user.
Is there any way to store and get the details in limited or single column.
Since both the attribute name and value comes from users, a typical 3-table model of saving many-to-many relationship is a bit of overkill.
I would just kept users and their attributes in two separate tables:
+---------+-----------+--------------+
| user_id | user_name | user_email |
+---------+-----------+--------------+
| 1001 | John | john#doe.com |
+---------+-----------+--------------+
| 1002 | Tim | tim#doe.com |
+---------+-----------+--------------+
+----------+-----------+--------------+--------------+
| field_id | user_id | field_name | field_value |
+----------+-----------+--------------+--------------+
| 1 | 1001 | Option1 | Option2 |
+----------+-----------+--------------+--------------+
| 2 | 1001 | Mobile | 2345656565 |
+----------+-----------+--------------+--------------+
| 3 | 1001 | city | dfs |
+----------+-----------+--------------+--------------+
| 4 | 1002 | Other | something |
+----------+-----------+--------------+--------------+
Possibly with some additional columns for sorting, tagging, etc.
I would use 3 tables.
Table 1 - user. PK is UserId. Other fields are name, rank, serial number, etc
Table 2 - Attribute - Primary key is AttributeId. Other Field is attribute name. Examples of attribute names are emailAddress, cellphoneNumber, cityName.
Table3 - UserAttribute. Primary Key is UserId and AttributeId. Other field is Attribute value.