I have the following code running on my laptop which is running a MAMP server (so apache). On my system it works absolutely fine, however, when I push it to my server it starts throwing "Trying to get property of non-object errors. The databases and migrations are all up to date, and reflect the setup on my laptop. I am wondering why this would be happening on my server and not my laptop. It also seems to happen randomly, which makes me think it's possibly a caching problem?
foreach($posts as $post){
$user = User::where("id","=",$post->posted_by)->first();
$response['posts'][] = [
'id' => $post->id,
'username' => $user->username, //causes problem
'firstname' => $user->name, //causes problem
'lastname' => $user->lastname, // causes problem
'images' => $post->getPostImages(),
'title' => $post->title,
'content' => $post->content,
'price' => $post->price,
'category' => Category::find($post->category_id)->title //causes problem
];
}
This is happening because a category object cant be found
you should change your code to this
foreach($posts as $post){
$user = User::where("id","=",$post->posted_by)->first();
//check if category id is set
if(!$post->category_id)
abort(500,'category_id is not set with Post: #'.$post->id);
//get the category
$category = Category::find($post->category_id);
//check if it found a category
if(!is_object($category)){
abort(500,'could not find Category: #'.$post->category_id);
}
$response['posts'][] = [
'id' => $post->id,
'username' => $user->username, //causes problem
'firstname' => $user->name, //causes problem
'lastname' => $user->lastname, // causes problem
'images' => $post->getPostImages(),
'title' => $post->title,
'content' => $post->content,
'price' => $post->price,
'category' => $category->title //causes problem
];
}
This change should help you find out what is missing :)
Related
i keep getting the error from this code
//insert into orders table
$order = Order::create([
'user_id' => auth()->user()->id,
'date' => Carbon::now(),
'address' => $request->address,
'status' => 0
]);
If you need to see any other code from my files to help me please tell me
The entirety of this code works on other machines with PHP v 7.3 but my production machine is 7.4. Every time i go to print out the order creation data it just comes up blank. Trying to insert anything into the array using short syntax is just not working at all any ideas why. This little chunk is the offender in question and when printing out order creation data I don't get anything.
$order_creation_data = array(
'order' => array(
'email' => $email,
'fulfillment_status' => $shopify_fufillment_status,
'financial_status' => 'paid',
)
);
$shopify_address_build_array = array(
'first_name' => $shopify_customer_default_address['first_name'],
'last_name' => $shopify_customer_default_address['last_name'],
'address1' => $shopify_customer_default_address['address1'],
'address2' => $shopify_customer_default_address['address2'],
'phone' => $shopify_customer_default_address['phone'],
'city' => $shopify_customer_default_address['city'],
'province' => $shopify_customer_default_address['province'],
'country_code' => $shopify_customer_default_address['country_code'],
'zip' => $shopify_customer_default_address['zip'],
);
$order_creation_data['order']['billing_address'] = $shopify_address_build_array;
$order_creation_data['order']['shipping_address'] = $shopify_address_build_array;
I'm new at yii and trying to find my way around. I have done a tutorial or two. I then decided to start editing/changing the example to allow me to learn more. I created a page that does a simple PING. That gets validated. On success, it loads a static page. This all works.
What I wanted to do next is to see how I can utilize a grid to populate that with some data. My real life example is the same. I will get a array of data coming in.
It seems that CArrayDataProvider is what I need. So, I am trying to get a very simple example to work. If I get this to work, I can move on.
I have tried a whole bunch of examples. The error is the same every time. It seems that I do not have CArrayDataProvider installed? If that is even possible.
I did a standard basic install:
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
I have the following at the beginning of my controllers file:
use yii2\data\ArrayDataProvider;
I get no error here.
I searched for the file itself on the file system, could not find it. I did find ArrayDataProvider, so I tried that, same result:
use vendor\yiisoft\yii2\data\ArrayDataProvider;
The error is:
PHP Fatal Error – yii\base\ErrorException
Class 'CArrayDataProvider' not found
This is on line 24:
"dataProvider = new CArrayDataProvider($fruits);"
Here is my example code. Not that I think the issue is in here, but to show what I am trying to do:
$fruits = array(
array('id' => 1, 'name'=>'apple', 'color' => 'green'),
array('id' => 2, 'name'=>'orange', 'color' => 'orange'),
array('id' => 3, 'name'=>'banana', 'color' => 'yellow'),
array('id' => 4, 'name'=>'pineapple', 'color' => 'brown')
);
$dataProvider = new CArrayDataProvider($fruits);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'fruits-grid',
'dataProvider' => $dataProvider ,
'columns' => array(
array(
'name' => 'ID',
'value' => '$data["id"]',
),
array(
'name' => 'Name',
'value' => '$data["name"]'
),
array(
'name' => 'Color',
'value' => '$data["color"]'
),
)
));
On the file-system (linux) itself I did a update:
composer update
I have been Googling for the last 2 days and I am finding nothing.
I tried adding a date picket. That worked. I used:
https://www.tutorialspoint.com/yii/yii_extensions.htm
So in short, the static page that I call, now displays a DateTimePicker.
At the start of the file I added:
use kartik\datetime\DateTimePicker;
And in the body:
<?php
echo DateTimePicker::widget([
'name' => 'dp_1',
'type' => DateTimePicker::TYPE_INPUT,
'value' => '23-Feb-1982 10:10',
'pluginOptions' => [
'autoclose'=>true,
'format' => 'dd-M-yyyy hh:ii'
]
]);
?>
How do I get yii2 to allow me to use ArrayDataProvider. Or how do I install the extension? Or who do I reference it?
In Yii2 there's not a CArrayDataProvider. Use ArrayDataProvider, like described in docs:
$provider = new yii\data\ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
Pretty well documented here.
Yii2 supports ArrayDataProvider and yii 1.* supports CArrayDataProvider, so as per your code you are using Yii2, so just replace the below line with
$dataProvider = new CArrayDataProvider($fruits);
With this:
$dataProvider = new ArrayDataProvider($fruits);
Thx to everyone's help! I now have a working example.
In case anyone else has the same issues, I will post my working code here:
use kartik\grid\GridView;
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"risus#consequatdolorvitae.org"),
array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis#at.ca"),
array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et#arcuacorci.ca"),
array("id"=>4,"name"=>"Fulton","email"=>"a#faucibusorciluctus.edu"),
array("id"=>5,"name"=>"Neville","email"=>"eleifend#consequatlectus.com"),
array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo#miAliquam.com"),
array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien#dapibusquam.org"),
array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget#egestas.edu"),
array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing#interdumligulaeu.com"),
array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus#commodohendrerit.co.uk"),
];
$dataProvider = new \yii\data\ArrayDataProvider([
'key'=>'id',
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'value' => 'email',
]
]
]);
I saw the documentation but I still not finding the error on my code.
I am trying to add a product from excel file to Magento System, and then update it on each Store View (multi language shop).
For that, I've done the following PHP script. On this code, I'm reading only one Excel row (just to show you how it works) and then i am adding it to Magento (actually that's working) and then I am trying to Update stuff (gives the error). Be aware that $col[9] is the var that saves the SKU.
Note: I am using SOAP, but not V2.
$rowdata=$sheet->rangeToArray('A' . $row.':'.$maxcol . $row, NULL, TRUE, FALSE);
$col=$rowdata[0];
//$soap is the client. $session_id is the logged in SOAP session.
$attributeSets = $soap->call($session_id, 'product_attribute_set.list');
$attributeSet = current($attributeSets);
$result = $soap->call($session_id, 'catalog_product.create', array('simple', $attributeSet['set_id'], $col[9], array(
'categories' => array(2),
'websites' => array(1),
'name' => $col[1],
'description' => $col[10],
'short_description' => $col[13],
'weight' => '10',
'status' => '1',
'url_key' => 'product-url-key',
'url_path' => 'product-url-path',
'visibility' => '4',
'price' => $col[20],
'tax_class_id' => 1,
'meta_title' => 'Product meta title',
'meta_keyword' => 'Product meta keyword',
'meta_description' => 'Product meta description'
)));
var_dump ($result);
$updatearray= array(
'name' => $col[2],
'description' => $col[11],
'short_description' => $col[14]
);
$update = $soap->call($session_id, 'catalog_product.update', array($col[9], $updatearray, 'fr'));
var_dump ($update);
I would appreciate any help you guys can give!
I just found the fix for this problem but it really makes no sense.
So, when we are sending the SKU by call, we need to send with an extra space. It means the $update needs to be like this:
$soap->call($session_id, 'catalog_product.update', array($col[9].' ', $updatearray, 'fr'));
Last weekend I was trying to troubleshoot a bug on a website where the Session was not being preserved in IE - today I went to do further work on the site on my laptop, and I could no longer log in -invariably I have done something incredibly stupid.
I'm using xampp on a Windows laptop, and working on localhost, and this occurs in all browsers. I am not very experienced with troubleshooting these kinds of problems - I have been able to ascertain the following:
The user is able to login (Auth->login() successfully logs the user in), the issue is the Session is gone when they are redirected
I can see the Sessions being written in my /tmp/ dir containing (what looks to be) the correct data
I can create my own stupid cookies and their values persist
No other cookies exist for the site
So, it would appear to me that the Session cookie is not being set, but I have run out of ideas as to why this might be occurring. I haven't changed any cookie related browser settings (outside of enabling cookies in IE), and I have double checked my Chrome cookie settings. I have also, as I mentioned, written some junk cookies in AppController, and I can see them created, and their data persists.
If I call $_SESSION after login(), everything looks great, but if I print $_SESSION before logging in, it's empty.
I am quite sure I have managed to do something retarded, but I have run out of ideas as to what it might be. I have restored my /app/core.php to be the Cake defaults:
Configure::write('Session', array(
'defaults' => 'php'
));
My login() function looks essentially as follows:
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again.'));
}
Auth settings in AppController:
class AppController extends Controller {
public $components = array(
'Session',
'Cookie',
'Acl',
'Email',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'password')
)),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
),
);
And example output from printing $this->Auth->user(), $_SESSION before the redirect in login():
\app\Controller\UsersController.php (line 203)
array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
\app\Controller\UsersController.php (line 204)
array(
'Config' => array(
'userAgent' => '4af162a3a94462226b6e93c6806203aa',
'time' => (int) 1417317929,
'countdown' => (int) 10,
'language' => 'eng'
),
'Auth' => array(
'User' => array(
'id' => '10',
'name' => 'super',
'is_active' => '1',
'email' => 'super#test.com',
'group_id' => '3',
'address' => '3',
'phone' => 'xxxx',
'category' => 'P',
'communication_in' => 'E',
'created' => '2014-11-29 16:27:19',
'modified' => '2014-11-29 16:27:19',
'Group' => array(
'id' => '3',
'name' => 'Administrators',
'created' => '2014-11-16 21:01:35',
'modified' => '2014-11-16 21:01:35'
)
)
)
)
Last created session file:
Config|a:4:{s:9:"userAgent";s:32:"4af162a3a94462226b6e93c6806203aa";s:4:"time";i:1417317929;s:9:"countdown";i:10;s:8:"language";s:3:"eng";}Auth|a:1:{s:4:"User";a:12:{s:2:"id";s:2:"10";s:4:"name";s:5:"super";s:9:"is_active";s:1:"1";s:5:"email";s:14:"super#test.com";s:8:"group_id";s:1:"3";s:7:"address";s:1:"3";s:5:"phone";s:10:"xxxxx";s:8:"category";s:1:"P";s:16:"communication_in";s:1:"E";s:7:"created";s:19:"2014-11-29 16:27:19";s:8:"modified";s:19:"2014-11-29 16:27:19";s:5:"Group";a:4:{s:2:"id";s:1:"3";s:4:"name";s:14:"Administrators";s:7:"created";s:19:"2014-11-16 21:01:35";s:8:"modified";s:19:"2014-11-16 21:01:35";}}}
Facepalm of the day:
Many hours later, I finally thought to check phpinfo(), and of course, the session.cookie-domain was set to the remote site. I suppose at some point last week I edited the wrong PHP ini file.