Call to a member function setId() on a non-object in magento - magento-1.7

Call to a member function setId() on a non-object in
and here is my code
require_once '../app/Mage.php';
Mage::app();
$model = Mage::getModel('catelog/product');
for($id=1;$id<4;$id++)
echo "in loop";
// Mage::init();
try
{
$model->setId($id)->delete();
echo "Data deleted successfully.";
}
catch (Exception $e)
{
echo $e->getMessage();
}
how i solve this prob please tell me

Change
$model = Mage::getModel('catelog/product');
To
$model = Mage::getModel('catalog/product');
To delete a product:
$product = Mage::getModel('catalog/product')->load($productId);
$product->delete();

Related

ZF2 application - Dispatch error event triggered every time

I'am working on a Zend Framework 2 application and have a strange behavior concerning error handling. My code in Module.php:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, [$this, 'onPreRoute'], 100);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'handleError']);
}
public function onPreRoute(MvcEvent $e)
{
$serviceManager = $e->getTarget()->getServiceManager();
$router = $serviceManager->get('router');
$router->setTranslator($serviceManager->get('translator'));
}
public function handleError(MvcEvent $e)
{
$error = $e->getParam('error');
file_put_contents('error.log', $error . PHP_EOL, FILE_APPEND);
switch($error) {
case 'error-router-no-match':
$router = $e->getRouter();
$url = $router->assemble([], ['name' => 'home']);
header('Location: ' . $url);
exit;
}
}
As you can see I'am translating the routes. This works fine. But on every request the dispatch error event is triggered too. The error.log file will be created every time. But the redirect will be only performed if the route doesn't really exist. I think it depends on the translator or is my code in Module.php not correct?
Resolved!
The reason was that the browser automatically requests /favicon.ico and that was not available :-)

htmlPurifier not working with Froala editor WYSIWYG

When i Input.:
<script>alert("XSS")</script>Cleaning Test
My output should be
Cleaning Test
but i get same as input <script>alert("XSS")</script>Cleaning Test
can someone help me to solve this problem
and tried a lot but doesn't works i need to check my htmlpurifie is working
this is my code
<?php
require_once 'htmlpurifier/library/HTMLPurifier.auto.php';
ini_set("display_errors", 1);
error_reporting(E_ALL);
define('DB_SERVER', "localhost");
define('DB_USER', "sanoj");
define('DB_PASSWORD', "123456");
define('DB_DATABASE', "test");
define('DB_DRIVER', "mysql");
$country = filter_input(INPUT_POST, 'title');
$dirty_html = filter_input(INPUT_POST, 'wysiwyg');
$purifier = new HTMLPurifier();
$clean_html = $purifier->purify($dirty_html);
try {
$db = new PDO(DB_DRIVER . ":dbname=" . DB_DATABASE . ";host=" . DB_SERVER, DB_USER, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("INSERT INTO final(title, wysiwyg) VALUES (:title, :wysiwyg)");
$stmt->bindParam(':title', $country, PDO::PARAM_STR, 100);
$stmt->bindParam(':wysiwyg', $clean_html, PDO::PARAM_STR, 100);
if ($stmt->execute()) {
echo '1 row has been inserted';
}
$db = null;
} catch (PDOException $e) {
trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
}
?>

facebook php sdk 4 version, unable to fetch user albums

I am developing a facebook application via php sdk 4 version.
My code is as follows:
try {
$session = $helper->getSession();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
if ($session) {
try {
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$me = $response->getGraphObject();
$user_id = $me->getProperty('id');
echo $user_id;
$accessToken = $session->getAccessToken();
echo $accessToken;
echo "<br>".$user_id;
$request = new FacebookRequest($session, 'GET', '/me/albums');
$response = $request->execute();
$userAlbums = $response->getGraphObject();
echo $userAlbums['data'][0]['id'];
} catch(FacebookRequestException $e) {
echo $e->getMessage();
}
} else {
$helper = new FacebookRedirectLoginHelper('https://apps.facebook.com/lykebook/');
$auth_url = $helper->getLoginUrl(array('user_friends', 'publish_actions', 'user_photos', 'user_status', 'friends_photos','friends_status','publish_stream'));
echo "<script>window.top.location.href='".$auth_url."'</script>";
}
But the problem is I am not getting any album data. I don't know what the problem is? The earlier request i.e: /me is working fine. I checked that by printing $user_id. But the next request for getting albums is not working i.e /me/albums. Help me in correcting this.
Try using the getGraphObjectList() method since you are expecting more than one object. Then the result will be an array of GraphObject objects, see here.
From here, you need to access these as objects and not arrays with the helper methods available (e.g. getProperty()).
Otherwise, you can retrieve the array backing this object with asArray().
You could use getGraphEdge, here below the code:
$fb->setDefaultAccessToken($accessToken);
try {
$response = $fb->get('/me/albums');
$albums = $response->getGraphEdge();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
foreach ($albums as &$value) {
echo $value;
}

SLIM framework how to assign conditions to route callback function

I have a route defined in my Slim app like so:
$app->get('/marcas/:id', 'getMarcas');
My callback function is defined as:
function getMarcas($id) {
$sql = "SELECT * FROM marcas WHERE id=:id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$marcas = $stmt->fetchObject();
$db = null;
echo json_encode($mrcas);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
How can I apply a route condition like:
->conditions(array('id' => '[0-9]{2,}'));
Thanks
You can assign conditions exactly the way you guessed. See the Route Conditions documentation for details: http://docs.slimframework.com/#Route-Conditions
you can use
$app = new \Slim\Slim();
$app->get('/hello/:firstName/:lastName', $callable)
->conditions(array('lastName' => '[0-9]{2,}'));
with calling of get/post

Sending a SOAP message with PHP

what I'm trying to do is send a load of values captured from a form to a CRM system with SOAP and PHP. I've been reading up on SOAP for a while and I don't understand how to go about doing so, does anybody else know?
In order to do this it might be easiest to download a simple soap toolkit like 'NuSOAP' from sourceforge.
And then you would code something like the following (example submission of ISBN number):
<?php
// include the SOAP classes
require_once('nusoap.php');
// define parameter array (ISBN number)
$param = array('isbn'=>'0385503954');
// define path to server application
$serverpath ='http://services.xmethods.net:80/soap/servlet/rpcrouter';
//define method namespace
$namespace="urn:xmethods-BNPriceCheck";
// create client object
$client = new soapclient($serverpath);
// make the call
$price = $client->call('getPrice',$param,$namespace);
// if a fault occurred, output error info
if (isset($fault)) {
print "Error: ". $fault;
}
else if ($price == -1) {
print "The book is not in the database.";
} else {
// otherwise output the result
print "The price of book number ". $param[isbn] ." is $". $price;
}
// kill object
unset($client);
?>
This code snippet was taken directly from, which is also a good resource to view
http://developer.apple.com/internet/webservices/soapphp.html
Hope this helps.
You probably found a solution since then - but maybe the following helps someone else browsing for this:
soap-server.php:
<?php
class MySoapServer {
public function getMessage ()
{
return "Hello world!";
}
public function add ($n1,$n2)
{
return $n1+n2;
}
}
$option = array ('uri' => 'http://example.org/stacky/soap-server');
$server = new SoapServer(null,$option);
$server->setClass('MySoapServer');
$server->handle();
?>
and soap-client.php
<?php
$options = array ('uri' => 'http://example.org/stacky/soap-server',
'location' => 'http://localhost/soap-server.php');
$client = new SoapClient(null,$options);
echo $client ->getMessage();
echo "<br>";
echo $client ->add(41,1);
?>