CREATE TABLE Product ( id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id) ) ENGINE = InnoDB; CREATE TABLE Feature ( id INT AUTO_INCREMENT NOT NULL, product_id INT DEFAULT NULL, PRIMARY KEY(id) ) ENGINE = InnoDB; ALTER TABLE Feature ADD FOREIGN KEY (product_id) REFERENCES Product(id);
<?php /** @Entity **/ class Product { // ... /** * @OneToMany(targetEntity="Feature", mappedBy="product") **/ private $features; // ... public function __construct() { $this->features = new \Doctrine\Common\Collections\ArrayCollection(); } } /** @Entity **/ class Feature { // ... /** * @ManyToOne(targetEntity="Product", inversedBy="features") * @JoinColumn(name="product_id", referencedColumnName="id") **/ private $product; // ... function set_product($data){ $this->product = $data; } }
Jak mam dodać now rekord do klasy Feature ? Jak ustawić zmienną $product ?
$Feature = new models\Feature; $Feature ->set_product(2); $em->persist($Feature); $em->flush() ;
Jak tak zrobie to dostaje error
A PHP Error was encountered Severity: Warning Message: spl_object_hash() expects parameter 1 to be object, integer given Filename: ORM/UnitOfWork.php Line Number: 1318 A PHP Error was encountered Severity: Warning Message: spl_object_hash() expects parameter 1 to be object, integer given Filename: ORM/UnitOfWork.php Line Number: 734 A PHP Error was encountered Severity: Warning Message: get_class() expects parameter 1 to be object, integer given Filename: ORM/UnitOfWork.php Line Number: 738 ( ! ) 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 ( ! ) 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ć...