symfony2 / doctrine 2 / Mysql : null values saved as empty strings - php

I have a basic symfony2 form where I can set a value for my field.
I've just figured out that null values where saved as empty strings, where I expect an exception to be raised because thenull value is not accepted.
My mapping (within a trait) :
/**
* #ORM\Column(name="name", type="string", length=100, nullable=false)
*/
protected $name;
My field :
->add('name', null, array(
'label'=>'Nom détaillé',
'required' => false))
Setting required to true and defining assert rules works well but here I'm trying to solve this conversion issue.
If I put a vardumpin the SetName method, I do get a null value which is later saved as an empty string in my database. How can I solve that ?
EDIT : the show create table t
CREATE TABLE `recipe` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`isProduct` tinyint(1) NOT NULL,
`portions` int(11) DEFAULT NULL,
`nickname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`shortDescription` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
`weight` decimal(10,2) DEFAULT NULL,
`isPrivate` tinyint(1) NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`content` longtext COLLATE utf8_unicode_ci,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
`viewCount` int(11) NOT NULL,
`parentId` int(10) unsigned DEFAULT NULL,
`userId` int(10) unsigned DEFAULT NULL,
`createdBy` int(10) unsigned DEFAULT NULL,
`updatedBy` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_DD24B40110EE4CEE` (`parentId`),
KEY `IDX_DD24B40164B64DCC` (`userId`),
KEY `IDX_DD24B401D3564642` (`createdBy`),
KEY `IDX_DD24B401E8DE7170` (`updatedBy`),
CONSTRAINT `FK_DD24B40110EE4CEE` FOREIGN KEY (`parentId`) REFERENCES `recipe` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_DD24B40164B64DCC` FOREIGN KEY (`userId`) REFERENCES `user` (`id`),
CONSTRAINT `FK_DD24B401D3564642` FOREIGN KEY (`createdBy`) REFERENCES `user` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_DD24B401E8DE7170` FOREIGN KEY (`updatedBy`) REFERENCES `user` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Seems like you just need to add a strict equivalence check === against the variable and then throw new Exception() if it's null

Related

Key exists in table but shows Can't Drop foreign key in MySQL

The result of "SHOW CREATE TABLE order_products" are
CREATE TABLE `order_products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`quantity` int(11) NOT NULL,
`price` double DEFAULT NULL,
`amount` double(8,2) NOT NULL,
`product_id` int(10) unsigned NOT NULL,
`tax_id` int(10) unsigned DEFAULT NULL,
`discount_id` int(10) unsigned DEFAULT NULL,
`order_id` int(10) unsigned DEFAULT NULL,
`username` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`shop_name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `order_products_product_id_foreign` (`product_id`),
KEY `order_products_tax_id_foreign` (`tax_id`),
KEY `order_products_discount_id_foreign` (`discount_id`),
KEY `order_products_order_id_foreign` (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
And i try to delete my key "order_products_product_id_foreign" by
ALTER TABLE order_products DROP FOREIGN KEY order_products_product_id_foreign
But it gives me error
#1091 - Can't DROP 'order_products_product_id_foreign'; check that column/key exists
You don't need to specify the FOREIGN KEY keyword here.
alter table order_products drop key order_products_product_id_foreign;
It's not a foreign key. so execute the line.
ALTER TABLE order_products DROP KEY order_products_product_id_foreign

Not able to assign On Delete Set null and on updated set Null in mysql

bank_api_uat_user Table
CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bank_name` varchar(255) DEFAULT NULL,
`role` varchar(10) NOT NULL,
`bank_code` char(10) NOT NULL,
`user_name` varchar(255) NOT NULL,
`password` varchar(20) NOT NULL,
`api_key` varchar(255) NOT NULL,
`Client_Secret` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_name` (`user_name`),
KEY `user_name_2` (`user_name`),
KEY `id` (`id`,`user_name`),
KEY `role` (`role`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;
create_role Table
CREATE TABLE IF NOT EXISTS `create_role` (
`date` datetime NOT NULL,
`Role_name` varchar(50) NOT NULL,
`Role_code` varchar(5) NOT NULL,
PRIMARY KEY (`Role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to assign referential integrity to bank_api_uat_user table each time i add constraint it gives below error
MySQL said: Documentation
1215 - Cannot add foreign key constraint
Below query used to create foreign key.
ALTER TABLE `bank_api_uat_user` ADD CONSTRAINT `const_file_role` FOREIGN KEY (`role`) REFERENCES `test`.`create_role`(`Role_code`) ON DELETE SET NULL ON UPDATE SET NULL;
Its important to make a child column Null for setting null reference option.
the above queries work if role is declared as NULL
CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bank_name` varchar(255) DEFAULT NULL,
`role` varchar(10) NULL,
`bank_code` char(10) NOT NULL,
`user_name` varchar(255) NOT NULL,
`password` varchar(20) NOT NULL,
`api_key` varchar(255) NOT NULL,
`Client_Secret` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_name` (`user_name`),
KEY `user_name_2` (`user_name`),
KEY `id` (`id`,`user_name`),
KEY `role` (`role`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;
Full explanation can be find here : https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html

laravel change nullable column to no nullable in foreign key

This is my migration:
$table->increments('id');
$table->string('nombre1',255);
$table->string('nombre2',255)->nullable();
$table->string('apellido1',255)->nullable();
$table->string('apellido2',255)->nullable();
$table->string('apellido_casada',255)->nullable();
$table->string('cedula',255)->nullable();
$table->integer('entidad_id')->nulleable()->unsigned();
**$table->integer('cargo_id')->nulleable()->unsigned();
$table->integer('partido_id')->nulleable()->unsigned();
$table->integer('provincia_id')->nulleable()->unsigned();
$table->integer('distrito_id')->nulleable()->unsigned();
$table->integer('corregimiento_id')->nulleable()->unsigned();**
$table->string('otro_nombre',255)->nullable();
$table->dateTime('fecha_nacimiento')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('entidad_id')->references('id')->on('entidad');
$table->foreign('cargo_id')->references('id')->on('cargo');
$table->foreign('partido_id')->references('id')->on('partido');
$table->foreign('provincia_id')->references('id')->on('provincia');
$table->foreign('distrito_id')->references('id')->on('distrito');
$table->foreign('corregimiento_id')->references('id')->on('corregimiento');
When i run this migration the table generated is:
CREATE TABLE pep (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
nombre1 VARCHAR(255) COLLATE UTF8_UNICODE_CI NOT NULL,
nombre2 VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
apellido1 VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
apellido2 VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
apellido_casada VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
cedula VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
**entidad_id INT(10) UNSIGNED NOT NULL,
cargo_id INT(10) UNSIGNED NOT NULL,
partido_id INT(10) UNSIGNED NOT NULL,
provincia_id INT(10) UNSIGNED NOT NULL,
distrito_id INT(10) UNSIGNED NOT NULL,
corregimiento_id INT(10) UNSIGNED NOT NULL,**
otro_nombre VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
fecha_nacimiento DATETIME DEFAULT NULL,
created_at TIMESTAMP NULL DEFAULT NULL,
updated_at TIMESTAMP NULL DEFAULT NULL,
deleted_at TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (id),
KEY pep_entidad_id_foreign (entidad_id),
KEY pep_cargo_id_foreign (cargo_id),
KEY pep_partido_id_foreign (partido_id),
KEY pep_provincia_id_foreign (provincia_id),
KEY pep_distrito_id_foreign (distrito_id),
KEY pep_corregimiento_id_foreign (corregimiento_id),
CONSTRAINT pep_cargo_id_foreign FOREIGN KEY (cargo_id)
REFERENCES cargo (id),
CONSTRAINT pep_corregimiento_id_foreign FOREIGN KEY (corregimiento_id)
REFERENCES corregimiento (id),
CONSTRAINT pep_distrito_id_foreign FOREIGN KEY (distrito_id)
REFERENCES distrito (id),
CONSTRAINT pep_entidad_id_foreign FOREIGN KEY (entidad_id)
REFERENCES entidad (id),
CONSTRAINT pep_partido_id_foreign FOREIGN KEY (partido_id)
REFERENCES partido (id),
CONSTRAINT pep_provincia_id_foreign FOREIGN KEY (provincia_id)
REFERENCES provincia (id)
) ENGINE=INNODB DEFAULT CHARSET=UTF8 COLLATE = UTF8_UNICODE_CI;
The problem is that some of the columns: entidad_id, cargo_id, etc, are declared as NULL and when i set the FK, laravel created them as NOT NULL.
Is anyting here that i am missing??
Sorry for being late.
You have typos in code
nulleable
$table->integer('entidad_id')->nulleable()->unsigned();
should be
nullable
$table->integer('entidad_id')->nullable()->unsigned();
Don't know why Laravel is not yelling on typos :(

#1215 - Cannot add foreign key constraint

I'm working a project with Yiiframwork and I have this table in my data base project
CREATE TABLE IF NOT EXISTS `tbl_annonce` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`idEntreprise` tinyint(3) unsigned NOT NULL
COMMENT 'CONSTRAINT FOREIGN KEY (idEntreprise) REFERENCES tbl_entreprise(id)',
`titre` varchar(100) NOT NULL,
`detailleDiscription` varchar(5000) NOT NULL,
`categorie` varchar(100) DEFAULT NULL,
`typePoste` varchar(100) NOT NULL,
`salaireMin` varchar(80) NOT NULL,
`salaireMax` varchar(80) NOT NULL,
`niveauEtude` varchar(80) NOT NULL,
`niveauExperience` varchar(80) NOT NULL,
`langue` varchar(50) DEFAULT NULL,
`poste` varchar(50) NOT NULL,
`pays` varchar(50) NOT NULL,
`ville` varchar(50) NOT NULL,
`adresse` varchar(80) NOT NULL,
`datePublication` timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`etat` varchar(50) NOT NULL,
`photo` varchar(255) NULL,
`nometr` text NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_idEntrepriseAnn`
FOREIGN KEY (idEntreprise)
REFERENCES tbl_entreprise(id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
I only use 'comment' because I found it in a tutorial for a yii framework project.
I have got the attribute 'idEntreprise' as FK in my DB but when I entred other 'idEntreprise' that does note exist in my table entreprise it was not problem, instead it must be a problem.
So I added 'identreprise' as Fk and that have this problem now.
Hope you understand my problem :/
can any one helep me plz !!
To define a foreign key on a column, it must fulfil some conditions.
Child and parent column must have same column definition by type,
signed.
Parent column must have an index defined on it.
Make sure that
The column tbl_entreprise.id is indexed.
The column definition idEntreprise tinyint(3) unsigned in tbl_annonce matches
with that of tbl_entreprise.id
Refer to: MySQL: Foregin Key Constaints

trying to make a foreign key but MySql won't allow me, is there any way?

i have these 3 tables
CREATE TABLE IF NOT EXISTS `enrollment` (
`STUDENT_NUM` varchar(10) NOT NULL,
`SUBJECT_NUM` varchar(10) NOT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`ROOM_ID` int(11) DEFAULT NULL,
`PRELIM` float(10,2) DEFAULT NULL,
`MIDTERM` float(10,2) DEFAULT NULL,
`FINALS` float(10,2) DEFAULT NULL,
`FINAL_GRADE` float(10,2) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`STUDENT_NUM`,`SUBJECT_NUM`),
KEY `SUBJECT_NUM` (`SUBJECT_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `subjects` (
`SUBJECT_NUM` varchar(10) NOT NULL,
`EMPLOYEE_NUM` varchar(10) NOT NULL,
`SUBJECT_TITLE` varchar(100) DEFAULT NULL,
`DEPARTMENT` varchar(100) DEFAULT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`room_id` int(11) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`COUNT` int(10) DEFAULT NULL,
`STATUS` varchar(50) DEFAULT NULL,
`FLAG` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`SUBJECT_NUM`),
UNIQUE KEY `SUBJECT_NUM` (`SUBJECT_NUM`),
KEY `EMPLOYEE_NUM` (`EMPLOYEE_NUM`),
KEY `EMPLOYEE_NUM_2` (`EMPLOYEE_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `room` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`room` varchar(255) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `room` (`room`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
what i am trying to do is make the field ROOM_ID from enrollment and subjects foreign key and the reference be the ID from room.. the ROOM_ID must not be unique...
i am getting this error
#1452 - Cannot add or update a child row: a foreign key constraint fails (`enrollmentdb`.`#sql-277_164`, CONSTRAINT `#sql-277_164_ibfk_3` FOREIGN KEY (`ROOM_ID`) REFERENCES `room` (`ID`))
when i am using this SQL command:
ALTER TABLE enrollment
ADD FOREIGN KEY (room_id)
REFERENCES room(ID)
Try below code if its working :
ALTER TABLE enrollment
ADD CONSTRAINT ROOM_ID_fk
FOREIGN KEY(ROOM_ID)
REFERENCES room(ID);
Thanks!
room_id is capitalised in your table. Try:
ALTER TABLE enrollment
ADD FOREIGN KEY (ROOM_ID)
REFERENCES room(ID)
replace 'room_id' with 'ROOM_ID'

Categories