forsaken122 Posted April 6, 2011 Share Posted April 6, 2011 This is my goal: Attach an attachment to Employee.First of all, I already added a table to prestashop database called "ps_employee_attachment" with three fields (id, id_attachment , id_employee). Second I have added a list box with Employees in New Attachment Form (Catalog>Attachment>New Attachment), where i can select the employee associated at the Attachment.Third, in the file class /classes/Attachment.php I added this function needed for populate the "ps_employee_attachment" table: public static function attachToEmployee($id_employee ) { return Db::getInstance()->execute("INSERT INTO `"._DB_PREFIX_."employee_attachment` (`id_employee`, `id_attachment`) VALUES (".intval($id_employee).",".intval($this->id).")"); } Finally, I added this piece of code to the function "postProcess" in /adminXX/tabs/AdminAttachments.php, this code make the call to the function "attachToEmployee" when a new Attachment is added, here my "postProcess" function(my added code is between comments): public function postProcess() { if (Tools::isSubmit('submitAdd'.$this->table)) { if ($id = intval(Tools::getValue('id_attachment')) AND $a = new Attachment($id)) { $_POST['file'] = $a->file; $_POST['mime'] = $a->mime; } if (!sizeof($this->_errors)) if (isset($_FILES['file']) AND is_uploaded_file($_FILES['file']['tmp_name'])) { if ($_FILES['file']['size'] > $this->maxFileSize) $this->_errors[] = $this->l('File too large, maximum size allowed:').' '.($this->maxFileSize/1000).' '.$this->l('kb'); else { $uploadDir = dirname(__FILE__).'/../../download/'; do $uniqid = sha1(microtime()); while (file_exists($uploadDir.$uniqid)); if (!copy($_FILES['file']['tmp_name'], $uploadDir.$uniqid)) $this->_errors[] = $this->l('File copy failed'); @unlink($_FILES['file']['tmp_name']); /*HERE MY ADDED CODE*/ if ($a->attachToEmployee($_POST['id_employee'])) $this->_errors[] = $this->l('id_employee: '.$_POST['id_employee'].' - id: '.$id); /*HERE MY ADDED CODE - END*/ $_POST['name_2'] .= '.'.pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $_POST['file'] = $uniqid; $_POST['mime'] = $_FILES['file']['type']; } } $this->validateRules(); } return parent::postProcess(); } It doesn't work, when I insert a new Attchment I see only Prestashop backend menu. And there is no new row in my ps_employee_attachment.What's wrong? Is this possible?Thanks in advance Link to comment Share on other sites More sharing options...
Broceliande Posted April 6, 2011 Share Posted April 6, 2011 Hi,i'd try using $this->id_attachment rather than $this->id in : return Db::getInstance()->execute("INSERT INTO `"._DB_PREFIX_."employee_attachment` (`id_employee`, `id_attachment`) VALUES (".intval($id_employee).",".intval($this->id).")"); Link to comment Share on other sites More sharing options...
forsaken122 Posted April 7, 2011 Author Share Posted April 7, 2011 It display this error:Fatal error: Call to undefined method AdminAttachments::displayConfirmation() in /web/htdocs/www.caffenormanno.it/home/shop/admin1708/tabs/AdminAttachments.php on line 62l('id_employee: '.$_POST['id_employee'].' - id: '.$id);[/code]But if I delete this to "displayConfirmation()" I get this:Fatal error: Using $this when not in object context in /web/htdocs/www.caffenormanno.it/home/shop/classes/Attachment.php on line 93So the problem is that I can't call the attribute $this inside my method "attachToEmployee" in Attachments.php classI have an error also writing this: $a->attachToEmployee($_POST['id_employee']); so I have written this: Attachment::attachToEmployee($_POST['id_employee']); My problem is get the correct value of attachment's id. Link to comment Share on other sites More sharing options...
Broceliande Posted April 7, 2011 Share Posted April 7, 2011 Your method is written as static public static function ... But you need to be in objet context if you wanna be able to use $this object and then get the attachment id.You should use : public function attachToEmployee($id_employee ) {.... and then $a->attachToEmployee($_POST['id_employee']); Then just place a var_dump($this); in attachToEmployee method to be sure of its id's variable , should be $this->id_attachment Link to comment Share on other sites More sharing options...
forsaken122 Posted April 7, 2011 Author Share Posted April 7, 2011 I have modify my "attachToEmployee" function like this: public function attachToEmployee($id_employee) { var_dump($this); return Db::getInstance()->execute("INSERT INTO `"._DB_PREFIX_."employee_attachment` (`id_employee`, `id_attachment`) VALUES (".intval($id_employee).",".intval($this->id_attachment).")"); } and I call "attachToEmployee" with this call: $a->attachToEmployee($_POST['id_employee']); And I get this error: Fatal error: Call to a member function attachToEmployee() on a non-object in /web/htdocs/www.caffenormanno.it/home/shop/admin1708/tabs/AdminAttachments.php on line 61 Link to comment Share on other sites More sharing options...
Broceliande Posted April 7, 2011 Share Posted April 7, 2011 Well that's another error,There it just means that $a is not an instance of Attachment Class.$a is defined there but has a local scope : if ($id = intval(Tools::getValue('id_attachment')) AND $a = new Attachment($id)) { You have to instanciate your obj again .Meanwhile, you also are able to pass its id to your method in case you can't get it using $this (but i'm pretty sure you can): /*HERE MY ADDED CODE*/ if ($id = intval(Tools::getValue('id_attachment')) AND $a = new Attachment($id)) { if ($a->attachToEmployee($_POST['id_employee']),$id) $this->_errors[] = $this->l('id_employee: '.$_POST['id_employee'].' - id: '.$id); } /*HERE MY ADDED CODE - END*/ public function attachToEmployee($id_employee,$idattachment) { var_dump($this); return Db::getInstance()->execute("INSERT INTO `"._DB_PREFIX_."employee_attachment` (`id_employee`, `id_attachment`) VALUES (".intval($id_employee).",".$idattachment.")"); } should work Link to comment Share on other sites More sharing options...
forsaken122 Posted April 7, 2011 Author Share Posted April 7, 2011 I get no error with your code, but it doesn't work because I can't see new row in my table on database.Infact it works only if this "if statment" is verified if ($id = intval(Tools::getValue('id_attachment')) AND $a = new Attachment($id)){ //add row in table } I don't know when this condition works! Link to comment Share on other sites More sharing options...
Broceliande Posted April 7, 2011 Share Posted April 7, 2011 In fact this condition will be vérified as soon as you have id_attachment set in get or post variables.Without it you just cannot get an instance of Attachment , nor know the attachment id ?however you have this condition before : if (!sizeof($this->_errors)) ...So let's see if the condition is verified : if ($id = intval(Tools::getValue('id_attachment')) AND $a = new Attachment($id)){ die('I got it verified!'); ..... ..... } Link to comment Share on other sites More sharing options...
Broceliande Posted April 7, 2011 Share Posted April 7, 2011 hmm , last but not least : of course your code will only be executed when you upload a file . did you expect something else? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now