2017-11-24 5 views
1

문제점 : 내 쿼리에 사용자 ID를 제공하여 모든 회사와 관련된 사용자를 얻으려고합니다.Symfony 2.7 - 열을 찾을 수 없음 - 다 대 다 관계

오류 내가 갖는 :

CRITICAL - Uncaught PHP Exception Doctrine\DBAL\Exception 
\InvalidFieldNameException: "An exception occurred while executing 'SELECT 
t0.id AS id_1, t0.name AS name_2, t0.phone AS phone_3, t0.fax AS fax_4, 
t0.country AS country_5, t0.address AS address_6, t0.zipcode AS zipcode_7, 
t0.state AS state_8, t0.city AS city_9, t0.notes AS notes_10 FROM company t0 
WHERE company_representatives.user_id = ?' with params [177]: 
SQLSTATE[42S22]: Column not found: 1054 Unknown column 
'company_representatives.user_id' in 'where clause'" at C:\wamp64 
\www\Portail\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver 
\AbstractMySQLDriver.php line 71 

내가

/** 
* @var ArrayCollection 
* 
* @ORM\ManyToMany(targetEntity="Dbm\UserBundle\Entity\User") 
* @ORM\JoinTable(name="company_representatives") 
*/ 
private $representatives; 

을 Company.php

내 실체 "사용자"

와 ManyToMany 관계가있는 기업 "회사"가 User.php는 그의 엔티티 클래스에서 Company와 아무런 관계가 없습니다. 때문에 내 ManyToMany 관계의

, 내가하고 "USER_ID" "을 COMPANY_ID"가 포함 된 company_representatives 테이블이 할

아래 코드는 오류 발생 "을 (를) 찾을 수 없습니다 열"을 일으키는 것입니다.

$companies = $em->getRepository('DbmPortalBundle:Company')->findBy(array('representatives' => $user->getId())); 

-Cache는

를 삭제되었습니다 - 교리를 사용하는 경우 [매핑] 및 [데이터베이스] = OK : 스키마 - 난 내 생성자에있는 ArrayCollection로 대표 인스턴스를

을 확인

수정

지금 당장 트릭을 수행 할 수있는 해결 방법을 사용했습니다. 나는 나중에 이것을 고쳐야 할 것이 분명하다. 내 해결 방법은 내 "user_id"에 연결된 모든 회사의 ID를 찾기 위해 "company_representatives"테이블에서 원시 SQL을 직접 사용하는 것입니다. 그런 다음 회사 회사의 ID로 "FindBy"를 사용하여 모든 회사의 오브젝트를 얻을 수 있습니다.

답변

0

ArrayCollection을 Company.php의 _construct() 메소드에서 초기화 하시겠습니까?

// Entity/Company.php 

public function __construct() { 
    $this->representatives = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

... 

교리 문서 here을 참조하십시오. 스키마 :

+0

작동하지 않았습니다.또한 ManyToMany Bidirectional로 엔티티 설정을 시도했지만 캐시 지우기 후에도 동일한 오류가 발생합니다. – Phil

0

PHP 응용 프로그램/콘솔 교리하려고 당신은 findBy을 사용할 수 없습니다

0

--force 업데이 트를 (또는 어떤 방법 찾기) 많은 관계로 많은과를 (어쩌면이 지원됩니다 미래). 조건이 올바르게 작성되었습니다. "WHERE company_representatives.user_id =?" parct [177] "with doctrine하지만 company_representatives와의 조인은 추가되지 않습니다.

/** 
* @var ArrayCollection 
* 
* @ORM\ManyToMany(targetEntity="Company", inversedBy="representatives") 
* @ORM\JoinTable(name="company_representatives") 
*/ 
private $companies; 

public function __construct() 
{ 
    $this->companies = new ArrayCollection(); 
} 

public function getCompanies() 
{ 
    return $this->companies; 
} 

마지막으로 당신이 사용자 개체에서 직접 회사를 얻을 수 있습니다 : 당신이 원하는 무엇

내가 또한 사용자 엔티티 관계를 추가하는 제안 한 사용자의 모든 기업을 얻을 것입니다

$user->getCompanies();