Magento. Saving data in custome table - magento

I am trying insert data in my table but allways get this error
Fatal error: Call to undefined method UltimoValue_Warranty_Model_Mysql4_Warranty_Collection::setData() in
/var/www/magento.dev/app/code/community/UltimoValue/Warranty/controllers/IndexController.php
on line 17
here is the saving code in IndexController
public function saveAction()
{
$warrantiesCollection = Mage::getModel('warranty/warranty')->getCollection();
$post = $this->getRequest()->getPost();
if(isset($post['order_number']) && isset($post['save'])) {
try {
$warrantiesCollection->setData('id', null);
$warrantiesCollection->setData('first_name', $post['first_name']);
$warrantiesCollection->setData('last_name', $post['last_name']);
$warrantiesCollection->setData('order_number', $post['order_number']);
$warrantiesCollection->setData('tablet_serial_number', $post['tablet_serial_number']);
$warrantiesCollection->setData('date_purchased', $post['date_purchased']);
$warrantiesCollection->save();
} catch(Exception $e) {
echo $e->getMessage(), "\n";
}
header("Location: " . $_SERVER['HTTP_ORIGIN'] . "/index.php/warranty/index/thankyou/");
exit();
}
}
when i am using getData - all fine...
What i am doing wrong ?

instead
$warrantiesCollection = Mage::getModel('warranty/warranty')->getCollection();
i have to write
$warrantiesCollection = Mage::getModel('warranty/warranty');
thanks to all

Related

Fatal error: Call to a member function save_file_info() on a non-object in /opt/lampp/htdocs/ERP/application/controllers/demo2.php on line 82

I am a new PHP developer, I have been working sample codeigniter program file upload.
File upload successfully in the folder, but error was coming. How to resolve..
Controller // There were errors, we have to delete the uploaded files
if ($is_file_error) {
if ($file) {
$file = './uploads/' . $file['file_name'];
if (file_exists($file)) {
unlink($file);
}
}
}
if (!$is_file_error) {
//save the file info in the database
$resp = $this->file->save_file_info($file);
if ($resp === TRUE) {
$this->handle_success('File was successfully uploaded.');
} else {
//if file info save in database was not successful then delete from the destination folder
$file = './upload/' . $file['file_name'];
if (file_exists($file)) {
unlink($file);
}
$this->handle_error('Error while saving file info to Database.');
}
}
Your $this->file does not contains object of file, put this in if condition and then call save_file_info($file) on it.
if( $this->file )
{
$resp = $this->file->save_file_info($file);
}
else
{
$this->handle_error('File is not uploaded properly!, please try again.');
}

Blade :: compileString() and embedded variables

I am able to parse HTMl that uses blade template variables through the following code:
$generated = Blade::compileString($string);
ob_start();
try
{
eval($generated);
}
catch (\Exception $e)
{
ob_get_clean(); throw $e;
}
$content = ob_get_clean();
return $content;
And it works fine as long as i don't use blade variables within. Which on being parsed give me undefined variable error. How can i make sure that blade variables are available in my custom parsing method?
This works for me with the latest version of Laravel 5.7. Notice how I include the __env var so that functions like #include, #foreach, etc can work.
public static function renderBlade($string, $data = null)
{
if (!$data) {
$data = [];
}
$data['__env'] = app(\Illuminate\View\Factory::class);
$php = Blade::compileString($string);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);
try {
eval('?' . '>' . $php);
} catch (Exception $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw $e;
} catch (Throwable $e) {
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
throw new FatalThrowableError($e);
}
return ob_get_clean();
}
Turns out I was not passing the arguments array to the method that parses the Blade structure. My assumption was that the Mail::send method takes care of making variables available which it takes in as a second parameter. I also had to extract($args, EXTR_SKIP).
$generated = Blade::compileString($string);
ob_start(); extract($args, EXTR_SKIP)
try
{
eval($generated);
}
catch (\Exception $e)
{
ob_get_clean(); throw $e;
}
$content = ob_get_clean();
return $content;

Parse error NuSOAP webservice with Codeigniter

I'm using CodeIgniter with NuSOAP library for webservices and this is the error I get when accessing the Client controller:
wsdl error: XML error parsing WSDL from http://localhost/turismoadmin/index.php/Webservice/index/wsdl on line 77: Attribute without value
This is the server controller:
class Webservice extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->library('soap_lib');
$server = new nusoap_server;
$server->configureWSDL('Agencia Turistica', 'urn:server');
$server->wsdl->schemaTargetNamespace = 'urn:server';
$server->register('addcontact',
array('nombre' => 'xsd:string', 'apellido' => 'xsd:string' , 'ciudad' => 'xsd:string'),
array('return' => 'xsd:string'));
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA: '';
$server->service($HTTP_RAW_POST_DATA);
}
function index()
{
if($this->uri->rsegment(3)=="wsdl"){
$_SERVER['QUERY_STRING']="wsdl";
}else{
$_SERVER['QUERY_STRING']="";
}
function addcontact($nombre, $apellido, $ciudad){
$this->modelo_turismo->addcontact($nombre, $apellido, $ciudad);
$resultado = $this->modelo_turismo->selectmax_contacto();
return (json_encode($resultado->fetch_all()));
}
}
}
and this is the Client controller:
class Client extends CI_controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->library('soap_lib');
$this->nusoap_client = new nusoap_client(site_url('Webservice/index/wsdl'), true);
$err = $this->nusoap_client->getError();
if ($err){
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
$result1 = $this->nusoap_client->call('addcontact', array("marcos","de lafuente","hermosillo"));
echo($result1);
// Check for a fault
if ($this->nusoap_client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result1);
echo '</pre>';
} else {
// Check for errors
$err = $this->nusoap_client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result1);
echo '</pre>';
}
}
}
}
I'm trying to do it based
ON THIS TOPIC (Thanks nana.chorage)
I also added this entry to my config/routes.php
$route['Webservice/wsdl']="Webservice/index/wsdl";
And for not to pass unnoticed, I can see my service when I enter this URL:
http://localhost/turismoadmin/index.php/Webservice/wsdl
I really dont know what I'm doing wrong, I have searched a lot around and I can't get rid of it!
Then nusoap client URL should be like this
$this->nusoap_client = new nusoap_client(site_url('Webservice/index?wsdl'), 'wsdl');

How can I delete a record from database in a ajax request in magento

I have created custom module and I am working on record deletion but its not working. Code which I am using is
$keyId=$params = $this->getRequest()->getParams('id');
$model = Mage::getModel('groupprice/groupprice');
try {
$model->setId($keyId)->delete();
echo "Data deleted successfully.";
} catch (Exception $e){
echo $e->getMessage();
}
Is anything wrong ?
This code is working in a simple request but not working with ajax request .
You should change code and use getPost
$keyId=$params = $this->getRequest()->getPost('id');
Hope this wil help.
Try this code :
$keyId=$params = $this->getRequest()->getParam('id');
$model = Mage::getModel('groupprice/groupprice');
try
{
$model->setId($keyId)->delete();
echo "Data deleted successfully.";
}
catch (Exception $e)
{
echo $e->getMessage();
}

"File was not uploaded" magento error even file is uploaded to particular folder

I am making an application in which i am uploading file from my custom tab as follows
i created a observer for event called catalog_product_save_after. In the particular function, i am uploading those files to catalog/product folder and its get uploaded to those folder without any error.
But when i am trying to assign those images to that particular product, i got the following error.
File was not uploaded at file E:\xampp\htdocs\magento\lib\Varien\File\Uploader.php on line 152
If i check system.log then i see the 2012-08-24T11:33:15+00:00 ERR (3): User Error: Some transactions have not been committed or rolled back in E:\xampp\htdocs\magento\lib\Varien\Db\Adapter\Pdo\Mysql.php on line 3645 this error.
My observer function is as follows
public function Savedata($observer)
{
$params = Mage::app()->getRequest()->getParams();
$product = $observer->getEvent()->getProduct();
$product_id = $product->getId();
$filesData = array();
$keysData = array_keys($_FILES);
foreach($keysData as $keys)
{
$uploader = new Varien_File_Uploader("$keys");
$uploader->setAllowedExtensions(array('jpeg', 'jpg', 'png', 'tiff'));
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS . "catalog" . DS . "product";
if(!is_dir($path))
{
mkdir($path);
}
$extension = pathinfo($_FILES["$keys"]['name'], PATHINFO_EXTENSION);
$date = new DateTime();
$file_name = $keys . "_" . $product_id . "." . $extension;
$_FILES["$keys"]['name'] = $file_name;
$uploader->save($path, $_FILES["$keys"]['name']);
$filesData[] = $path .DS. $_FILES["$keys"]['name'];
}
print_r($filesData);
///till this works file.. when i add following code, program dies.
try
{
$productData = Mage::getModel('catalog/product')->load($product_id);
print_r($productData->getData());
foreach($filesData as $file)
{
$productData->addImageToMediaGallery($file, "image" ,false, false);
}
$productData->save();
$productData = Mage::getModel('catalog/product')->load($product_id);
print_r($productData->getData());
}
catch (Exception $e)
{
echo $e->getMessage() . "||" . $e->getCode() . "||" . $e->getFile() . "||" . $e->getLine();
}
exit();
}
any suggestion where i am going wrong? why this error occur?
i am using magento 1.7
I see that you're going to update your catalog/product object when you're saving it.
You are using event catalog_product_save_after.
Inside the observer, you call:
$productData = Mage::getModel('catalog/product')->load($product_id);
...
$productData->save();
$productData is catalog/product object -> you saved it.
You can guess what will happen, it will trigger event catalog_product_save_after and so on, looping forever.
I see that you call $productData->save(); because in this event, the value has been saved and can not be changed.
So instead of using catalog_product_save_after, you can use catalog_product_save_before.
The same like your code, but remove $productData->save(); because it will automatically call save.
Mage_Core_Model_Abstract
public function save()
{
/**
* Direct deleted items to delete method
*/
if ($this->isDeleted()) {
return $this->delete();
}
if (!$this->_hasModelChanged()) {
return $this;
}
$this->_getResource()->beginTransaction();
$dataCommited = false;
try {
$this->_beforeSave();
if ($this->_dataSaveAllowed) {
//see this line below, it will call save, so you don't need to call save in your `catalog_product_save_before` (no looping forever)
$this->_getResource()->save($this);
$this->_afterSave();
}
$this->_getResource()->addCommitCallback(array($this, 'afterCommitCallback'))
->commit();
$this->_hasDataChanges = false;
$dataCommited = true;
} catch (Exception $e) {
$this->_getResource()->rollBack();
$this->_hasDataChanges = true;
throw $e;
}
if ($dataCommited) {
$this->_afterSaveCommit();
}
return $this;
}

Resources