Viewed   108 times

How do I set a default value in Doctrine 2?

 Answers

3

Database default values are not "portably" supported. The only way to use database default values is through the columnDefinition mapping attribute where you specify the SQL snippet (DEFAULT cause inclusive) for the column the field is mapped to.

You can use:

<?php
/**
 * @Entity
 */
class myEntity {
    /**
     * @var string
     *
     * @Column(name="myColumn", type="string", length="50")
     */
    private $myColumn = 'myDefaultValue';
    ...
}

PHP-level default values are preferred as these are also properly available on newly created and persisted objects (Doctrine will not go back to the database after persisting a new object to get the default values).

Friday, December 2, 2022
2

As far as I know, there is still no way to do that in DQL, and the reason might be that it does not make much sense from an ORM perspective, according to one of the lead developers.

Nevertheless, the feature request, although very old, is still open; so it might be implemented in the future.

Tuesday, November 1, 2022
 
4

Try to:

  1. Clear any cache that may contain the config or PHP code.
  2. Rename a field in case the first solution didn't work.
Thursday, November 3, 2022
3

Using the syntax that you used,

int array[100] = {-1};

says "set the first element to -1 and the rest to 0" since all omitted elements are set to 0.

In C++, to set them all to -1, you can use something like std::fill_n (from <algorithm>):

std::fill_n(array, 100, -1);

In portable C, you have to roll your own loop. There are compiler-extensions or you can depend on implementation-defined behavior as a shortcut if that's acceptable.

Thursday, October 20, 2022
 
4

You should add schema name for that function:

CREATE TABLE [dbo].[Clients](
   [ModifyingUser] [nvarchar](128) NOT NULL DEFAULT dbo.sCurrentAppUser (),
   [Modification] [char](1) NULL DEFAULT 'A',
   [ModifyingHost] [nvarchar](128) NOT NULL DEFAULT host_name (),
   [ClientID] [uniqueidentifier] NOT NULL,
   [Label] [nvarchar](1024) NULL,
   CONSTRAINT [PK_Clients] PRIMARY KEY (
      [ClientID] ASC
   )
)
Wednesday, November 30, 2022
 
clem_g.
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :