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

  • SEARCH
  • Home
  • 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 8324543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:06:39+00:00 2026-06-09T00:06:39+00:00

I do not understad why with some Entity objects I can set the Id

  • 0

I do not understad why with some Entity objects I can set the Id and for others objects I get an error and says me that the Id can’t be null and I have to pass an object instead.

e.g.:

$log = new Log();
$log->setTypeId(1);
$log->setUserId(1);
$entityManager->persist($log);
$entityManager->flush();

If I try the code above I get error that says: Integrity constraint violation: 1048 Column ‘user_id’ cannot be null. And I have to first create the Type Object and de User object and the pass them:

$log->setType($TypeObject)
$log->setUser($UserObject)

But for other entity objects I have no problem assigning the value directly, why is that?

This is my Entity Log:

<?php
/**
 * @Entity
 * @Table(name="log")
 * @HasLifecycleCallbacks
 */
class Log
{
    /**
     * @var type 
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

     /**
     *
     * @var type 
     * @Column(type="integer")
     */
    protected $user_id;

     /**
     *
     * @var type 
     * @Column(type="integer")
     */
    protected $type_id;

     /**
     *
     * @var type 
     * @Column(type="datetime")
     */
    protected $created;

    /**
     *
     * @var type 
     * @ManyToOne(targetEntity="User", inversedBy="logs")
     */
    protected $user;

    /**
     *
     * @ManyToOne(targetEntity="Type", inversedBy="logs")
     */
    protected $type;

    public function getId()
    {
        return $this->id;
    }

    public function getUserId()
    {
        return $this->user_id;
    }

    public function getTypeId()
    {
        return $this->type_id;
    }

    public function getCreated()
    {
        return $this->created;
    }

    public function setUserId($userId)
    {
        $this->user_id = $userId;
    }

    public function setTypeId($typeId)
    {
        $this->type_id = $typeId;
    }

    public function setCreated($created)
    {
        $this->created = $created;
    }

    public function setUser($user)
    {
        $this->user = $user;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    /**
     * @PrePersist
     */
    public function prePersist()
    {
        $this->setCreated(new DateTime());
    }

}
?>
  • 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-06-09T00:06:42+00:00Added an answer on June 9, 2026 at 12:06 am

    EDIT

    I found this statement on the website of Doctrine2. It’s a best practice that you might want to follow when coding your models.

    Doctrine2 Best Practices

    25.9. Don’t map foreign keys to fields in an entity

    Foreign keys have no meaning whatsoever in an object model. Foreign keys are how a relational database establishes relationships. Your object model establishes relationships through object references. Thus mapping foreign keys to object fields heavily leaks details of the relational model into the object model, something you really should not do

    EDIT

    Doctrine does the mapping from your objects to their respective Ids.

    What you’ve done here is a bit redundant.

    You’ve essentially told doctrine the same thing twice.

    You’ve told it that it has a ‘user_id’ column AND that it also has a User object, which are the same thing. But doctrine can already guess that this relationship will have a user_id column based on the fact that the log class has a user object inside.

    You should simply do the following instead

    <?php
    /**
     * @Entity
     * @Table(name="log")
     * @HasLifecycleCallbacks
     */
    class Log
    {
        /**
         * @var type 
         * @Id
         * @Column(type="integer")
         * @GeneratedValue
         */
        protected $id;
    
         /**
         *
         * @var type 
         * @Column(type="datetime")
         */
        protected $created;
    
        /**
         *
         * @var type 
         * @ManyToOne(targetEntity="User", inversedBy="logs")
         */
        protected $user;
    
        /**
         *
         * @ManyToOne(targetEntity="Type", inversedBy="logs")
         */
        protected $type;
    
        public function getId()
        {
            return $this->id;
        }
    
        public function getCreated()
        {
            return $this->created;
        }
    
        public function setCreated($created)
        {
            $this->created = $created;
        }
    
        public function setUser($user)
        {
            $this->user = $user;
        }
    
        public function setType($type)
        {
            $this->type = $type;
        }
    
        /**
         * @PrePersist
         */
        public function prePersist()
        {
            $this->setCreated(new DateTime());
        }
    
    }
    

    Doctrine will worry about the user_id and type_id on it’s own. You don’t have to worry about it. This way you get to work with full fledged objects, making it easier to program, instead of having to worry about id’s. Doctrine will handle that.

    If ALL you have is an id, because that’s what you’re using on the front end, then just fetch the object associated with that id using the Entitymanager.

    $user = $em->getEntity( 'User', $idFromWeb );
    $log = new Log();
    $log->setUser( $user );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason that I don't understand, video websites do not play when using
I have an app that basically can be used to download, upload, and manage
I found some old code which I'm not sure I understand completely. The folowing
I do not understand why this error occurs...Is this a bug in XE, or
I do not understand pointers. Where can I learn more about them?
I do not understand this error, do not generate error in JsonResult Test (),
I have a problem that I haven't been able to find a solution to,
I have 2 entities, A and B that have a many to many relationship.
I have an entity class Document and another one called Space. The relation: @ManyToOne(fetch
I have some problem while trying to use the schemagen tool from JAXB library

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.