I have two tables such as Employee and DeletedEmployee.
What I need is while am deleting data in employee table, that specific data must be move to deletedemployee table.
DeletedEmployee table's structure is as below :
id int(11) NOT NULL AUTO_INCREMENT,
emp_name varchar(45) NOT NULL,
emp_age varchar(45) NOT NULL,
emp_address varchar(45) NOT NULL,
emp_phone varchar(45) NOT NULL,
emp_designation varchar(45) NOT NULL,
PRIMARY KEY (id)
Employee
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL,
age varchar(45) NOT NULL,
address varchar(45) NOT NULL,
phone varchar(45) NOT NULL,
designation varchar(45) NOT NULL,
PRIMARY KEY (id)
You can use ActiveRecord::afterDelete(). Since the current object has not yet been destroyed you can get the required fields. Assuming you have a model DeletedEmployee you can do the following in your Employee model:
public function afterDelete() {
$deletedEmployee = new DeletedEmployee;
$deletedEmployee->id = $this->id.
//assign other attributes
$deletedEmployee->save();
parent::afterDelete();
}
The trigger , as the name suggests can be used to initiate some action when some event occurs like for eg : Insert,Update,Delete,etc.
The query could be as follows :
CREATE OR REPLACE TRIGGER trgAfterDelete
AFTER DELETE ON Employee
AS
insert into DeletedEmployee values(val1,val2,val3,....)
PRINT 'AFTER DELETE TRIGGER fired.'
GO
You can learn to enable the trigger from this link.
Related
I need to use my Website model to get a row from my database within the websites table, however this row is identified through my domains table.
So basically it would be great to do a query on my domains table and match the row, then from that get the website row from the websites table using the website_id column.
But I want to simply pass this data into my controller by just referencing the Model within the method.
class WebsiteController extends Controller {
public function index(Website $website) {
print_r($website);
return view('index');
}
}
My domains table:
CREATE TABLE `domains` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`website_id` INT(11) NOT NULL DEFAULT '0',
`domain` VARCHAR(255) NOT NULL DEFAULT '0',
`active` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `website_id` (`website_id`),
CONSTRAINT `website_id` FOREIGN KEY (`website_id`) REFERENCES `websites` (`id`)
)
COMMENT='This table will contain all of the domains registered on MarvWeb, this will link to the website record. '
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3;
And websites table:
CREATE TABLE `websites` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL DEFAULT NULL,
`tagline` VARCHAR(255) NULL DEFAULT NULL,
`description` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COMMENT='This table will contain all the websites data. '
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2;
Does this make sense?
Add a website function to your Domain model.
class Domain extends Model{
public function website(){
return $this->hasOne('App\Website');
}
// remainder of model.
}
When you retrieve the Domain query results, the website can be accessed by
print_r($domainRowResult->$website->tagline);
I have the following PHP page to create a table using a text file.
table_create.php
<?php
include $db;
$query_file = "sql.txt";
$fp = fopen($query_file, 'r');
$sql = fread($fp, filesize($query_file));
fclose($fp);
$retval = mysql_query($sql);
if(! $retval )
{
die("Could not create the tables<br>");
}
echo "Table created successfully<br>";
?>
sql.txt
CREATE TABLE ht_account (
id int(11) NOT NULL AUTO_INCREMENT,
date date NOT NULL,
type varchar(50) NOT NULL,
mode varchar(50) NOT NULL,
party varchar(50) NOT NULL,
payee varchar(50) NOT NULL,
rate decimal(13,2) NOT NULL,
box int(11) NOT NULL,
amount decimal(13,2) NOT NULL,
token varchar(50) NOT NULL,
remarks varchar(50) NOT NULL,
user varchar(50) NOT NULL,
user_confirm varchar(50) NOT NULL,
status varchar(50) NOT NULL);
CREATE TABLE ht_bank (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
ac_no varchar(50) NOT NULL,
address varchar(50) NOT NULL);
CREATE TABLE ht_user_role (
id int(11) NOT NULL AUTO_INCREMENT,
value varchar(50) NOT NULL);
When I try to create a single table in the sql.txt file, the code works perfectly.
For example:
CREATE TABLE ht_account (
id int(11) NOT NULL AUTO_INCREMENT,
date date NOT NULL,
type varchar(50) NOT NULL,
mode varchar(50) NOT NULL,
party varchar(50) NOT NULL,
payee varchar(50) NOT NULL,
rate decimal(13,2) NOT NULL,
box int(11) NOT NULL,
amount decimal(13,2) NOT NULL,
token varchar(50) NOT NULL,
remarks varchar(50) NOT NULL,
user varchar(50) NOT NULL,
user_confirm varchar(50) NOT NULL,
status varchar(50) NOT NULL);
But when I try to create multiple tables, It does not create any table. I doubt that the format in the sql.txt may be incorrect.
The format is, almost sure, correct but mysql_query doesn't work with multiple queries:
mysql_query() sends a unique query (multiple queries are not
supported) to the currently active database on the server that's
associated with the specified link_identifier.
It's better to use mysqli functions because mysql ones are deprecated for PHP 5.5 and mysqli has the function mysqli_multi_query that you need.
If you still want to use mysql functions you could do something like:
$sql_array=explode(';',$sql);
foreach ($sql_array as $s) {
if(! mysql_query($s)){
echo mysql_error()."<br>";
}
}
I am trying to create a table in a php script and I am getting an SQL error. I can't find it or figure out how to fix it. I do have $table defined earlier in the script. Thanks in advance!
$newtable = "CREATE TABLE $table (id INT(11) NOT NULL AUTO_INCREMENT, time datetime NOT NULL,punchtype VARCHAR(255) NOT NULL,groupname VARCHAR(255) NOT NULL, dept VARCHAR(255) NOT NULL, notes VARCHAR(255))";
Error Message: Incorrect table definition; there can be only one auto column and it must be defined as a key
try this, it works
$newtable = "CREATE TABLE $table
(id INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
time datetime NOT NULL,
punchtype VARCHAR(255) NOT NULL,
groupname VARCHAR(255) NOT NULL,
dept VARCHAR(255) NOT NULL,
notes VARCHAR(255))";
My server is on hostgator running on a linux centOS.
I'm simply trying to create a table within my database and I figured out how to get the table to get created. Although when I add the AUTO_INCREMENT setting the code doesn't execute and the table isn't created.
Why would this be and how can I correct it?
Here is my code:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";
To use AUTO_INCREMENT you may have to assign the column as a primary key:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL,
PRIMARY KEY (id))";
Your query will give error
there can be only one auto column and it must be defines as a key, so add primary key to id field
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";
I am new to this and need a little help. I have a 2 tables...
CREATE TABLE `vehicles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vehicle_type` varchar(50) NOT NULL,
`vehicle_make` varchar(50) NOT NULL,
`vehicle_model` varchar(50) NOT NULL,
`vehicle_year` varchar(50) NOT NULL,
`vin` varchar(50) NOT NULL,
`registered_state` varchar(10) NOT NULL,
`license_plate` varchar(20) NOT NULL,
`insurrance_policy` varchar(50) NOT NULL,
PRIMARY KEY(`id`)
)
ENGINE=INNODB;
CREATE TABLE `drivers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`dob` date NOT NULL,
`ss_no` varchar(50) NOT NULL,
`address` varchar(100) NOT NULL,
`city` varchar(50) NOT NULL,
`state` varchar(10) NOT NULL,
`zip_code` int(5) NOT NULL,
`cell_phone` varchar(50) NOT NULL,
`home_phone` varchar(50),
`dl_no` varchar(50) NOT NULL,
`dl_state` varchar(10) NOT NULL,
`dl_exp` date NOT NULL,
`dl_2_no` varchar(50) NOT NULL,
`dl_2_state` varchar(10) NOT NULL,
`dl_2_exp` date NOT NULL,
`vehicle_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY(`id`),
CONSTRAINT `Ref_01` FOREIGN KEY (`vehicle_id`)
REFERENCES `vehicles`(`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE=INNODB;
SET FOREIGN_KEY_CHECKS=1;
As you can see every driver has a car associated with him. How can I query every driver and every car that is associated with him in an object.
I can get all the drivers using this.
$d = new Driver();
$data['driver'] = $d->get();
In my model for driver has
var $has_one = array('vehicle');
I want to get all the records in $data['driver']
Here's one simple way, assuming you have one-to-one relationship:
In your Model Driver:
var $has_one = array('vehicle');
In your Model Vehicle:
var $has_one = array('driver');
To get a vehicle and its driver:
$v = new Vehicle();
$v->include_related('driver')->get();
include_related() will only work with $has_one related models.
Driver's properties are now stored in $v with the prefix driver_. To access the vehicle's driver columns:
echo $v->driver_first_name;
echo $v->driver_last_name;
Alternatively, you can auto-load the driver every time you access the vehicle:
// In Vehicle
var $auto_populate_has_one = TRUE;
For has-many relationships, you can do this:
$d = new Driver();
$d->get_by_id($id);// Get a driver by id
foreach ($d->vehicle->get() as $car)
{
// Print all this Driver's vehicles
echo $car->vehicle_model;
}
This is just one way to do it. There are so may ways to access relationships in Datamapper, your best bet is to read the documentation thoroughly, then read it again. Datamapper is a great ORM and you should learn and experiment with all it's features to get the most out of it.
This could be a good opportunity to use a Foreign Key in your Drivers Table. Here is a link to the MySQL documentation on using Foreign Keys in your table: MySQL Foreign Keys
Alternatively if you don't want to deal with the hassles of advanced MySQL calls you could also just process two queries, one to your vehicles table, and one to your drivers table, and then use PHP to iterate (loop) through the results until you can find and match the correct values with each other.
Actually both ways are probably a bit of a hassle to set up, but your foreign key would likely be the easier to maintain.