Search code examples
doctrine-ormcodeigniter-2

spl_object_hash() expects parameter 1 to be object, string given


i have a productRepositiry model which is used to create a product in the database

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, this get the value using post and passes the values to the 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;
    }
}

Error Message :: Message: spl_object_hash() expects parameter 1 to be object, string given


Solution

  • I don't know what's the exact structure of entity Product. But I would ask you to verify if Product has some relation to other entities but you maybe passing those entities as String instead of actual entity.

    You can use find function to get the entity.