Symfony2 프로젝트의 경우 블로그 게시와 소위 플랫폼 간의 관계를 만들어야했습니다. 플랫폼은 사이트를 보는 데 사용하는 도메인을 기반으로 특정 필터를 정의합니다. 예 : url first-example.com으로 사이트에 가입하면이 사이트는이 특정 플랫폼에 연결된 블로그 게시물 만 제공합니다.다 대 다 관계로 EntityRepository :: findBy()를 사용하면 Doctrine에서 E_NOTICE가됩니다.
이렇게하려면 게시 및 플랫폼이라는 두 개의 엔티티를 만들었습니다. 이후에 다 대 다 (Many-To-Many) 관계로 매핑했습니다. Doctrines의 EntityRepository
에있는 내장 함수 findBy()
에서이 다 대 다 관계를 통해 데이터를 검색하려고합니다. $postRepo
가 Post
개체와 $platform
기존 Platform
개체에 대한 올바른 저장소입니다
// every one of these methods will throw the same error
$posts = $postRepo->findBy(array('platforms' => array($platform)));
$posts = $postRepo->findByPlatforms($platform);
$posts = $postRepo->findByPlatforms(array($platform));
. 나는 다음과 같은 오류 치울 :
ErrorException: Notice: Undefined index: joinColumns in [...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1495
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1495
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1452
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1525
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1018
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:842
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:157
[...]/src/Foobar/BlogBundle/Tests/ORM/PostTest.php:102
은 다 대다 관계를이 방법으로 관련 entites를 검색 할 경우에도 가능을, 또는 나 자신에 의해이 함수를 작성하도록 강요 오전
어느 쪽이든? 이상한 점은 Doctrine은 "불가능합니다."와 같은 오류는 발생하지 않지만 내부는 E_NOTICE
입니다. 그게 가능해야한다고 생각하는 텐트인데, 여기에 몇 가지 요점이 빠져 있습니다.
흥미로운 부분으로 분리 된 두 엔티티는 다음과 같습니다.
<?php
namespace Foobar\CommunityBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
// [...] other namespace stuff
/**
* @ORM\Entity(repositoryClass="Foobar\CommunityBundle\Entity\Repository\PlatformRepository")
* @ORM\Table(name="platforms")
*/
class Platform
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// [...] other field stuff
}
<?php
namespace Foobar\BlogBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
// [...] other namespace stuff
/**
* @ORM\Entity(repositoryClass="Foobar\BlogBundle\Entity\Repository\PostRepository")
* @ORM\Table(name="posts")
*/
class Post implements Likeable, Commentable, Taggable, PlatformAware
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Foobar\CommunityBundle\Entity\Platform", cascade={"persist"})
* @ORM\JoinTable(name="map_post_platform",
* joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="platform_id", referencedColumnName="id")}
* )
*/
protected $platforms;
// [...] other fields
/**
* Constructor
*/
public function __construct()
{
// [...]
$this->platforms = new ArrayCollection();
}
}
물론 (뿐만 아니라 관련 라인까지 제거)을 composer.json 파일의
{
[...]
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.1.*",
"doctrine/orm": ">=2.2.3,<2.4-dev",
"doctrine/doctrine-bundle": "1.0.*",
"doctrine/doctrine-fixtures-bundle": "dev-master",
[...]
},
[...]
}
findPostsByPlatform
이 답변 주셔서 감사합니다 것입니다. 조금만 더. 첫 번째 방법은 사용자 지정 저장소에 자체 함수를 작성하는 것입니다. 나 자신을 불분명하게 표현했을지 모르지만 Doctrine에 내장 된'find *()'함수를 통해 관련 엔티티를 가져 오려고했습니다. 단방향 연결이 있기 때문에 두 번째 방법은 작동하지 않습니다. 그러므로'Platform'에'$ posts' 속성이 없기 때문에 getter와 setter가 없습니다. 그럼에도 불구하고 여러분의 대답은 많은 도움이됩니다. 이제는 필터링 된 many-to-many 연관을 얻기 위해 내장 된 방법을 사용할 수 없다는 것을 확신합니다. – devsheeep