Why can't I use public properties in Doctrine objects?

Say I have a very simple CRUD system in PHP for managing a database. Say it contains a list of products. Using Doctrine ORM, I would like to query the database and view / edit / add records. According to the Getting Started Guide ,

When creating entity classes, all fields must be protected or private (non-public) , with getter and setter methods for each of them (except $ id). Using mutators allows Doctrine to connect to calls that manipulate entities in ways you wouldn’t be able to if you simply set values ​​directly using the # field = foo object;

That's an example:

// src/Product.php
class Product
{
    /**
     * @var int
     */
    protected $id;
    /**
     * @var string
     */
    protected $name;

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

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

    public function setName($name)
    {
        $this->name = $name;
    }
}

// Recording a new title
$product->setName("My new name");
$db->persist($product);
$db->flush();

// Echoing the title
echo $product->getName();

. , , . :

// src/Product.php
class Product
{
    /**
     * @var int
     */
    public $id;
    /**
     * @var string
     */
    public $name;
}

:

$product = $db->getRepository('Product')->find(1);

// Recording a new name
$product->name = "My new title";
$db->persist($product);
$db->flush();

// Echoing the title
echo $product->name;

:

  • .

? ?

+4
2

, , .

, Doctrine. Doctrine ORM .

, , PHP, , Doctrine. , PHP- getters/seters , (, Kotlin, #).

? . extend .

:

// src/Product.php
class Product
{
    /**
     * @var int
     */
    protected $id;
    /**
     * @var string
     */
    protected $name;

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

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

    public function setName($name)
    {
        $this->name = $name;
    }
}

:

class Product extends \YourNamespace\Product implements \Doctrine\ORM\Proxy\Proxy
{

    // ... Lots of generated methods ...

    public function getId(): int
    {
        if ($this->__isInitialized__ === false) {
            return (int)  parent::getId();
        }


        $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', []);

        return parent::getId();
    }

    public function getName(): string
    {

        $this->__initializer__ && $this->__initializer__->__invoke($this, 'getName', []);

        return parent::getName();
    }

    // ... More generated methods ...
}

(, , , , Doctrine ORM)

, Proxy - Doctrine. - . . . , Doctrine.

+3

Doctrine?

:

/?

.

, ?

, . , , .

, id. , ? - ? ?

! id.

?

: , -, "IX", , invoice_id ?

, invoice_id "XX12345"?

, (, ), , .

,

private $invoiceId;

public function setInvoiceId($invoiceId) 
{
    if (is_null($invoiceId) || preg_match("#^IX(.*)$#i", $invoiceId) === 0)
        return false;

    $this->invoiceId = $invoiceId;

    return $this;
}

, .

+2

Source: https://habr.com/ru/post/1684976/


All Articles