Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7594045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:18:58+00:00 2026-05-30T21:18:58+00:00

Using annotations is quite simple to set a default value for a given column

  • 0

Using annotations is quite simple to set a default value for a given column and initialize collections for entity relations:

use Doctrine\Common\Collections\ArrayCollection;

class Category
{   
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    /**
     * @ORM\Column(type="bool")
     */
    protected $is_visible;

    public function __construct()
    {
        $this->products   = new ArrayCollection();
        $this->is_visible = true; // Default value for column is_visible
    }
}

How the same can be achieved using YAML definition instead, without manually write Category.php? Is __construct() the only method for doing this?

Acme\StoreBundle\Entity\Category:
    type: entity
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        is_visible:
            type: bool
    oneToMany:
        products:
            targetEntity: Product
            mappedBy: category
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:18:59+00:00Added an answer on May 30, 2026 at 9:18 pm

    I think you misunderstood annotations somehow because the default value is set via plain php.

    /**
     * @ORM\Column(type="bool") <- This is an annotation
     */
    protected $is_visible;
    
    public function __construct()
    {
        $this->products   = new ArrayCollection(); // <- This is not an annotation
        $this->is_visible = true; // <- This is not an annotation
    }
    

    There is no difference using the YAML mapping for the default value. The reason is simple, here how you class is looking with annotations:

    use Doctrine\Common\Collections\ArrayCollection;
    
    class Category
    {   
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
         */
        protected $products;
    
        /**
         * @ORM\Column(type="bool")
         */
        protected $is_visible;
    
        public function __construct()
        {
            $this->products   = new ArrayCollection();
            $this->is_visible = true; // Default value for column is_visible
        }
    }
    

    And this is how it looks with YAML mapping:

        use Doctrine\Common\Collections\ArrayCollection;
    
    class Category
    {   
        protected $id;
        protected $products;
        protected $is_visible;
    
        public function __construct()
        {
            $this->products   = new ArrayCollection();
            $this->is_visible = true; // Default value for column is_visible
        }
    }
    

    The difference in the second example is there is no more annotations, since the mapping is done via YAML. The construction of the class is done exactly the same. Thus, default values are set at construction time which is done in plain PHP.

    There is no difference between annotations and YAML mapping for this task. So, bottom line, you need to edit the generated PHP class to put your default values. There is no way you can set it in YAML and let doctrine put this code for you, at least, at the time we speak.

    Maybe I misunderstood your question :), if it is the case, don’t hesitate to correct me.

    Hope it helps.

    Regards,
    Matt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring 3, and trying to set up a simple web-app using annotations
Using annotations how do you map a field in an entity which is a
I tried Hibernate Mapping Many-to-Many using Annotations with the example given in vaannila. http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html
I want to validate a string against a set of values using annotations. What
Got a simple form, and I am using the data annotations and model to
I'm using the MOXy JAXB implementation and make quite extensive use of the @XmlInverseReference
I'm considering using Annotations to define my Hibernate mappings but have run into a
I am using annotations to map a basic collection of Strings to an existing
Can you override method in java without using annotations? Because eclipse doesn't support it
I'm trying to combine Spring with Hibernate using Annotations and I'm getting the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.