Object has "Undefined Properties" that are actually defined - laravel

I'm using the package: https://packagist.org/packages/gavroche/ups-api
I'm getting some odd behaviors from Laravels Exception Handler when I run this:
$tracking = new \UPS\Tracking(
'KEY', 'USERNAME', 'PASS'
);
$shipment = $tracking->track('TRACKING NUMBER HERE');
foreach($shipment->Package->Activity as $activity) {
var_dump($activity->ActivityLocation->Address->City);
}
The interesting thing is the cities are var_dumped however laravels exception handler, in the same breath, says Undefined property: stdClass::$City.
Here is a vardump of one of the $activity objects:
object(stdClass)[518]
public 'ActivityLocation' =>
object(stdClass)[519]
public 'Address' =>
object(stdClass)[520]
public 'City' => string 'CLEVELAND' (length=9)
public 'StateProvinceCode' => string 'TN' (length=2)
public 'PostalCode' => string '37311' (length=5)
public 'CountryCode' => string 'US' (length=2)
public 'Code' => string 'M4' (length=2)
public 'Description' => string 'RECEPTION' (length=9)
public 'SignedForByName' => string 'HICKMAN' (length=7)
public 'Status' =>
object(stdClass)[521]
public 'StatusType' =>
object(stdClass)[522]
public 'Code' => string 'D' (length=1)
public 'Description' => string 'DELIVERED' (length=9)
public 'StatusCode' =>
object(stdClass)[523]
public 'Code' => string 'KB' (length=2)
public 'Date' => string '20130426' (length=8)
public 'Time' => string '085500' (length=6)
Any property in the $activity->ActivityLocation->Address object laravel sees issue with. However, I can request $activity->Status->StatusType->Description and there are no problems.
A regular for loop fixes the issue. Seems to be some issue in the incrementing of php's foreach().
for ($i=0; $i < (count($shipment->Package->Activity) - 1); $i++) {
var_dump($shipment->Package->Activity[$i]->ActivityLocation->Address->City);
}

You were not clear on this, but are you trying to access this as
$activity->ActivityLocation->Address->City ?
You left the $activity part off your question and used $City and not City, and I wasnt sure if this was intentional or not

Related

Yii2, elasticsearch. How to assign my _id as primaryKey

i'm newbie so I hope you can help me.
I have yii2 and i need to implement elasticSearch(ES).
All set up.
I made standart model and standart CRUD. Just a few changes to implement ES like
use yii\elasticsearch\ActiveRecord;
But I have a problem with getting _id in my view/index.php
to get _id in view.php i made
$model->_id = $model->primaryKey;
But how I can fix it with DataProvider in index.php?
my attributes
public function attributes()
{
return [
'_id',
'first_name',
'last_name',
'email',
'created',
'brand',
'product',
'qty',
'sum'
];
}
My rules
public function rules()
{
return [
[['first_name', 'last_name', 'email', 'created',
'brand', 'product', 'qty', 'sum'
],
'safe'],
[['qty'], 'integer'],
[['sum'], 'double'],
['email', 'email'],
];
}
I think it should work like with MongoDB, but it doesn't.
if I want to delete or view or update the data being on index.php I can se only such links w/o id
http://yii2.dev/index.php?r=elastic%2Fdelete&id=
Pls help
P.S.
Here is a var_dump
array (size=5)
0 =>
object(frontend\models\Elastic)[67]
private '_id' (yii\elasticsearch\ActiveRecord) => string 'AVreA6SUvBgWLKxTzUZn' (length=20)
private '_score' (yii\elasticsearch\ActiveRecord) => float 1
private '_version' (yii\elasticsearch\ActiveRecord) => null
private '_highlight' (yii\elasticsearch\ActiveRecord) => null
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=8)
'created' => string '2017-03-12' (length=10)
'first_name' => string 'Luka' (length=4)
'last_name' => string 'Signori' (length=7)
'email' => string 'l.signori#gmail.com' (length=19)
'brand' => string 'Apple' (length=5)
'product' => string 'iPad' (length=4)
'qty' => string '35' (length=2)
'sum' => string '600.00' (length=6)
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=8)
'created' => string '2017-03-12' (length=10)
'first_name' => string 'Luka' (length=4)
'last_name' => string 'Signori' (length=7)
'email' => string 'l.signori#gmail.com' (length=19)
'brand' => string 'Apple' (length=5)
'product' => string 'iPad' (length=4)
'qty' => string '35' (length=2)
'sum' => string '600.00' (length=6)
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
you can set.good luck
$dataProvider = new ActiveDataProvider([
'query' => $query,
'key' => ['your primaryKey']
'sort' => [
'defaultOrder' => ['datetime'=>SORT_DESC]
],
]);

Cannot access data passed in redirect

so i am using laravel 4.2. my problem is I cannot echo the data from my redirect route. how can i access it the right way? I tried searching to get the right answer but it didn't help me.
public function postLogin()
{
$timeIn = date('Y-m-d G:i:s');
$userLog = New UserLog;
$userLog->username = Input::get('username');
$userLog->time_in = $timeIn;
$userLog->save();
$users = $userLog;
return Redirect::route( 'account' )
->with( 'users', $users );
}
public function account(){
$data = Session::get('users');
var_dump($data );
echo $data->username;// this one throws an error
}
I can see that it has values in var_dump($data).
object(__PHP_Incomplete_Class)[92]
public '__PHP_Incomplete_Class_Name' => string 'UserLog' (length=7)
public 'timestamps' => boolean false
public 'table' => string 'userlogs' (length=8)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
protected 'attributes' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 15:11:43' (length=19)
'id' => int 240
protected 'original' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 15:11:43' (length=19)
'id' => int 240
these are my routes file
Route::get('/login', array(
'uses' => 'TimeController#login',
'as' => 'login')
);
Route::post('/postLogin', array(
'uses' => 'SessionsController#postLogin',
'as' => 'postLogin')
);
Route::get('/account', array(
'uses' => 'SessionsController#account',
'as' => 'account')
);
I felt like i made a huge success just by displaying the data, and the problem causing me to get errors is the __PHP_Incomplete_Class as you can see on my var_dump($data) above. To access the $data correctly, i need to unserialize it first before accessing the regular way. here i just added one line of code and everything works very well. thanks to this post unserializing.
public function account(){
$data = Session::get('users');
$data = unserialize(serialize($data)); //added code to unserialize the __PHP_Incomplete_Class
var_dump($data);
echo $data->username;
}
in my var_dump($data) the __PHP_Incomplete_Class is now gone
object(UserLog)[143]
public 'timestamps' => boolean false
protected 'table' => string 'userlogs' (length=8)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
protected 'attributes' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 19:57:08' (length=19)
'id' => int 276
protected 'original' =>
array (size=3)
'username' => string 'user' (length=4)
'time_in' => string '2015-07-28 19:57:08' (length=19)
'id' => int 276

Eloquent query correctly eager loads relation, but when I try to dump the related object I get only the ID

Hey guys I'm trying to grab a relation from the Friend model (pivot for user friends). When I try to dump the relation of the Friend object I should be getting a User model, instead it dumps only the ID of the User.
Here is the Friend Model:
class Friend extends Eloquent
{
public function requester()
{
return $this->belongsTo('Acme\Users\User', 'requester');
}
}
Here is friend repo:
public function getFriendRequests($id, $onlyAccepted = false)
{
return Friend::where('acceptor', $id)->with('requester')->get();
}
Here is FriendsController:
$friendAccepts = $this->friendRepo->getFriendAccepts(Auth::id(), true);
dd($friendAccepts[0]->acceptor);
It should be dumping a User object but I'm only getting the ID of the user when I do:
int 101
Here is output when I dd($friendAccepts[0]);
(It correctly shows the relation eager loaded as a User object
object(Acme\Friends\Friend)[513]
protected 'fillable' =>
array (size=5)
0 => string 'requester' (length=9)
1 => string 'acceptor' (length=8)
2 => string 'status' (length=6)
3 => string 'created_at' (length=10)
4 => string 'updated_at' (length=10)
protected 'table' => string 'friends' (length=7)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=7)
'id' => int 1
'requester' => int 101
'acceptor' => int 1
'status' => int 2
'created_at' => string '2014-05-02 22:53:35' (length=19)
'updated_at' => string '2014-07-31 12:56:53' (length=19)
'deleted_at' => null
protected 'original' =>
array (size=7)
'id' => int 1
'requester' => int 101
'acceptor' => int 1
'status' => int 2
'created_at' => string '2014-05-02 22:53:35' (length=19)
'updated_at' => string '2014-07-31 12:56:53' (length=19)
'deleted_at' => null
protected 'relations' =>
array (size=1)
'acceptor' =>
object(Acme\Users\User)[318]
protected 'fillable' =>
array (size=10)
...
protected 'table' => string 'users' (length=5)
protected 'hidden' =>
array (size=2)
...
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=16)
...
protected 'original' =>
array (size=16)
...
protected 'relations' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'appends' =>
array (size=0)
...
protected 'guarded' =>
array (size=1)
...
protected 'dates' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'with' =>
array (size=0)
...
protected 'morphClass' => null
public 'exists' => boolean true
protected 'pendingEvents' =>
array (size=0)
...
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean true
protected 'pendingEvents' =>
array (size=0)
empty
UPDATE:
When I use toArray() and access acceptor as a keyed array element it dumps the user object's properties.
dd(Friend::where('requester', Auth::id())->with('acceptor')->first()->toArray()['acceptor'])
Output:
array (size=7)
'id' => int 1
'requester' => int 101
'acceptor' =>
array (size=14)
'id' => int 1
'email' => string 'nader.verla#johnston.com435' (length=27)
'fname' => string 'April' (length=5)
'lname' => string 'Macejkovic' (length=10)
'username' => string 'AprilMacejkovic40466' (length=20)
'birthday' => string '2006-12-02' (length=10)
'gender' => string 'Male' (length=4)
'location' => string 'Port Noelside' (length=13)
'website' => string 'pagacvandervort.net' (length=19)
'bio' => string 'Ut ut quia vitae vero. Sit eligendi voluptate quia. Voluptas ea accusamus fuga officiis sunt. Sed est fugiat et et voluptas. Distinctio vero error aliquid. Ipsam et tempora asperiores temporibus in autem.' (length=204)
'profile_picture' => string '' (length=0)
'created_at' => string '2013-10-18 09:49:31' (length=19)
'updated_at' => string '2014-03-05 04:17:24' (length=19)
'deleted_at' => null
'status' => int 2
'created_at' => string '2014-05-02 22:53:35' (length=19)
'updated_at' => string '2014-07-31 12:56:53' (length=19)
'deleted_at' => null
You may not want to have relationship methods named the same as table "attributes"(fields) if you plan on using dynamic properties to access those methods.
There is already a mechanism in place to resolve the dynamic properties and inaccesible properties using __get($key) which calls getAttribute($key). The first thing it checks for is if the key exists in the $attributes of the model instance and returns it if it does. This will stop it from continuing to check if there is a relation loaded with that name or if there is a method to call with a camel case version of that key.
"This allows every attribute to dynamically accessed through the _get method without accessors."
Ref
Eloquent Model API
Eloquent Model #__get
Eloquent Model #getAttribute

One to Many in Laravel

I have a model in which a Firm has a status. This means there are two tables:
Firm
FirmStatus
I have this defined in my project like this:
class FirmStatus extends Eloquent {
protected $table = 'firm_statuses';
protected $primaryKey = 'firm_status_id';
protected $fillable = array('firm_status_id', 'name');
}
class Firm extends Eloquent {
protected $table = 'firms';
protected $primaryKey = 'firm_id';
protected $fillable = array('name', 'firm_status_id');
public function users()
{
return $this->hasMany('User', 'firm_id', 'firm_id');
}
public function firm_status()
{
return $this->belongsTo('FirmStatus', 'firm_status_id');
}
}
But I can't seem to get this to work, no matter what way I try to access the FirmStatus on the Firm it just doesn't work?
In my controller I have this code:
$firm = Firm::find($id);
$firmClassifications = DB::table('firm_classifications')->orderBy('name', 'asc')->lists('name','firm_classification_id');
return var_dump($firm);
which returns this:
object(Firm)[243]
protected 'table' => string 'firms' (length=5)
protected 'primaryKey' => string 'firm_id' (length=7)
protected 'fillable' =>
array (size=5)
0 => string 'name' (length=4)
1 => string 'firm_classification_id' (length=22)
2 => string 'fca_number' (length=10)
3 => string 'question_number_passed_on' (length=25)
4 => string 'firm_status_id' (length=14)
protected 'connection' => null
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=7)
'firm_id' => int 8
'name' => string 'siofds4' (length=7)
'fca_number' => string '' (length=0)
'firm_classification_id' => int 1
'created_at' => string '2014-09-08 11:06:17' (length=19)
'updated_at' => string '2014-09-08 12:24:23' (length=19)
'firm_status_id' => int 2
protected 'original' =>
array (size=7)
'firm_id' => int 8
'name' => string 'siofds4' (length=7)
'fca_number' => string '' (length=0)
'firm_classification_id' => int 1
'created_at' => string '2014-09-08 11:06:17' (length=19)
'updated_at' => string '2014-09-08 12:24:23' (length=19)
'firm_status_id' => int 2
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean true
I am expecting that $firm->firm_status (method call or not, whichever way will work) will allow me to access the specified firm status, but $firm->firm_status results in null.
First off, you won't see anything in var_dump($firm) result unless you load the relation.
Next thing - Eloquent requires you to use camelCased methods for relations, if you want to work with dynamic methods (call $model->relation).
So you need:
public function firmStatus()
{
return $this->belongsTo('FirmStatus', 'firm_status_id');
}
then you can access it:
$firm->firmStatus; // FirmStatus model | null
$firm->firm_status; // this will be translated to firmStatus anyway, but not other way around
and to view it in your call:
$firm = Firm::with('firmStatus')->first();
var_dump($firm);
// but better use toArray(), since it's easier to read
dd($firm->toArray());
If a firm can have a single status, use hasOne() instead of belongsTo():
class Firm extends Eloquent {
public function firm_status()
{
return $this->hasOne('FirmStatus', 'firm_status_id', 'firm_status_id');
}
}
If you need to query all of the firms with a given status, add the inverse relationship to FirmStatus:
class FirmStatus extends Eloquent {
public function firm()
{
return $this->hasMany('Firm', 'firm_status_id', 'firm_status_id');
}
}

Get number of articles from a specific content category using Joomla functions

I'm struggling to get the number of articles from a specific category from my custom module? Would anyone mind pointing me in the correct direction as to what function I should use?
I've been using the following:
$model = JModelLegacy::getInstance('Articles', 'ContentModel');
$model->setState('filter_fields.catid', 16);
$articles = $model->getItems();
But I only get the following returned:
object(ContentModelArticles)[134]
protected 'cache' =>
array
empty
protected 'context' => string 'com_content.articles' (length=20)
protected 'filter_fields' =>
array
0 => string 'id' (length=2)
1 => string 'a.id' (length=4)
2 => string 'title' (length=5)
3 => string 'a.title' (length=7)
4 => string 'alias' (length=5)
5 => string 'a.alias' (length=7)
6 => string 'checked_out' (length=11)
7 => string 'a.checked_out' (length=13)
8 => string 'checked_out_time' (length=16)
9 => string 'a.checked_out_time' (length=18)
10 => string 'catid' (length=5)
11 => string 'a.catid' (length=7)
12 => string 'category_title' (length=14)
13 => string 'state' (length=5)
14 => string 'a.state' (length=7)
15 => string 'access' (length=6)
16 => string 'a.access' (length=8)
17 => string 'access_level' (length=12)
18 => string 'created' (length=7)
19 => string 'a.created' (length=9)
20 => string 'created_by' (length=10)
21 => string 'a.created_by' (length=12)
22 => string 'ordering' (length=8)
23 => string 'a.ordering' (length=10)
24 => string 'featured' (length=8)
25 => string 'a.featured' (length=10)
26 => string 'language' (length=8)
27 => string 'a.language' (length=10)
28 => string 'hits' (length=4)
29 => string 'a.hits' (length=6)
30 => string 'publish_up' (length=10)
31 => string 'a.publish_up' (length=12)
32 => string 'publish_down' (length=12)
33 => string 'a.publish_down' (length=14)
34 => string 'images' (length=6)
35 => string 'a.images' (length=8)
36 => string 'urls' (length=4)
37 => string 'a.urls' (length=6)
protected 'query' =>
array
empty
protected '__state_set' => null
protected '_db' =>
object(JDatabaseMySQLi)[15]
public 'name' => string 'mysqli' (length=6)
protected 'nameQuote' => string '`' (length=1)
protected 'nullDate' => string '0000-00-00 00:00:00' (length=19)
protected 'dbMinimum' => string '5.0.4' (length=5)
private '_database' (JDatabase) => string 'xxxx' (length=25)
protected 'connection' =>
object(mysqli)[16]
public 'affected_rows' => null
public 'client_info' => null
public 'client_version' => null
public 'connect_errno' => null
public 'connect_error' => null
public 'errno' => null
public 'error' => null
public 'field_count' => null
public 'host_info' => null
public 'info' => null
public 'insert_id' => null
public 'server_info' => null
public 'server_version' => null
public 'sqlstate' => null
public 'protocol_version' => null
public 'thread_id' => null
public 'warning_count' => null
protected 'count' => int 0
protected 'cursor' =>
object(mysqli_result)[202]
public 'current_field' => null
public 'field_count' => null
public 'lengths' => null
public 'num_rows' => null
public 'type' => null
protected 'debug' => boolean false
protected 'limit' => int 0
protected 'log' =>
array
empty
protected 'offset' => int 0
protected 'sql' =>
object(JDatabaseQueryMySQLi)[258]
protected 'db' =>
&object(JDatabaseMySQLi)[15]
protected 'type' => string 'select' (length=6)
protected 'element' => null
protected 'select' =>
object(JDatabaseQueryElement)[257]
protected 'name' => string 'SELECT' (length=6)
protected 'elements' =>
array
0 => string 'm.id, m.title, m.module, m.position, m.content, m.showtitle, m.params, mm.menuid' (length=80)
protected 'glue' => string ',' (length=1)
protected 'delete' => null
protected 'update' => null
protected 'insert' => null
protected 'from' =>
object(JDatabaseQueryElement)[259]
protected 'name' => string 'FROM' (length=4)
protected 'elements' =>
array
0 => string '#__modules AS m' (length=15)
protected 'glue' => string ',' (length=1)
protected 'join' =>
array
0 =>
object(JDatabaseQueryElement)[260]
protected 'name' => string 'LEFT JOIN' (length=9)
protected 'elements' =>
array
0 => string '#__modules_menu AS mm ON mm.moduleid = m.id' (length=43)
protected 'glue' => string ',' (length=1)
1 =>
object(JDatabaseQueryElement)[262]
protected 'name' => string 'LEFT JOIN' (length=9)
protected 'elements' =>
array
0 => string '#__extensions AS e ON e.element = m.module AND e.client_id = m.client_id' (length=72)
protected 'glue' => string ',' (length=1)
protected 'set' => null
protected 'where' =>
object(JDatabaseQueryElement)[261]
protected 'name' => string 'WHERE' (length=5)
protected 'elements' =>
array
0 => string 'm.published = 1' (length=15)
1 => string 'e.enabled = 1' (length=13)
2 => string '(m.publish_up = '0000-00-00 00:00:00' OR m.publish_up <= '2013-10-07 14:10:50')' (length=79)
3 => string '(m.publish_down = '0000-00-00 00:00:00' OR m.publish_down >= '2013-10-07 14:10:50')' (length=83)
4 => string 'm.access IN (1,1)' (length=17)
5 => string 'm.client_id = 0' (length=15)
6 => string '(mm.menuid = 113 OR mm.menuid <= 0)' (length=35)
protected 'glue' => string ' AND ' (length=5)
protected 'group' => null
protected 'having' => null
protected 'columns' => null
protected 'values' => null
protected 'order' =>
object(JDatabaseQueryElement)[264]
protected 'name' => string 'ORDER BY' (length=8)
protected 'elements' =>
array
0 => string 'm.position, m.ordering' (length=22)
protected 'glue' => string ',' (length=1)
protected 'union' => null
protected 'autoIncrementField' => null
protected 'tablePrefix' => string 'joomla_' (length=7)
protected 'utf' => boolean true
protected 'errorNum' => int 0
protected 'errorMsg' => string '' (length=0)
protected 'hasQuoted' => boolean false
protected 'quoted' =>
array
empty
protected 'name' => string 'articles' (length=8)
protected 'option' => string 'com_content' (length=11)
protected 'state' =>
object(JObject)[279]
protected '_errors' =>
array
empty
public 'filter_fields.catid' => int 16
protected 'event_clean_cache' => string 'onContentCleanCache' (length=19)
protected '_errors' =>
array
empty
I changed my code to the following:
$model = JModelLegacy::getInstance('Articles', 'ContentModel');
$model->setState('filter.category_id', 16);
$articles = $model->getItems();
$num_articles = count($articles);

Resources