0

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
5
  • 1
    This is totally clear. The field is not nullable text so null translates to empty string. Commented Apr 26, 2015 at 18:56
  • What's the default value? Can you post the show create table t output? Commented Apr 26, 2015 at 18:57
  • hmm, is that the usual behaviour ? should I not receive an exception ? or how could I have it throw an exception ? Commented Apr 26, 2015 at 19:02
  • You can use Doctrine Event hooks and Lifecycle callbacks for such error checking. Read more here doctrine-orm.readthedocs.org/en/latest/reference/events.html Commented Apr 26, 2015 at 19:20
  • Or rather a Validator if it's a form field. Commented Apr 26, 2015 at 19:21

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

well, sure I can check for that and throw an exception, that's not a problem. i'm more trying to understand if it's the default behaviour (I thought it was note) and if it is the default, how can I have it throw an exception automatically instead of converting autmatically to empty string
by the way, with the same field for another entity (as I am using a trait), I have an exception when null.
I see. What does the trait look like? And are both objects that use the trait part of the same lineage?
Basically I'm thinking there has to be a difference between the save methods on the two objects. We need to find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.