Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [CodeIgniter][Doctrine2] Insert
Forum PHP.pl > Forum > PHP > Frameworki
angel170
Chciałe dodać rekord do tabeli stworzonej zgodnie z manualem 6.6. One-To-Many, Bidirectional?

  1. CREATE TABLE Product (
  2. id INT AUTO_INCREMENT NOT NULL,
  3. PRIMARY KEY(id)
  4. ) ENGINE = InnoDB;
  5. CREATE TABLE Feature (
  6. id INT AUTO_INCREMENT NOT NULL,
  7. product_id INT DEFAULT NULL,
  8. PRIMARY KEY(id)
  9. ) ENGINE = InnoDB;
  10. ALTER TABLE Feature ADD FOREIGN KEY (product_id) REFERENCES Product(id);

  1. <?php
  2. /** @Entity **/
  3. class Product
  4. {
  5. // ...
  6. /**
  7.   * @OneToMany(targetEntity="Feature", mappedBy="product")
  8.   **/
  9. private $features;
  10. // ...
  11.  
  12. public function __construct() {
  13. $this->features = new \Doctrine\Common\Collections\ArrayCollection();
  14. }
  15. }
  16.  
  17. /** @Entity **/
  18. class Feature
  19. {
  20. // ...
  21. /**
  22.   * @ManyToOne(targetEntity="Product", inversedBy="features")
  23.   * @JoinColumn(name="product_id", referencedColumnName="id")
  24.   **/
  25. private $product;
  26. // ...
  27.  
  28. function set_product($data){
  29. $this->product = $data;
  30. }
  31.  
  32. }


Jak mam dodać now rekord do klasy Feature ? Jak ustawić zmienną $product ?

  1. $Feature = new models\Feature;
  2. $Feature ->set_product(2);
  3. $em->persist($Feature);
  4. $em->flush() ;


Jak tak zrobie to dostaje error
  1. A PHP Error was encountered
  2.  
  3. Severity: Warning
  4.  
  5. Message: spl_object_hash() expects parameter 1 to be object, integer given
  6.  
  7. Filename: ORM/UnitOfWork.php
  8.  
  9. Line Number: 1318
  10. A PHP Error was encountered
  11.  
  12. Severity: Warning
  13.  
  14. Message: spl_object_hash() expects parameter 1 to be object, integer given
  15.  
  16. Filename: ORM/UnitOfWork.php
  17.  
  18. Line Number: 734
  19. A PHP Error was encountered
  20.  
  21. Severity: Warning
  22.  
  23. Message: get_class() expects parameter 1 to be object, integer given
  24.  
  25. Filename: ORM/UnitOfWork.php
  26.  
  27. Line Number: 738
  28.  
  29.  
  30. ( ! ) Fatal error: Uncaught exception 'Doctrine\ORM\ORMException' with message 'Found entity of type on association models\Feature#product, but expecting models\Product' in C:\wamp\www\ideaWeb_CMS_doctrine\app\libraries\Doctrine\ORM\UnitOfWork.php on line 737
  31. ( ! ) Doctrine\ORM\ORMException: Found entity of type on association models\Feature#product, but expecting models\Product in C:\wamp\www\ideaWeb_CMS_doctrine\app\libraries\Doctrine\ORM\UnitOfWork.php on line 737


No chyba że stworze całkeim inna klase która nię bedzie miałą żadnych relacji i wtedy bede mogł dodac rekord normalnie ale to chyba nie tak powinno sie robić...
Crozin
Cesze powinieneś ustawić jakiś produkt, a tymczasem wstawiasz tam liczbę 2 - nie uważasz, że to trochę bez sensu?
angel170
Fakt nie mysle tongue.gif dzięki
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.