我有一个productRepositiry模型,用于在数据库中创建产品
class ProductRepository extends EntityRepository{
public function createProduct($productDetails) {
$this->_em->persist($productDetails);
$this->_em->flush();
log_message("info","Product {$productDetails->name} created.");
print "Product {$productDetails->name} created.<br/><br/><br/>";
return $productDetails;
}
procuct controller,使用post获取值并将值传递给productRepositiry
public function addProduct(){
if(! $this->session->userdata('user_id')){
redirect('welcome');
}else{
$productDetails = new Entity\Product;
$productDetails->market = $this->input->post('mid');
$productDetails->name = $this->input->post('product_name');
$productDetails->price = $this->input->post('product_price');
$productDetails->discount = $this->input->post('product_discount');
$productDetails->category = $this->input->post('product_cat');
$productDetails->forwho = $this->input->post('product_for');
$productDetails->desc = $this->input->post('product_desc');
$productDetails->created = new DateTime("now");
![enter image description here][1]
$productRepository = $this->doctrine->em->getRepository('Entity\Product');
if($productRepository) echo "found;";
$createdProduct = $productRepository->createProduct($productDetails);
echo "Level 2 -> ";
$p = $productRepository->findBy(array('id' => $createdProduct->id));
echo "\n" . $p[0]->id. " => " . $p[0]->name;
}
}
错误消息::消息:spl_object_hash()期望参数1为对象,给定字符串
答案 0 :(得分:1)
我不知道实体Product
的确切结构是什么。但我会要求您验证Product
是否与其他实体有某种关系,但您可能将这些实体作为字符串而不是实际实体传递。
您可以使用find
函数获取实体。