A
A
Antiless2016-10-15 03:14:26
symfony
Antiless, 2016-10-15 03:14:26

Some questions about DoctrineORM and Symfony?

During the study of the symphony, several questions arose that I could not figure out. I want to get help from hardened colleagues.
1) In the documentation, it is advised to prescribe getters / seters for all entity fields. I don't understand why, if php has __call() . The situation when you need to do individual processing of the field is clear, but why in most cases clog the class with the same type of methods - I don’t understand. What's the point?
2) I made a foreign key from the address to the user, indicated the actions in a cascade, the connection is displayed in the PMA, but the parent element does not want to be deleted. there are children. What is the problem?


/**
* @ORM\ManyToOne(targetEntity="Customer", cascade={"remove"}, fetch="EAGER")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="customer_id")
*/
protected $customerId;

3) When saving the address, the framework requires to pass an object of type Customer in the $customerId field. I understand that it will most likely ask for an id from the object, but why load the whole object into memory if, for example, I know its id in advance? And if I want to process a lot of addresses, then for each one to load its user - it's strange. Is there a way to fix it to require a scalar value without removing the relationship between the entities?
4) How to make an ORM annotation so that a field of type "datetime" is filled with the current date-time? I wrote like this:

/**
* @ORM\Column(type="datetime", name="inserted_at", options={"default" : "CURRENT_TIMESTAMP"})
*/

, but the database is not saved. field cannot be NULL. Google suggested that you fill in the options in the constructor or setter yourself, but it’s strange to somehow do what the database itself can do.
Thanks to all the indifferent))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yuri, 2016-10-15
@Antiless

1) the doctrine creates proxy objects for all your entities, and in the future you will receive them from the database. in proxy objects for getters and setters, it makes wrapper methods.
+ It's very inconvenient to work with call, because there is no auto-completion in the development environment, only because of this I would not use the call.
2) perhaps the cache was not reset, the doctrine sometimes caches annotations and does not apply them immediately with a small change.
3) humble yourself. there is an overhead, of course, he was indignant at the beginning, but the OOP requires sacrifice.
4) yes, just add it to the constructor, there are no problems, the advantage is that this field is immediately available, as well as a mega bonus for writing tests when you need to create objects with the specified dates.

I
index0h, 2016-10-15
@index0h

I don't understand why, if php has __call().

Magic is hard to debug, test, and maintain. It may be required, but in very, very narrow cases, when there is no other way. Getters with setters are far from the case, given that the PhpStorm IDE type generates them perfectly.
In order not to litter it with useless magic.
When fetching, the doctrine generates proxy classes that reload and hydrate child objects if necessary. If you want a pure id, don't declare a link))
Why? If you want to work with addresses - work with addresses, what do users have to do with it?
Add a default value to the constructor.

A
Andrey Shakhtarin, 2016-10-15
@AndreyShakhtarin

How to make an ORM annotation so that a field of type "datetime" is filled with the current date-time?

class Имя_Класса
{

     $поле_времяни;
     /**
     * Set поле_времяни
     *
     * @param \DateTime
     *
     * @return Имя_Класса
     */
    public function поле_времяни()
    {
        $this->поле_времяни = new \DateTime('today');
    }
}

I usually do this...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question