Building a component, version 1.5 (gonna be cut loose soon, I know, but it's what I need to work with.)
Issue with the /admin/tables/mycom.php file. (In which mycom is whatever the component name is)
I'm not following the use of this file. From reading the walkthrough, it's creating a table class that extends JTables.
Now, some of the DB column names I'm using have 'space' characters in them. ie, 'field 1' instead of 'field1' (don't ask me, it's not my data.)
The sytax for identify these fiels is:
$myfield = null
It says these reference your fields in the mySQL table, but my field names include space which wouldn't work with this syntax.
Any help understanding this class, it's php file and what it's use is would be much appreciated.
By creating a JTable class for your table most of the code for editing your table is done for you. I suggest you familiarize yourself with JTable in the Joomla library so you know what functions are pre-written for you that you can use and override.
As to the issue of your DB column name having spaces... well to be honest it's just a bad idea. Here are some MySQL naming convention best practices for you....
always use lowercase with underscores instead of CamelCase. Goes for both the table and column names. (first_name instead of FirstName, address_1 instead of Address1
every table should have an "id" column as the primary key, don't call it UserID or anything else
the table name should be plural (profiles, supports, etc.)
foreign keys should have the singular name of the related table followed by underscore id. For example: "profile_id" or "support_id".
If you can I would recommend you do some ALTER TABLE updates to the MySQL e.g.
ALTER TABLE `#__example` CHANGE `Field 1` `field_1` VARCHAR(50) NOT NULL DEFAULT 'empty';
Related
i have a problem with validate my query in mysql using enum. as i know mysql doesnt support with constraint so i use enum
Example code :
alter table jewerly modify gender enum('M','F') NOT NULL DEFAULT 'M';
it run well if i use like above, but i have a problem to validate in another condition.
i want to modify validation with enum for : inputted productID format must like : PR[xxx], xxx means the number. if we use constraint, it will be like 'PR[0-9][0-9][0-9]',
but i dont know how to use in enum.
alter table jewelry modify jewelid enum('PR[0-9][0-9][0-9]') NOT NULL;
i tried this query above but it doesnt work.. any idea?
What you are attempting to do makes no sense in MySQL. You seem to be using a regular expression format for an enum definition.
Although enum can be used for such validation, it has some drawbacks. There are limits to the number of values, for instance. Changing the type can be problematic -- such as fixing a spelling error.
Instead, you might consider use foreign keys for this purpose:
create table ref_jewelid (
jewelid varchar(255)
);
create table jewelry (
. . .
constraint fk_jewelid foreign key (jewelid) references ref_jewelid(jewelid)
);
You would then populate ref_jewelid with the valid values, perhaps using a spreadsheet to generate the names. You might even take this one step further and have jewelid be a bona fide numeric id, with the name being one attribute of it. That is the approach I would probably take.
i am creating plugin in wordpress for contact us form and in that i am creating fields(textbox,textarea etc...) dynamically based on my requirement. so there is no fixed number of fields. i am storing dynamically created fields in my db table.
in some project i may require to create 3 fields and some project i may require to create 5 fields.. my dynamic form is working fine...
now my problem is DB. how do i create table to store contact us data? bcz sometimes there might be 3 fields and sometime there might be more than or less than 3 fields..
so my question is how do i make my table schema for this scenario...
suggestion will be highly appreciated...
Thanks in advnce
Change table structure is strongly not suggested, it's bad in performance, and may lost data if you drop a column.
So, the original subject 'make db table field dynamic' is possible but not a good plan, we still have alternative plan:
Store dynamic field in one table column
Find a way to combine your dynamic field to a string, either use a separator, or use json_encode works, if the column is long enough (TEXT --> MEDIUMTEXT), you can have unlimit dynamic field.
Create enough column at beginning
Apearently this not as good as upper plan, but it's easier to understand, and search in these column is easier too.
BTW, Have you considered convert these dynamic column to rows ?
You need to store the information taken from the form in one table field using an array. That way it should not matter how many question there are.
Should be quite simple take a look at php arrays.
You can have a single longtext datatype column in your database table and you just need to serialize your form values and store in the single column.
I agree with the others. Dynamically creating database tables is bad. The database schema should hardly ever change once a project is complete.
One solution is:
If you had a contact with multiple addresses, you create a parent/child relationship as follows. Perhaps you can do something similiar with your situation.
Contact
long contactID primary key
name
addressID
Address
long addressID primary key
long contactID foreign key
streetAddress
CityID
CountryID
i don't think so it's a good idea to do like this... but i am suggesting you this because in your case it might be useful
you can do it by ALTER...
so what you can do is "When you are creating new field dynamically at that time only you can create new column in your table like this...
ALTER TABLE table_name ADD $name(Column name) //$name may be your attribute name <input name="">
and same way when you are deleting your field at that time you can drop your table column
ALTER TABLE table_name DROP $name(Column name) //$name may be your attribute name
here i assumed that you have already created table table_name (without any fields)
hope it may help you
is better to use ENUM or separate table to get list from possible values (eg html select)?
What is better practice?
when i use SHOW COLUMNS, enum type must by parsed;
OR
when i use separate table to store possible values and use SELECT * ???
When you use enum, you are actually hardcode your list in the database table. So if you need to add something you will need to go and alter the table. On the other hand, when you use a table that you are connecting it through foreighn key, you can add easily a keyword (choice) from the front end application without the need to alter your schema.
I prefer to use enum when the choices are not subject to change and the table with the foreign key relationship when my list is changing over time. Choice is yours.
I hope that this is helpful for you.
The answer is: 'it depends'.
It depends on whether you expect the list of those values to be never (or very rarely) changed, or to be changing frequently.
In the first case, it makes sense to create an ENUM as it defines the list of values as part of data model itself. It explicitly tells the user, what values are allowed.
In the latter case, we deal with data rather than with data model, so it's both more sensible and practical, to have a separate table for these values.
It is really depends on number of options you are planning to store. If there are only 3, I would suggest to use UNUM, otherwise separated table is better. Second table better also if you are going to add more options in the future.
If option are limited and fix use enum with direct ('not active','active','pending')
Otherwise it will be better to make child table for options and store corresponding integer id(Primary key of child table) in parent table.
Refer My Answer :
which is a better design for database status values?
When generating a table from this model:
function init()
{
parent::init();
$this->addField('person_id')->refModel('Model_Person')->mandatory(true);
$this->addField('username')->mandatory(true);
$this->addField('password')->mandatory(true);
}
I get this SQL statement:
create table users (
id int auto_increment not null primary key,
person_id varchar(255),
person int(11),
username varchar(255),
password varchar(255));
In this SQL statement i get the opposite of what is said in the tutorial:
Calling refModel with a field name ending in "_id" will actually create 2 field definitions. "publisher_id", for instance, will be defined as integer and will have type "reference", and a field "publisher" will also be added, with exactly same properties - but it will be a calculated field and will use sub-select to determine the value.
I want to know:
Is the generated SQL statement correct?
What does this VARCHAR additional generated field do? (I made CRUD and when added new records, the value of this field was saved as NULL).
When using refModel(), if i used the model name only ('Person') i got an error (Unable to include Person.php), i had to use the complete class name ('Model_Person'). Is this ok? shouldn't i be able to use the model name only?
The mandatory() doesn't use NOT NULL, is there way to do this?
The generated SQL statement is not correct. It's a bug in generator. You need just one field o type "int" ending with the _id.
The reason why it does this, is because refModel() actually creates two fields in the model, one of which is used for editing (_id) and other is used for listing data (as a sub-query)
When you use refModel, you should use "Model_Person". The consistency between refModel, setModel and other fields will be improved in 4.2, it's not done due to compatibility reasons.
The SQL Generator by it's nature is incomplete and it can't be complete, so it's better that schema is reviewed anyway. For instance you might have some fields which are not defined in "Model". Also I prefer that developers pay attention to SQL as it might not reflects models precisely, one model may use multiple tables through join or models may inherit each then add more field definitions there.
mandatory() is a model-level requirement which works similarly to other validations. While MySQL could handle the "mandatory" condition, it wouldn't be able to handle others. Besides, you may remove "mandatory" when you inherit models.
I'll try to add a guide on effective use of Models in Agile Toolkit.
As a part of a script I'm writing I would like to add a templating system where the variables are derived from a database table like this:
id int auto_increment primary key,
name varchar(200) not null,
value float(6,2) not null
My problems are:
1- I am not able to find a way that makes it user-friendly to display those variables when editing a page & I don't know how many variables there would be so I cannot add all-in-one form.
2- I don't know how the variable should be saved to the database (I'm looking to save a reference of the variable not its value so when it's updated on the table, all pages that uses it will have the new value) ... but using the variable number doesn't seem to be clear on what it holds when someone first looks into the page while editing.
3- Should I use something like preg_replace to replace those variables or is there a better method to do that?
If i understand your question correctly, you are seeking a way to extract column definitions from your database to build a web form regarding to the fields defined in a database table? if this is the case, you can obtain the structure easily with
SHOW COLUMNS
FROM database-table
Even better would be to use the information_schema database (available since MySQL 5.x), because you wont have to parse anything from it:
SELECT *
FROM information_schema.columns
WHERE table_name = "database-table"