I am creating a component for Joomla! 2.5 and I have the following MySQL table structure.
CREATE TABLE `#__table_a` (
id INT NOT NULL AUTO_INCREMENT,
actividad VARCHAR( 255 ),
publish_up DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
publish_down DATETIME NOT NULL
);
CREATE TABLE `#__table_b` (
id INT NOT NULL AUTO_INCREMENT,
table_a_id INT NOT NULL, /*foreign key to table '#__table_a' field 'id'*/
hora TIME NOT NULL,
created DATETIME NOT NULL,
publish_up DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
publish_down DATETIME NOT NULL
);
What I want is that, in the form where I managed data from "# __table_a", I need to be able to manage data that is listed in the table "#__table_b".
How do I do it?,
Ie where do I do the "query" for such data and to display them in the form?, in the "table_a" model or controller?
Related
I am having issues with Go's GORM. When I am trying to save an entity to the DB with a model inside it, it does not save the foreign key with the owner model.
Below is my Models, The MySQL script and the way I save/create the model into the DB.
The error I am getting is: Field 'business_industry_id' doesn't have a default value
type Business struct {
gorm.Model
BusinessName string `json:"BusinessName" binding:"required" gorm:"column:business_name;type:varchar(100);unique;not null"`
BusinessIndustry Industry `json:"BusinessIndustry" binding:"required" gorm:"foreignkey:id"`
}
type Industry struct {
gorm.Model
Name string `json:"Name" gorm:"column:name;type:varchar(100);unique;not null"`
}
CREATE TABLE `industries`
(
id INT(6) AUTO_INCREMENT,
name VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE `businesses`
(
id INT(6) AUTO_INCREMENT,
business_name VARCHAR(100) NOT NULL UNIQUE,
business_industry_id INT(6) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
deleted_at TIMESTAMP,
updated_at TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (business_industry_id) REFERENCES industries (id)
);
err := bs.database.Create(business).Error
I tried remove the Grom attributes from the models, to let the framework figure it out on it's own, but I got the same error.
When I inspect the model, the industry have id of 3 (Because I resolved it earlier on myself) and after I do the save, the ID is 0.
But when I removed the attributes, the id was 3 after the save as well, but the same error occurred.
I know what the error message mean, because the sql message that is logged, doesn't actually insert the 3 into the business_industry_id field. What I don't know is, why it doesn't insert it.
I'm fairly certain you have to include the foreign key, you can't just have the associated model (see http://gorm.io/docs/has_many.html). So you need to do:
type Business struct {
gorm.Model
BusinessName string `json:"BusinessName" binding:"required" gorm:"column:business_name;type:varchar(100);unique;not null"`
BusinessIndustryID uint
BusinessIndustry Industry `json:"BusinessIndustry" binding:"required" gorm:"foreignkey:id"`
}
The plugin I've created has two tables with these structures :
CREATE TABLE notifications (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`sent_at` DATETIME NULL DEFAULT NULL,
`title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`message` TEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
`photo` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`icon` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci'
)
CREATE TABLE notification_recipients(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`notification_id` INT(11) NOT NULL,
`user_id` INT(11) NOT NULL,
`seen` TINYINT(1) NOT NULL,
`archived` TINYINT(1) NOT NULL
)
I can't figure out how to create a relation to show a backend select field where I can select users to get the notification, what should be done in the model.
I will later process the form in the controller in order to create the notification_recipients line to every user selected.
After reading and watching the tutorial about relations, I've come to understand I might need a pivot table.
But I need the notification_recipients table in order to process the notification later to show custom queries in frontend (seen and archived fields will determine the visibility in frontend).
How can I do that ? I don't understand the relationship I'm trying to build, shouw I have a third table ?
Thank you, best regards.
Update (solution found) :
I added a Checkbox List named "users".
According to the docs of OctoberCMS, you can add options from the model to a Radio field, and Checkbox Lists work the same way.
For now, I got it working by doing this in the Model used for the form:
use RainLab\User\Models\User;
...
public function getUsersOptions($value, $formData)
{
$users = User::all();
$mapped = $users->mapWithKeys(function ($item) {
return [$item['id'] => $item['name']];
});
return $mapped;
}
It worked great and I'm getting all users with their ids.
I am new to Spring Boot and Vaadin. I followed a tutorial to create CRUD pages for a phone book application however I am having trouble using foreign keys. I have a Contact table which has phone type (i.e. cell or home) as a foreign key - i.e. it is referenced to my PhoneType table. I am stuck on how to populate the phone type from a drop down of values populated in my PhoneType table. Right now I am I have the following member variable in my Contact class
#ManyToOne
#JoinColumn(name="type")
private PhoneType phoneType;
And in my PhoneType class I have
#Column(name = "type")
private String phoneType;
However I am getting an error that says "Error executing DDL via JDBC Statement".
The rest of the application works well with the CRUD pages.
Firstly in mySQL implementations you can't store actual objects unless you use 8.0+ JSON data type. SQL has no idea what a PhoneType is because it's an object and not a valid data type. https://www.w3schools.com/sql/sql_datatypes.asp
If you want to store actual objects you need to find a noSQL implementation that you like.
So your "Customer" class doesn't map to a table properly. You would need to make instance variables such as
String hasCellPhone, hasHomePhone; //etc for the options in your dropdown menu
instead of trying to put a phonetype object.
I asked almost the exact same question, I suggest you read this entire thread.
https://stackoverflow.com/a/50879597/5468597
create table item (
barcode bigint not null auto_increment primary key,
name varchar(20) not null,
type varchar(20) not null,
is_available boolean not null,
is_late boolean null,
notes varchar(255) null,
check_out_date datetime null,
due_date datetime null
#create index idx_barcode (barcode));
create table patron (
trinity_id bigint not null primary key,
name varchar(30) not null,
email varchar(20) not null,
owes_fines boolean not null,
fines_owed int null
#create index idx_trinity_id (trinity_id));
create table checked_out_items (
ref_id bigint primary key auto_increment not null,
patron_id bigint not null,
item_id bigint not null,
item_available boolean not null,
item_check_out_date datetime null,
item_due_date datetime null);
alter table checked_out_items
add constraint fk_patron_id
foreign key (patron_id) references patron(trinity_id),
add constraint fk_item_id
foreign key (item_id) references item(barcode)
#add constraint fk_item_available
#add constraint fk_check_out_date
#add constraint fk_due_date
#foreign key (item_available references item(is_available)
#foreign key (item_check_out_date) references item(check_out_date)
#foreign key (item_due_date) references item(due_date)
on update cascade
on delete cascade;
insert into patron values(0000000,'Test Erino','test#erino.edu',0,null);
insert into item values(1,'Chromebook','Laptop',0,null,null,null,null);
insert into checked_out_items(patron_id,item_id,item_available,item_check_out_date,item_due_date)
select patron.trinity_id,item.barcode,item.is_available,item.check_out_date,item.due_date
from patron
inner join item;
and lastly:
select * from item;
select * from patron;
select * from checked_out_items;
I won't post the java logic here. That's for you to read in the other thread.
I solved my question.
#ManyToOne (cascade = {CascadeType.ALL})
#JoinColumn(name="phoneType_typeId")
private PhoneType phoneType;
And
#Id
#GeneratedValue
#Column(name = "typeId")
private Long id;
I am using ordinary search functionality in my business controller . but
now need to implement FULL TEXT SEARCH with paginate can any one give idea or sample ?
I am using MySQL with MyISAM tabes
and this my table structure, fields marked bold are need to use in
search
CREATE TABLE IF NOT EXISTS businesses (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
created date NOT NULL,
modified datetime NOT NULL,
user_id bigint(20) NOT NULL,
slug varchar(255) NOT NULL,
**`name` varchar(255) NOT NULL,**
**street_name varchar(255) DEFAULT NULL,**
**shopping_center varchar(255) DEFAULT NULL,**
state_id int(10) NOT NULL,
suburb_id int(10) NOT NULL,
zip_code varchar(12) DEFAULT NULL,
website varchar(250) DEFAULT NULL,
email varchar(255) DEFAULT NULL,
phone_no varchar(255) NOT NULL,
mobile_no varchar(255) DEFAULT NULL,
fax varchar(255) DEFAULT NULL,
is_active tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
After you have created your FULLTEXT indices it's not different at all from normal paginate conditions, you just need to construct your SQL a little more manually and most importantly escape the user input manually.
$query = 'foobar'; // the user input to search for
$escapedQuery = $this->Business->getDataSource()->value($query);
$this->paginate['conditions'] = array("MATCH (Business.name) AGAINST ($escapedQuery)");
$businesses = $this->paginate();
I'm new to Joomla! and also new to the component development.
I want to create a component which be able to link between 2 tables.
joomla v 1.6:
A table's structure:
CREATE TABLE `#__a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
B table's structure:
CREATE TABLE `#__b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
AB table's structure:
CREATE TABLE `#__ab` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_a` int(11) NOT NULL,
`id_b` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
Assume that we have already created the basic create, edit page and delete action with the MVC for A and B (created from the hello world component). From the hellow world tutorial we could know clear about the file and folder structure and the componet's code, but in the tutorial there is only 1 table, but here there is 2 tables and also new id id table.
So it means that in the component we're developing here, there are 2 submenus.
In the edit or new page
There is A details block which we can fill name.(done from the tutorial)
There is B linking block which we can choose the B (select option, can select more then 1) to add into the A
and also display list the B we added. In every item in the list, it has a delete button or link to unlink between the A and B.
Any ideas how to do please?
Thanks and Best Regards Friends,
Rithy
First you need some logic that will save the results in the reference table and the second part is to retrieve the data to be shown on the user side. You need a Model:
class CompControllerA extends JControllerForm {
// Here put your save code
//....
function save() {
$formData = JRequest::getVar('jform');
$bRecords = $formData['bRecords'];
$aRecordId = $formData['id'];
$referenceModel->delete($aRecordId); // Delete all records that has same ID as current record
foreach($bRecords as $row) {
$data['id']=0;
$data['a_id']=$aRecordId;
$data['b_id']=(int)$row;
$bModel->save($data);
}
// dont forget to call parent method
parent::save();
}
}
Next step is when you create the for just take the results from the reference table and show the correct results in the form.
Here is some example code of a component I've built.
class IbookModelExtra extends JModelAdmin
{
protected function loadFormData()
{
$db =& $this->getDbo();
$query = $db->getQuery(true);
$query->select('b_id')->from('#__table_a_b')->where('a_id='.$data->id);
$db->setQuery((string)$query);
$data->b = $db->loadResultArray();
}
}