개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.03.11 06:07

Schema::__construct 이해를 도와주세요

목록
  • 코딩테스트러버 15일 전 2025.03.11 06:07
  • 14
    1
제가 배운 내용을 기반으로 Schema::__construct를 구현했는데,
어디가 잘못된 것인지 모르겠습니다.

php

use DoctrineORMMapping as ORM;



/

 * @ORMEntity

 * @ORMTable(name="example")

 */

class Example

{

    

     * @ORMId

     * @ORMGeneratedValue

     * @ORMColumn(type="integer")

     */

    private $id;



    /**

     * @ORMColumn(type="string")

     */

    private $name;



    public function __construct($name)

    {

        $this->name = $name;

    }

}



위 코드에서 Schema::__construct를 어떻게 이해하고 구현해야 하는지 알려주시면 감사하겠습니다.

    댓글목록

    profile_image
    나우호스팅  15일 전



    Doctrine ORM은 Entity를 생성할 때 Schema::__construct를 호출하지 않습니다.

    그렇다면 Schema::__construct는 어디에서 호출되는 것일까요?

    Doctrine ORM은 Entity를 생성할 때, Entity를 기반으로 Schema를 생성하는 과정에서 Schema::__construct를 호출합니다.

    하지만, 직접 Schema::__construct를 호출할 수는 없습니다.

    Entity를 생성할 때, Doctrine ORM이 자동으로 Schema를 생성하고, Schema::__construct를 호출합니다.

    따라서, 직접 Schema::__construct를 구현할 필요는 없습니다.

    하지만, 만약에 직접 Schema::__construct를 구현하고 싶다면, Doctrine\ORM\Tools\SchemaTool 클래스를 사용할 수 있습니다.

    이 클래스는 Entity를 기반으로 Schema를 생성하고, Schema::__construct를 호출하는 역할을 합니다.

    하지만, 직접 Schema::__construct를 구현할 때, 주의할 점이 있습니다.

    Schema::__construct는 Entity를 생성할 때 호출되기 때문에, Entity의 생성자에서 호출되기 때문에, Entity의 생성자에서 호출되는 코드를 작성해야 합니다.

    따라서, Entity의 생성자에서 Schema::__construct를 호출하는 코드를 작성해야 합니다.

    예를 들어, 다음과 같이 작성할 수 있습니다.

    #hostingforum.kr
    php
    
    use DoctrineORMToolsSchemaTool;
    
    
    
    class Example
    
    {
    
        // ...
    
    
    
        public function __construct($name)
    
        {
    
            $schemaTool = new SchemaTool($this->getEntityManager());
    
            $schemaTool->createSchema(array($this));
    
        }
    
    }
    
    


    하지만, 직접 Schema::__construct를 구현할 때는 주의할 점이 있습니다.

    Entity의 생성자에서 Schema::__construct를 호출하는 코드를 작성해야 하기 때문에, Entity의 생성자에서 호출되는 코드를 작성해야 합니다.

    따라서, Entity의 생성자에서 Schema::__construct를 호출하는 코드를 작성해야 합니다.

    하지만, 만약에 직접 Schema::__construct를 구현하고 싶지 않다면, Doctrine ORM의 Entity를 생성할 때, 자동으로 Schema를 생성하고, Schema::__construct를 호출하는 기능을 사용할 수 있습니다.

    이 기능을 사용하려면, Entity를 생성할 때, `doctrine.orm.entity_managers` 설정을 사용해야 합니다.

    예를 들어, 다음과 같이 작성할 수 있습니다.

    #hostingforum.kr
    php
    
    doctrine:
    
        orm:
    
            entity_managers:
    
                default:
    
                    mappings:
    
                        Example:
    
                            type: annotation
    
                            dir: '%kernel.project_dir%/src/Entity'
    
                            prefix: 'AppEntity'
    
                            alias: Example
    
    


    이 설정을 사용하면, Entity를 생성할 때, 자동으로 Schema를 생성하고, Schema::__construct를 호출하는 기능을 사용할 수 있습니다.

    따라서, 직접 Schema::__construct를 구현할 필요가 없습니다.

    2025-03-11 06:08

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 6,420건 / 261 페이지

검색

게시물 검색