N
N
Nicholas Lindemann2015-02-27 19:31:42
symfony
Nicholas Lindemann, 2015-02-27 19:31:42

How to reuse an entity?

Hello, I'm obsessed with such a task: in general, there is a User entity, in which there are firstName, lastName, username, email and password properties (their number will increase over time); when implementing the registration form, you need to save only two properties - email and password; but when I try to do so, an exception is thrown, where it is written that Doctrina passes absolutely all the parameters that have it, and sets the missing parameters to null. Why does it happen this way? Maybe you need to create your own entity for each implementation of the functionality, or maybe you need a completely different approach? Who knows, or has already encountered this issue, please share, you save me a lot of time.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Pavlov, 2015-02-27
@lexxpavlov

You have two options:
1) either allow fields to be nullable
2) or set a default value for the field.

namespace YourBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $firstName;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $lastName;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $username = "";

    /**
     * @ORM\Column(type="string", length=100, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $password;

    // getters & setters
}

See, the firstName and lastName fields are marked nullable=true, and the username field has a default value (an empty string).
PS don't forget to add the salt field in addition to the password.
PPS or better, take FOSUserBundle, and don't worry, there is already an excellent set of fields for the user, plus the ability to add your own.

A
Alexander Obiedkov, 2015-02-27
Obiedkov @aobiedkov

Doctrina passes absolutely all the parameters that have it, and sets the missing ones to null. Why does it happen this way?

And how did you want? Doctrine was created to work with the database, as you imagine inserting into the database table (5 columns) only 2 records, of course, the entire record is inserted and the parameters that you did not manually set or did not initialize in the constructor are assigned null.
function __construct() {
$this->firstName = "unknown";
$this->lastName= "unknown";
// и тд
}

Y
yuklia_1, 2015-03-02
@yuklia_1

alternatively you can use hooks

/**
 * @ORM\Entity @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="users")
 **/
class User
{
  /**
     * @ORM\PrePersist
     */
    public function prePersist()
    {
        $this->created = new \DateTime('now');
            $this->status = self::STATUS_PENDING;
    }

    /** @ORM\PreUpdate */
    public function preUpdate()
    {
        $this->updated = new \DateTime('now');
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question