I have the following script for creating three tables in MariaDB:
CREATE TABLE `booking` (
`booking_id` int(11) NOT NULL,
`booking_date` date DEFAULT NULL,
`room_no` int(11) DEFAULT NULL,
`guest_id` int(11) NOT NULL,
`occupants` int(11) NOT NULL DEFAULT '1',
`room_type_requested` varchar(6) DEFAULT NULL,
`nights` int(11) NOT NULL DEFAULT '1',
`arrival_time` varchar(5) DEFAULT NULL,
PRIMARY KEY (`booking_id`),
KEY `room_no` (`room_no`),
KEY `guest_id` (`guest_id`),
KEY `room_type_requested` (`room_type_requested`,`occupants`),
CONSTRAINT `booking_ibfk_1` FOREIGN KEY (`room_no`) REFERENCES `room` (`id`),
CONSTRAINT `booking_ibfk_2` FOREIGN KEY (`guest_id`) REFERENCES `guest` (`id`),
CONSTRAINT `booking_ibfk_3` FOREIGN KEY (`room_type_requested`) REFERENCES `room_type` (`id`),
CONSTRAINT `booking_ibfk_4` FOREIGN KEY (`room_type_requested`, `occupants`) REFERENCES `rate` (`room_type`, `occupancy`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `rate` (
`room_type` varchar(6) NOT NULL DEFAULT '',
`occupancy` int(11) NOT NULL DEFAULT '0',
`amount` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`room_type`,`occupancy`),
CONSTRAINT `rate_ibfk_1` FOREIGN KEY (`room_type`) REFERENCES `room_type` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `room_type` (
`id` varchar(6) NOT NULL,
`description` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to add another table, accommodation_payments. I want it to be linked to the amount in the rate table. How to define my foreign key (lets call it room_type_booked)?
Is it better to link it to the booking table or to the room_type table similar to how the FK room_type_requested in the booking table was created (see the latter option in the ERD above)?
Why are there are two constraints for room_type_requested in booking?
CREATE TABLE `accommodation_payments` (
`acc_pmt_id` int(11) NOT NULL,
`room_type_booked` varchar(6) DEFAULT NULL,
`payment_date` date DEFAULT NULL,
`payment_method` varchar(30) NOT NULL,
`pmt_amount` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`acc_pmt_id`),
KEY `room_type_booked` (`room_type_booked`),
CONSTRAINT `acc_pmt_ibfk_1` FOREIGN KEY (`room_type_booked`) REFERENCES `booking` (`room_type_requested`));
Related
I have a query that does what is required
SELECT v.* FROM vehicle v
WHERE v.company_id = 2 AND v.id NOT IN
(
SELECT h.vehicle_id
FROM hire h
WHERE
h.start_date is not null and h.end_date is null
)
So now I am trying to code this query in Symfony/Doctrine
I have this
$qb = $this->_em->createQueryBuilder();
$subq = $this->_em->createQueryBuilder();
$subq ->select('h.vehicle')
->from('AppBundle\Entity\Hire', 'h')
->andWhere('h.startDate is not null and h.endDate is null');
$qb->select('v')
->from('AppBundle\Entity\Vehicle', 'v')
->where($qb->expr()->notIn('v.id',$subq->getDQL()))
->andWhere('v.company = :company')
->setParameter('company', $company)
->orderBy('v.registrationNumber', 'ASC')
;
$t = $qb->getDQL();
return $qb;
As you see I tried dumping the DQL to see if that gave me any clues and here is is
SELECT v
FROM AppBundle\Entity\Vehicle v
WHERE (v.id NOT IN(SELECT h.vehicle FROM AppBundle\Entity\Hire h WHERE h.startDate is not null and h.endDate is null))
AND v.company = :company
ORDER BY v.registrationNumber ASC
I tried converting this back to simple SQL i.e. removing the AppBundle stuff and converting the column names back to actual column name and it runs and get the right result.
But I am getting this error
Doctrine\ORM\Query\QueryException:
[Semantical Error] line 0, col 69 near 'vehicle FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
at vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php:63
at Doctrine\ORM\Query\QueryException::semanticalError('line 0, col 69 near \'vehicle FROM\': Error: Invalid PathExpression. Must be a StateFieldPathExpression.', object(QueryException))
The 2 tables involved are
CREATE TABLE `hire` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) DEFAULT NULL,
`driver_id` int(11) DEFAULT NULL,
`vehicle_id` int(11) DEFAULT NULL,
`corporate_hire` tinyint(1) DEFAULT NULL,
`end_date` datetime DEFAULT NULL,
`end_miles` decimal(10,1) DEFAULT NULL,
`start_date` datetime DEFAULT NULL,
`start_miles` decimal(10,1) DEFAULT NULL,
`created_by` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`updated_by` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`deleted_by` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_B8017EFC979B1AD6` (`company_id`),
KEY `IDX_B8017EFCC3423909` (`driver_id`),
KEY `IDX_B8017EFC545317D1` (`vehicle_id`),
CONSTRAINT `FK_B8017EFC545317D1` FOREIGN KEY (`vehicle_id`) REFERENCES `vehicle` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_B8017EFC979B1AD6` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_B8017EFCC3423909` FOREIGN KEY (`driver_id`) REFERENCES `driver` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and
CREATE TABLE `vehicle` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_id` int(11) DEFAULT NULL,
`storage_id` int(11) DEFAULT NULL,
`created_by` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`updated_by` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`deleted_by` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`date_of_manufacture` datetime DEFAULT NULL,
`engine_size` int(11) DEFAULT NULL,
`make` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`model` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`registration_number` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`vin` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_1B80E486979B1AD6` (`company_id`),
KEY `IDX_1B80E4865CC5DB90` (`storage_id`),
CONSTRAINT `FK_1B80E4865CC5DB90` FOREIGN KEY (`storage_id`) REFERENCES `storage` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_1B80E486979B1AD6` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I am not sure what an Invalid Path Expression should be making me look for as an actual error.
What's working for us is (adapted to your example, so not tested):
$subQuery->select('IDENTITY(h.vehicle)');
$subQuery->from('AppBundle\Entity\Hire', 'h');
$dqlString = $subQuery->getQuery()->getDQL();
$query->andWhere('v.id' 'NOT IN (' . $dqlString . ')');
The only two differences that I see are:
subQuery returns IDENTITY() of h.vehicle, so only the 'identifier' (which is id in most cases)
our andWhere condition in main query is created manually - but your expression builder should work the same way.
So my assumption ist that your $subq ->select('h.vehicle') doesn't return ids which can be used in the NOT IN condition of your main query, which might be proven by your resolved DQL SELECT v : this will return all fields, but you'd required v.id only.
Can someone please help? I'm trying to create shopping cart and have this SQL, but I want it in Entity:
CREATE TABLE `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`phone` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`address` text COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`price` float(10,2) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) NOT NULL,
`total_price` float(10,2) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `customer_id` (`customer_id`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `order_items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`quantity` int(5) NOT NULL,
PRIMARY KEY (`id`),
KEY `order_id` (`order_id`),
CONSTRAINT `order_items_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
How could I create order_items in doctrine? It's many to many relation I think, but with 2 more columns.
Thank you
In Doctrine, if you want to have attributes attached to a Many To Many relationship, simply just create another entity. This entity will have two M-1 relationships linked to the other two entities, in the scenario of your question, you can have an OrderItem entity, which are associated with Orders and Products table via 1-M relationship (basically two foreign keys in SQL) and as many attributes for itself as you want.
I'm trying to implement this translation example (https://gist.github.com/tristanbes/2116290) with SonataAdminBundle & DoctrineExtensions, but when I run the create action form, the database translation field object_id is empty. All other fields are filled correctly on database!
My database schema:
CREATE TABLE `Book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` longtext,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_6BD70C0F5E237E06` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
CREATE TABLE `book_translations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`object_id` int(11) DEFAULT NULL,
`locale` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
`field` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`content` longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`),
UNIQUE KEY `lookup_unique_idx` (`locale`,`object_id`,`field`),
KEY `IDX_9F5610DF232D562B` (`object_id`),
CONSTRAINT `FK_9F5610DF232D562B` FOREIGN KEY (`object_id`) REFERENCES `Book` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Can anyone help me? I have the same source!!!
Thanks in advance,
Luis Miguens
Warning: spl_object_hash() expects parameter 1 to be object, string given in
/var/www/sitetwo/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1367
I created MainBlogBundle with Category.php and Product.php using yml mapping annotation via php app/console commands. After using CRUD actions for add/edit/delete/show actions I tried to add a category and after submitting Add category form I get the
Warning: spl_object_hash() expects parameter 1 to be object, string given in
/var/www/sitetwo/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1367
I posted my sample code on github and below is the script of database.
git#github.com:veerpartap/ProblemSymfony.git
/****************************************************************************/
-- phpMyAdmin SQL Dump
-- version 3.4.10.1deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 26, 2013 at 01:56 PM
-- Server version: 5.5.32
-- PHP Version: 5.5.3-1+debphp.org~precise+2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `sitetwo`
--
-- --------------------------------------------------------
--
-- Table structure for table `category`
--
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `Company`
--
CREATE TABLE IF NOT EXISTS `Company` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`address` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`owner_name` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`status` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `Company`
--
INSERT INTO `Company` (`id`, `company_name`, `address`, `owner_name`, `status`, `created`) VALUES
(1, 'My First Company', 'Street 5A Sector 85 Chandigarh 1665588', 'Mr. Prateek Kumar', 1, '2013-09-06 00:00:00'),
(2, 'My Second Private Company', 'Street 34N Sector 89, Chandigarh 165898', 'Mr. Saurabh Shuja', 1, '2013-09-07 00:00:00');
-- --------------------------------------------------------
--
-- Table structure for table `Post`
--
CREATE TABLE IF NOT EXISTS `Post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`body` longtext COLLATE utf8_unicode_ci NOT NULL,
`published` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `posts`
--
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`body` longtext COLLATE utf8_unicode_ci NOT NULL,
`published` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `products`
--
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) DEFAULT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`price` decimal(10,0) NOT NULL,
`description` longtext COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_B3BA5A5A12469DE2` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Table structure for table `User`
--
CREATE TABLE IF NOT EXISTS `User` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(150) NOT NULL,
`last_name` varchar(150) NOT NULL,
`sex` tinyint(1) DEFAULT NULL,
`date_of_birth` datetime DEFAULT NULL,
`education` varchar(10) NOT NULL,
`mobile` varchar(10) NOT NULL,
`email` varchar(100) NOT NULL,
`address` varchar(200) NOT NULL,
`status` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `User`
--
INSERT INTO `User` (`id`, `first_name`, `last_name`, `sex`, `date_of_birth`, `education`, `mobile`, `email`, `address`, `status`) VALUES
(1, 'Veerpartap', 'Singh', 1, '2008-11-24 00:00:00', 'MCA', '71505897', 'metveerpartapsingh#gmail.com', 'hl 99 phase 2 sas nagar mohali', 1),
(2, 'Vicky', 'Sharma', 1, '2008-05-09 00:00:00', 'MCA', '88754257', 'vicky.sharma#gmail.com', 'Village Burari, Jila Nawanshar', 1);
--
-- Constraints for dumped tables
--
--
-- Constraints for table `products`
--
ALTER TABLE `products`
ADD CONSTRAINT `FK_B3BA5A5A12469DE2` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`);
/****************************************************************************/
As stated in the above comment, the issue was about that I am not including the Arrarycollection namespace in the controller file. But after including the namespace I get another error while adding new products.
Below is the error message :
"__toString()" method was not foudn on the object of type Main\BlogBundle\Enity\Category to the choice field.
For this error, We need to add a __toString() method to your Category entity. For example:
public function __toString() { return $this->name; }
The PHP magic-method __toString() is used to present a textual representation of the object. In this case, the Category name will be used when selecting a Category in a Form of a related entity.
if you getthe __toString() error in a class XxxxxType extends AbstractType
you can add the fiel definition in the builder, like this. No need to modify your entity.
$builder
->add('enquete','entity',array('class' => 'AdequatSipBundle:Enquete',
'property' => 'Id', 'read_only'=>true))
->add('produit','entity',array('class' => 'AdequatSipBundle:Produit',
'property' => 'Name', 'read_only'=>true))
I am importing database and i got an error
1062 - Duplicate entry '1' for key PRIMARY.
i tried to upload table individually But it gives me error for those table which have at least one row.
i don't want to delete any record of any table.
here is one table which is giving error.
CREATE TABLE IF NOT EXISTS `wpbeta_aff_affiliates` (
`affiliate_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(512) DEFAULT NULL,
`from_date` date NOT NULL,
`thru_date` date DEFAULT NULL,
`status` varchar(10) NOT NULL DEFAULT 'active',
`type` varchar(10) DEFAULT NULL,
PRIMARY KEY (`affiliate_id`),
KEY `affiliates_afts` (`affiliate_id`,`from_date`,`thru_date`,`status`),
KEY `affiliates_sft` (`status`,`from_date`,`thru_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `wpbeta_aff_affiliates`
--
INSERT INTO `wpbeta_aff_affiliates` (`affiliate_id`, `name`, `email`, `from_date`, `thru_date`, `status`, `type`) VALUES
(1, 'Direct', NULL, '2013-07-03', NULL, 'active', 'direct');
You do not need to specify the record id (with the exception of some circumstances when you need to keep the connection between records in different tables). Try using this query:
INSERT INTO wpbeta_aff_affiliates (name, email, from_date, thru_date, status, type) VALUES ('Direct', NULL, '2013-07-03', NULL, 'active', 'direct');