도와주세요.symfony에서 join 컬럼에서 제기되는 문제
조회수 5710 답변수 1 반응수 3 등록일 2016.07.26 18:10:10

안녕하십니까? 심포니에서 한 테블의 컬럼을 다른 테블에서 참조(Join Column)하려는데 잘 되지 않아요.도와주세요.

코드를 알려드립니다.Entity파일입니다. 

- InputStatus.php(Entity파일)

  1. <?php
  2.  
  3. namespace AppBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7.  
  8. /**
  9.  * InputStatus
  10.  *
  11.  * @ORM\Table(name="input_status")
  12.  * @ORM\Entity(repositoryClass="AppBundle\Repository\InputStatusRepository")
  13.  */
  14. class InputStatus
  15. {
  16.  
  17. //const ESTADO_INICIAL = 1;
  18. //const ESTADO_ALARMAS_INICIAL = 1;
  19.  
  20.  /**
  21.  * @var int
  22.  *
  23.  * @ORM\Column(name="id", type="integer")
  24.  * @ORM\Id
  25.  * @ORM\GeneratedValue(strategy="AUTO")
  26.  */
  27.  private $id;
  28.  
  29. /**
  30.  * @ORM\ManyToOne(targetEntity="Device")
  31.  * @ORM\JoinColumn(name="device_id", referencedColumnName="device_id")
  32.  */
  33.  private $deviceId;
  34.  
  35. /**
  36.  * @ORM\ManyToOne(targetEntity="InputWarningSetting")
  37.  * @ORM\JoinColumn(name="input", referencedColumnName="input")
  38.  */
  39.  private $input;
  40.  
  41.  /**
  42.  * @var \DateTime 
  43.  *
  44.  * @ORM\Column(name="start_time", type="datetime")
  45.  */
  46.  private $startDate;
  47.  
  48.  /**
  49.  * @var \DateTime 
  50.  *
  51.  * @ORM\Column(name="finish_time", type="datetime")
  52.  */
  53.  private $finishDate;
  54.  
  55.  /**
  56.  * Constructor
  57.  */
  58.  public function __construct()
  59.  {
  60.  }
  61.  
  62. public function __toString(){
  63. //return $this->deviceId;
  64. }
  65.  
  66. /**
  67.  * Get id
  68.  *
  69.  * @return integer 
  70.  */
  71.  public function getId()
  72.  {
  73.  return $this->id;
  74.  }
  75.  /**
  76.  * Set StartDate
  77.  *
  78.  * @param \DateTime $startTime
  79.  * @return InputStatus
  80.  */
  81.  public function setStartDate($startDate)
  82.  {
  83.  $this->startDate = $startDate;
  84.  
  85.  return $this;
  86.  }
  87.  
  88.  /**
  89.  * Get StartDate
  90.  *
  91.  * @return \DateTime 
  92.  */
  93.  public function getStartDate()
  94.  {
  95.  return $this->startDate;
  96.  }
  97.  
  98.  /**
  99.  * Set FinishDate
  100.  *
  101.  * @param \DateTime $finishDate
  102.  * @return InputStatus
  103.  */
  104.  public function setFinishDate($finishDate)
  105.  {
  106.  $this->finishDate = $finishDate;
  107.  
  108.  return $this;
  109.  }
  110.  
  111.  /**
  112.  * Get FinishDate
  113.  *
  114.  * @return \DateTime 
  115.  */
  116.  public function getFinishDate()
  117.  {
  118.  return $this->finishDate;
  119.  }
  120.  
  121.  /**
  122.  * Set DeviceId
  123.  *
  124.  * @param \AppBundle\Entity\Device $deviceId
  125.  * @return InputStatus
  126.  *
  127. */
  128.  public function setDeviceId(\AppBundle\Entity\Device $deviceId = null)
  129.  {
  130.  $this->deviceId = $deviceId;
  131.  
  132.  return $this;
  133.  }
  134.  
  135.  /**
  136.  * Get DeviceId
  137.  *
  138.  * @return \AppBundle\Entity\Device
  139.  */
  140.  public function getDeviceId()
  141.  {
  142.  return $this->deviceId;
  143.  }
  144. }



- Device.php(Entity파일)

  1. <?php
  2.  
  3. namespace AppBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7.  
  8. /**
  9.  * Device
  10.  *
  11.  * @ORM\Table(name="device_info")
  12.  * @ORM\Entity(repositoryClass="AppBundle\Repository\DeviceRepository")
  13.  * @UniqueEntity(
  14.  * fields={"deviceId"},
  15.  * message="The device id is already used."
  16.  * )
  17.  */
  18. class Device
  19. {
  20.  
  21. //const ESTADO_INICIAL = 1;
  22. //const ESTADO_ALARMAS_INICIAL = 1;
  23.  
  24.  /**
  25.  * @var int
  26.  *
  27.  * @ORM\Column(name="id", type="integer")
  28.  * @ORM\Id
  29.  * @ORM\GeneratedValue(strategy="AUTO")
  30.  */
  31.  private $id;
  32.  
  33.  /**
  34.  * @var string
  35.  *
  36.  * @ORM\Column(name="device_id", type="string", length=32)
  37.  */
  38.  private $deviceId;
  39.  
  40.  /**
  41.  * @var int
  42.  *
  43.  * @ORM\Column(name="device_status", type="integer", nullable=true)
  44.  */
  45.  private $deviceStatus;
  46.  
  47. /**
  48.  * @var string
  49.  *
  50.  * @ORM\Column(name="device_ref", type="string", length=64, nullable=true)
  51.  */
  52.  private $deviceRef;
  53.  
  54. /**
  55.  * @var string
  56.  *
  57.  * @ORM\Column(name="address", type="string", length=128, nullable=true)
  58.  */
  59.  private $address;
  60.  
  61.  /**
  62.  * @var float
  63.  *
  64.  * @ORM\Column(name="latitude", type="float", nullable=true)
  65.  */
  66.  private $latitude;
  67.  
  68.  /**
  69.  * @var float
  70.  *
  71.  * @ORM\Column(name="longitude", type="float", nullable=true)
  72.  */
  73.  private $longitude;
  74.  
  75.  /**
  76.  * @var string
  77.  *
  78.  * @ORM\Column(name="phone", type="bigint", nullable=true)
  79.  */
  80.  private $phone;
  81.  
  82.  /**
  83.  * @var int
  84.  *
  85.  * @ORM\Column(name="alarm", type="integer", nullable=true)
  86.  */
  87.  private $alarm;
  88.  
  89. /**
  90.  * @var string
  91.  *
  92.  * @ORM\Column(name="pwd", type="string", length=32, nullable=true)
  93.  */
  94.  private $pwd;
  95.  
  96.  /**
  97.  * Constructor
  98.  */
  99.  public function __construct()
  100.  {
  101. $this->deviceStatus = 0;
  102.  }
  103.  
  104. public function __toString(){
  105. return $this->deviceId;
  106. }
  107.  
  108. /**
  109.  * Get id
  110.  *
  111.  * @return integer
  112.  */
  113.  public function getId()
  114.  {
  115.  return $this->id;
  116.  }
  117.  
  118.  /**
  119.  * Set deviceId
  120.  *
  121.  * @param string $deviceId
  122.  * @return Device
  123.  */
  124. public function setDeviceId($deviceId)
  125. {
  126. $this->deviceId = $deviceId;
  127.  
  128. return $this;
  129. }
  130.  
  131.  /**
  132.  * Get deviceId
  133.  *
  134.  * @return string
  135.  */
  136. public function getDeviceId()
  137. {
  138. return $this->deviceId;
  139. }
  140.  
  141.  /**
  142.  * Set deviceStatus
  143.  *
  144.  * @param string $deviceStatus
  145.  * @return Device
  146.  */
  147. public function setDeviceStatus($deviceStatus)
  148. {
  149. $this->deviceStatus = $deviceStatus;
  150.  
  151. return $this;
  152. }
  153.  
  154.  /**
  155.  * Get deviceStatus
  156.  *
  157.  * @return string
  158.  */
  159. public function getDeviceStatus()
  160. {
  161. return $this->deviceStatus;
  162. }
  163.  
  164.  /**
  165.  * Set deviceRef
  166.  *
  167.  * @param string $deviceRef
  168.  * @return Device
  169.  */
  170. public function setDeviceRef($deviceRef)
  171. {
  172. $this->deviceRef = $deviceRef;
  173.  
  174. return $this;
  175. }
  176.  
  177.  /**
  178.  * Get deviceRef
  179.  *
  180.  * @return string
  181.  */
  182. public function getDeviceRef()
  183. {
  184. return $this->deviceRef;
  185. }
  186.  
  187.  /**
  188.  * Set address
  189.  *
  190.  * @param string $address
  191.  * @return Device
  192.  */
  193. public function setAddress($address)
  194. {
  195. $this->address = $address;
  196.  
  197. return $this;
  198. }
  199.  
  200.  /**
  201.  * Get address
  202.  *
  203.  * @return string
  204.  */
  205. public function getAddress()
  206. {
  207. return $this->address;
  208. }
  209.  
  210.  /**
  211.  * Set latitude
  212.  *
  213.  * @param float $latitude
  214.  * @return Device
  215.  */
  216. public function setLatitude($latitude)
  217. {
  218. $this->latitude = $latitude;
  219.  
  220. return $this;
  221. }
  222.  
  223.  /**
  224.  * Get latitude
  225.  *
  226.  * @return float
  227.  */
  228. public function getLatitude()
  229. {
  230. return $this->latitude;
  231. }
  232.  
  233.  /**
  234.  * Set longitude
  235.  *
  236.  * @param float $longitude
  237.  * @return Device
  238.  */
  239. public function setLongitude($longitude)
  240. {
  241. $this->longitude = $longitude;
  242.  
  243. return $this;
  244. }
  245.  
  246.  /**
  247.  * Get longitude
  248.  *
  249.  * @return float
  250.  */
  251. public function getLongitude()
  252. {
  253. return $this->longitude;
  254. }
  255.  
  256.  /**
  257.  * Set phone
  258.  *
  259.  * @param string $phone
  260.  * @return Device
  261.  */
  262. public function setPhone($phone)
  263. {
  264. $this->phone = $phone;
  265.  
  266. return $this;
  267. }
  268.  
  269.  /**
  270.  * Get phone
  271.  *
  272.  * @return string
  273.  */
  274. public function getPhone()
  275. {
  276. return $this->phone;
  277. }
  278.  
  279.  /**
  280.  * Set alarm
  281.  *
  282.  * @param int $alarm
  283.  * @return Device
  284.  */
  285. public function setAlarm($alarm)
  286. {
  287. $this->alarm = $alarm;
  288.  
  289. return $this;
  290. }
  291.  
  292.  /**
  293.  * Get alarm
  294.  *
  295.  * @return int
  296.  */
  297. public function getAlarm()
  298. {
  299. return $this->alarm;
  300. }
  301.  
  302.  /**
  303.  * Set pwd
  304.  *
  305.  * @param string $pwd
  306.  * @return Device
  307.  */
  308. public function setPwd($pwd)
  309. {
  310. $this->pwd = $pwd;
  311.  
  312. return $this;
  313. }
  314.  
  315.  /**
  316.  * Get pwd
  317.  *
  318.  * @return string
  319.  */
  320. public function getPwd()
  321. {
  322. return $this->pwd;
  323. }
  324. }



위의 두 파일에서 InputStatus.php파일에서 Device.php파일의 $deviceId컬럼을 join하려고 합니다. join column방법(@ORM\ManyToOne)에 문제가 있는것 같아요.

감사합니다.

android 님 질문에 도움이 되셨나요?? 조민우 2016.09.28 00:48:57

작성해주신 엔티티로 반영해보니 아래와 같이 General error: 1005 (errorno: 150) 이 발생하네요  

  1.  [Doctrine\DBAL\Exception\DriverException]
  2.   An exception occurred while executing 'ALTER TABLE input_status ADD CONSTRAINT FK_B7B4EC4194A4C7D4 FOREIGN KEY (device_id) REFERENCES device (device_id)':
  3.   SQLSTATE[HY000]: General error: 1005 Can't create table 'symfony2_test.#sql-2c44_f' (errno: 150)

외래키에 대한 제약조건을 준수하지 못한 경우 토해내는 에러코드인데요

제약 조건 중에 참조하는 '부모 테이블'의 컬럼은 반드시 PK이거나 UNIQUE해야 한다는 제약조건이 있습니다.

아래와 같이 부모테이블(Devide)의  deviceId를 유니크로 변경해 보시기 바랍니다.


  1. /**
  2.  * @var string
  3.  *
  4.  * @ORM\Column(name="device_id", type="string", length=32, unique=true)
  5.  */
  6.  private $deviceId;


건투를 빕니다!!!^_^

2016.07.26 22:54:52 반응 이력

답변 작성

질문에 적합한 답변을 상세히 작성해 주시기 바랍니다.

답변이 찬성되면 태그평판 +2점이 적립, 반대되면 태그평판 -1점 차감됩니다.

답변이 채택되면 태그평판 +10점이 적립됩니다.

etc 게시판 정보
  • 1.7k
    질문수
  • 100
    아카이브수
  • 46
    채택수
  • 0
    멤버수
etc 질문 통계
최근 30일
답변율
6%
채택율
0%
전체
답변율
3%
채택율
2%
최근에 등록된 질문