I am trying to add a new product to my database via this form:
<form id="add_product" method="post" action="{{ path('company_addp') }}">
<span class="fa fa-times close" onclick="new_prod()"></span><br />
<span>Add new product:</span>
<div id="holder">
</div>
<label for="img" style="display:inline-block; color: #5eb5e0; cursor: pointer;">click here</label> to add a picture
<input type="file" name="upload" id="img" style="display: none;" />
<input type="text" placeholder="product name" name="name">
<textarea placeholder="product description" name="desc"></textarea>
<input type="text" placeholder="price" name="price"/>
<input type="text" placeholder="quantity" name="qty" />
<input type="text" placeholder="category" name="type" />
<input type="submit" value="add product" />
</form>
via this controller:
public function addproductAction(Request $request){
if($request->getMethod() == "POST"){
$session = $request->getSession();
$id = $session->get('id');
$type = $session->get('type');
if($type == "comp"){
$em = $this->getDoctrine()->getManager();
$prod = new Products();
$prod->setName($request->get('name'));
$prod->setCompany($id);
$prod->setDsc($request->get('desc'));
$prod->setPrice($request->get('price'));
$prod->setStock($request->get('qty'));
$prod->setType($request->get('type'));
$file = $request->files->get('upload');
if(!is_null($file)){
// generate a random name for the file but keep the extension
$filename = uniqid().".".$file->getClientOriginalExtension();
$path = "assets/product";
$file->move($path,$filename); // move the file to a path
$prod->setPic($filename);
$em->flush();
}
$em->persist($prod);
$em->flush();
return $this->redirectToRoute('company_homepage');
}
}
else{
return new Response('you are trying to access wrong route!');
}
}
}
but somehow null is being returned in place of the name of the pic, as of this is the msg I am getting when I submit the form:
An exception occurred while executing 'INSERT INTO products (name, dsc, pic, company, type, price, sold, stock) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["johnsonn's baby shampoo", "it is a baby shampoo and does not hurt the eyes, also good for hair", null, 2, "baby products", "150", null, "990"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'pic' cannot be null
I dont know what the problem is as I am not getting this error for other place where I am updateing the pic with the same code
nullable=true, then$ php app/console doctrine:schema:update --forceto syncronize your entity with the table. Here's an example: symfony.com/blog/symfony2-annotations-gets-better. Or assure that through your form, you get a non-null value.