PHP ORM如何在构造函数中设置id

PHP ORM如何在构造函数中设置id,php,object,orm,model,constructor,Php,Object,Orm,Model,Constructor,我对以下代码有一个基本问题: <?php interface UserInterface { public function getId(); public function getName(); } class User implements UserInterface { private $_id; private $_name; public function __construct($id, $name) { $

我对以下代码有一个基本问题:

<?php

interface UserInterface
{
    public function getId();
    public function getName();
}

class User implements UserInterface
{
    private $_id;
    private $_name;

    public function __construct($id, $name)
    {
        $this->_id = $id;
        $this->_name = $name;
    }

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

    public function getName()
    {
        return $this->_name;
    }
}

class UserMapper
{
    public function insert(UserInterface $user)
    {
        ... insertion code
    }
}

?>

UserMapper的insert方法需要一个UserInterface对象。因此,我创建了一个:

<?php

$user = new User(1, "Chris");
$userMapper = new UserMapper();
$userMapper->insert($user);

?>

我的问题是,用户id是插入对象后从数据库中自动增加的值。但是对象的构造函数迫使我定义一个id,因为没有id对象是不完整的。如何解决这个一般问题

将id作为第二个参数传递给具有默认值的构造函数wo不是一个选项,因为据我所知,如果没有id,对象将是不完整的


谢谢您的帮助。

您可以传递
null

$user = new User(null, "Chris");

$\未检查id是否为有效整数,如果使用
null
,您就知道此模型还没有有效id

您可以传递
null

$user = new User(null, "Chris");
$\未检查id是否为有效整数,如果使用
null
,您就知道此模型还没有有效id