Trying to get property 'ship_date' of non-object - php

$this['order'] = $order = \Spot\Shipment\Models\Order::find($this->param('id'));
$progress = 0;
$progress_status = 'warning';
$shipdate = \Carbon\Carbon::parse($order->ship_date);
$deliverydate = (($order->deliverytime)? $shipdate->addHours($order->deliverytime->count) :null);
$today = \Carbon\Carbon::now();
$time_diff = $today->diffInDays($deliverydate, false);
switch($order->requested){

simply put $order is most likely null. Work out why, dump the value of $this->param('id'), it might be null too.
now, some little tips:
use a naming convention for your variables, you are mixing snake_case and nocase.
what is $this['order'], some ArrayAccess implementation? And why not $this->order?
learn more at phptherightway.com
Also as you are new to StackOverflow, please take some time to write out a more thought out question detailing what is going wrong and provide some context.
Also, think about tags, for example, this question has nothing to do with mysql and phpmyadmin, correct?

Related

Symfony 6.0 time format problem database upload

There were some problems with symfony over time. unfortunately, this is not the first time I have run into this. Has anyone ever encountered this problem? If so, thank you for your help.
Controller.php
if($xml = simplexml_load_file($feedUrl)) {
$result["chanel"] = $xml->xpath("/rss/channel/item");
foreach($result as $key => $attribute) {
$i=0;
foreach($attribute as $element) {
$ret[$i]['title'] = (string)$element->title;
$ret[$i]['category'] = (string)$element->category;
$ret[$i]['link'] = (string)$element->link;
$ret[$i]['pubDate'] = json_decode(json_encode($element->pubDate), TRUE);
$ret[$i]['enclosure'] = (array)$element->enclosure;
$ret[$i]['description'] = (string)$element->description;
$i++;
}
}
}
foreach ($ret as $feed){
$newnews = new Newsfeed();
$newnews->setTitle($feed['title']);
$newnews->setCategory($feed['category']);
$newnews->setLink($feed['link']);
$newnews->setDescription($feed['description']);
$newnews->setDate(date("Y-m-d h:i:sa", strtotime($feed['pubDate'][0])));
$newnews->setImage($feed['enclosure']['#attributes']['url']);
$newnews->setSource('2');
$entityManager->persist($newnews);
$entityManager->flush();
}
This problem
The date() function returns a string, so this line won't work:
$newnews->setDate(date("Y-m-d h:i:sa", strtotime($feed['pubDate'][0])));
You can probably just use:
$newnews->setDate(new \DateTimeImmutable($feed['pubDate'][0]));
I have had this problem before. For me, when working with only Dates (no time) it was kind of misleading the fact that MySQL and thus doctrine annotations have the Date type, but PHP/Symfony does not.
As mentioned by Chris, the date() function returns a string, but the class DateTime is your friend here.
Even when it is called DateTime, this class allows formatting and you can indeed, for example, retrieve only a date with it. An example would be:
$date = new DateTime('2000-01-01 12:30:25');
echo $date->format('Y-m-d');
Or in your case, in which you DO want to return the time part too:
$pubDate = new DateTime($feed['pubDate'][0])->format('Y-m-d h:i:sa');
$newnews->setDate($pubDate);
Which can be easily transformed into a one-liner.
This will show only what you ask for in the format() function.
There is one trickiness though. In my country at least, it is very common to format dates with a slash (/), but DateTime->format() will not work with Y/m/d, so keep it in mind and check for the correct format here if you are going to be using slashes.

Unknown PHP obfuscation technique

I've come accross a piece of code using various techniques of obfuscation and, mostly driven by curiosity, have been trying to understand the techniques it uses.
I've done some work on it, but i'm at a point where I don't understand fully what it's doing :
public $x1528 = null;
public $x153c = null;
function __construct()
{
$this->x1528 = new \StdClass();
$this->x153c = new \StdClass();
$this->x1528->x21a9 = "getSingleton";
$this->x1528->x1569 = "x1565";
$this->x1528->x1e45 = "x1e40";
$this->x153c->x3b3b = "x3b38";
$this->x1528->x16c3 = "x16c2";
$this->x1528->x1bec = "x1be8";
$this->x1528->x245a = "x2455";
$this->x1528->x1b14 = "x10d7";
$this->x153c->x36d4 = "x36d2";
$this->x1528->x24d6 = "getSingleton";
$this->x1528->x1876 = "xf0f";
$this->x1528->x2901 = "x2900";
$this->x1528->x1877 = "x1876";
$this->x153c->x335b = "x3356";
$this->x1528->x2836 = "x2833";
$this->x1528->x2119 = "x2115";
$this->x1528->x18bb = "xf3d";
$this->x153c->x349e = "x349a";
$this->x1528->x2383 = "getData";
$this->x1528->x17b1 = "x5f2";
$this->x153c->x2d06 = "xf41";
$this->x1528->x1f35 = "x1f30";
$this->x1528->x1a93 = "x1138";
$this->x1528->x1d79 = "x1d76";
$this->x1528->x1d7c = "x1d79";
$this->x153c->x3248 = "_isAllowed";
...
[it keeps going for a while...]
So it declares empty variables, generates empty objects, and then stores strings and references to other variables, but...
for example,
$this->x1528->x21a9 = "getSingleton";
What is x21a9 ? There's no reference to this anywhere, and I thought the x1528 variable was empty ? Also, is this a way of referencing the $x1528 without the $, because i've never seen this syntax before.
This is using PHP techniques I was not aware of, and this has made me very curious. Any help ?
Without seeing the entire code it's hard to tell. But basically this is just "gibberish" making it hard to read, but basic PHP nevertheless.
What is x21a9 ?
It's just a random property set on the $x1528 class. Like:
$dummyClass = new StdClass(); // Same as $this->x1528 = new \StdClass();
$dummyClass->foo = "bar"; // Same as $this->x1528->x21a9 = "getSingleton";
Now, echo $dummyClass->foo would return bar. It's just setting a property with a value, but with "cryptic" names.
I thought the x1528 variable was empty ?
It starts out empty at the beginning of the class, but then in the constructor, it's immediately set up as an instance of StdClass:
$this->x1528 = new \StdClass();
Also, is this a way of referencing the $x1528 without the $, because i've never seen this syntax before.
This is basic syntax for objects. The object itself has a $ in front of it, but the properties don't.

Yii using a variable with an IN condition

I am trying to pull information into a page using my model. The issue is that I need to use an IN condition on my mysql using a variable.
Here is the code I use currently
$list_id = '1,3';
$clients = ListSubscriber::model()->findAll(array('condition'=>'list_id IN (:list_id)','params'=>array(':list_id'=>$list_id)));
I won't necessarily know how many numbers will be stored within $list_id, hence the need for a variable to work with the IN.
The code does execute without errors, but only seems to return the values for the first number of $list_id, so in this case it only finds users where the list_id = 1.
Any help is appreciated. I have found this question Yii addInCondition
However they are using static values, which does not resolve my issue.
When I do use static values, the code executes with results as expected.
You can use addInCondition :
$list_id = '1,3';
$criteria = new CDbCriteria();
$arr_list_id = explode(",",$list_id);
$criteria->addInCondition("list_id ", $arr_list_id );
$clients = ListSubscriber::model()->findAll($criteria);
$list_ids = array(1,3);
$clients = ListSubscriber::model()->findAllByAttributes(array('list_id'=>$list_ids));

Use keyword and dynamic class names

use my\Project\FooClass;
$obj = new FooClass(); // ok
$name = 'FooClass';
$obj2 = new $name(); // throws an error that the class wasn't found
Well, I believe the title and the example were pretty enough explanation of my question, so just - why does this throws an error, and how should I deal with this?
Sadly, this is not possible due to the way PHP imports/aliases from namespaces. This can be remedied by using literal namespace definitions, though it no doubt sucks.
As follows:
$r = "my\\Project\\FooClass";
$k = new $r();
There is a patch in the works, or at the very least, it was on PHP's bug report a couple of months back. They will hopefully do something about it.
If it bothers you, you can use class_alias() to remedy it, by the way.
try:
$obj2 = new $name;
Remove the parenthesis
Alternatively:
$obj2 = new {$name}();
Can't explain why this doesn't work. But for how to deal with it:
$name = 'FooClass';
$name = "my\\Project\\FooClass\\" . $name; // prepend namespace
$obj2 = new $name();

Objective PHP and key value coding

In some part of my code I need something like this:
$product_type = $product->type;
$price_field = 'field_'.$product_type.'_price';
$price = $product->$$price_field;
In other words I need kind of KVC - means get object field by field name produced at the runtime.
I simply need to extend some existing system and keep field naming convention so do not advice me to change field names instead.
I know something like this works for arrays, when you could easily do that by:
$price = $product[$price_field_key].
So I can produce key for array dynamically.
But how to do that for objects?
Please help me as google gives me river of results for arrays, etc...
Thank you
$price = $product->{$price_field};
Sorry Guys.
It was much easier than I thought.
Hopefullty it will help someone. Simply put:
$price_field = 'field_'.$product_type.'_price';
$price = $product->$price_field;
So you can use varialbe to get object field in Php.
I went to far with those $$ ;-)
Regards
How about using get_object_vars?
$price_field = 'field_'.$product_type.'_price';
$instvars = get_object_vars($product);
$price = $instvars[$price_field];
Actualy it would work as follows.
$product_type = $product->type;
$price_field = "field_".$product_type"._price";
$price = $product->$price_field;

Categories