Updating record from PHP Client library for Exact Online - exact-online

I had used PHP Client library for Exact Online.
I need to store the record, based on the condition if it exists or not. Since records are saving successfully. But unfortunately records are not updating.
$customer = [
'address' => 'No.22/N, 91 Cross, XYZ Street, ABC Road',
'address2' => 'DEF',
'city' => 'GHI',
'customerid' => '999',
'country' => 'DE',
'name' => 'Nishanth',
'zipcode' => '123456'
];
// Create a new account
$account->AddressLine1 = $customer['address'];
$account->AddressLine2 = $customer['address2'];
$account->City = $customer['city'];
$account->Code = $customer['customerid'];
$account->Country = $customer['country'];
$account->IsSales = 'true';
$account->Name = $customer['name'];
$account->Postcode = $customer['zipcode'];
$account->Email = 'nishanth#gmail.com';
$account->Status = 'C';
From the above piece of code, based on the condition the record needs to be updated or saved from the below coding snippets. Followed two approaches:
I Approach:
if($AccInfo)
{ // Update
$AccInfo->AddressLine1 = $customer['address'];
$AccInfo->AddressLine2 = $customer['address2'];
$AccInfo->City = $customer['city'];
$updateAcc = $AccInfo->update();
}
else
{ // Save
$savedAcc = $Accounts->save();
}
Result:
Warning: Attempt to assign property 'AddressLine1' of non-object in E:\xampp\htdocs\exact-php-client-master\example\example.php on line 506
Warning: Attempt to assign property 'AddressLine2' of non-object in E:\xampp\htdocs\exact-php-client-master\example\example.php on line 507
Warning: Attempt to assign property 'City' of non-object in E:\xampp\htdocs\exact-php-client-master\example\example.php on line 508
Fatal error: Uncaught Error: Call to a member function update() on array in E:\xampp\htdocs\exact-php-client-master\example\example.php:510 Stack trace: #0 {main} thrown in E:\..\..\exact-php-client-master\example\example.php on line 510
II Approach:
if($AccInfo)
{ // Update
$updateAcc = $Accounts->update();
}
else
{ // Save
$savedAcc = $Accounts->save();
}
Result:
Picqer\Financials\Exact\ApiException : Error 400: Bad Request - Error in query syntax.
How should we need to update the records to Exact Online Dashboard?

Finally I solved the issue by writing custom methods
$AccInfo = $Accounts->filter("Email eq 'nishanthjay#gmail.com'");
if($AccInfo)
{ // Update
$updateAcc = $Accounts->customUpdate($AccInfo[0]->ID);
echo '<pre>'; print_r($updateAcc);
}
else
{ // Save
$savedAcc = $Accounts->save();
}
I had written my own methods from ..\src\Picqer\Financials\Exact\Persistance\Storable.php
public function customUpdate($primaryKey='')
{
$this->fill($this->update2($primaryKey));
return $this;
}
public function update2($primaryKey='')
{
return $this->connection()->put($this->url() . "(guid'$primaryKey')", $this->json());
}
For any one who knows exactly how to update to an ExactOnline. You are always welcome to answer to the posted question via built-In function call known as update().

Related

Trying to get property 'name' of non-object when trying to click next page using pagination

I get an error saying "Trying to get property 'name' of non-object" when I tried to click on next page using pagination.
Here is my Controller:
$websites = Website::all();
$menu = Menu::with('categories')->whereHas('categories', function ($query) {
$query->where('slug', request()->category);
})->paginate(8);
$categories = Category::all();
$categoryName = $categories->where('slug', request()->category)->first()->name;
return view('shopmenu')->with([
'menu' => $menu,
'categories' => $categories,
'categoryName' => $categoryName,
'websites' => $websites,
]);
The error is in this code:
$categoryName = $categories->where('slug', request()->category)->first()->name;
How do I solve this?
You need to put check if you are getting any record then try to access name.
$categoryName = $categories->where('slug', request()->category)->first();
if ($categoryName) {
$categoryName = $categoryName->name;
}
Basically due to chaining when you get null in this query $categories->where('slug', request()->category)->first() and you try to access name property on null you get this error.

How do I return a union as an Active Record object?

I have these union in my controller :
$expression = new Expression('"News"');
$featuredNews2= news::find()
->alias('ne')
->select(['ne.title', 'ne.content','ne.featuredOrder', 'category'=>$expression])
->innerJoinWith('featuredOrder1');
$expression2 = new Expression('"Event"');
$featuredEvents2 = event::find()
->select(['ev.title', 'ev.content','ev.featuredOrder','category'=>$expression2])
->from('event ev')
->innerJoinWith('featuredOrder2');
$union = $featuredNews2->union($featuredEvents2);
The relation in model :
news model
public function getFeaturedOrder1()
{
return $this->hasOne(Featured::className(), ['featuredOrder' => 'featuredOrder']);
}
event model
public function getFeaturedOrder2()
{
return $this->hasOne(Featured::className(), ['featuredOrder' => 'featuredOrder']);
}
I need to return the query as an Active Query because I need to access my model's method e.g : $model->featuredOrder1->preview in my view.
The following works but it returns an array, as the result I can't access my model's method :
$unionQuery = (new \yii\db\Query)->select('*')
->from($union)
->orderBy('featuredOrder')->all(\Yii::$app->db2);
I have two questions :
How to return the equivalent $unionQuery above but as an active query object? I have googled and search on SO but what I found is how to return it as array.
This is out of curiosity, I wonder why I should provide my db connection as argument in my $unionQuery all() method above. If I didn't use an argument that point to db2, it will look for table name inside my db database instead ( db is my parent database, this db2 is my module's database/the correct one). This only happen with a union. My news and event model already have this in getdb() function:
return Yii::$app->get('db2');
update
I've tried this too :
$unionProvider = (new ActiveQuery(Featured::className()))->select('*')
->from(['union' => $featuredEvents2->union($featuredNews2)])
->orderBy('featuredOrder');
With this relation in featured model:
public function getNews()
{
return $this->hasOne(News::className(), ['featuredOrder' => 'featuredOrder']);
}
public function getEvents()
{
return $this->hasOne(Event::className(), ['featuredOrder' => 'featuredOrder']);
}
and in the view, I tried this :
foreach($unionProvider as $key=>$model){
echo $model->news->title;
}
but get this error : Trying to get property of non-object
Update 2
I forgot to add ->all() in my $unionProvider, but after that I got this error instead : PHP Fatal Error – yii\base\ErrorException
Allowed memory size of 134217728 bytes exhausted (tried to allocate 12288 bytes)
Might be something wrong with my query? Can't figure it out
You can try using pure SQL. Plus you can test to see if it is returning the correct results and then add it in the statement below.
$customers = Customer::findBySql('SELECT * FROM customer')->all();
Learn more in yii docs

Retrieve specific data using JSON decode Laravel

I'm new to Laravel. I need to retrieve specific data from the database using the JSON decode. I am currently using $casts to my model to handle the JSON encode and decode.
This is my insert query with json encode:
$request->validate([
'subject' => 'required|max:255',
'concern' => 'required'
]);
$issue = new Issue;
$issue->subject = $request->subject;
$issue->url = $request->url;
$issue->details = $request->concern;
$issue->created_by = $request->userid;
$issue->user_data = $request->user_data; //field that use json encode
$issue->status = 2; // 1 means draft
$issue->email = $request->email;
$issue->data = '';
$issue->save();
The user_data contains {"id":37,"first_name":"Brian","middle_name":"","last_name":"Belen","email":"arcega52#gmail.com","username":"BLB-Student1","avatar":"avatars\/20170623133042-49.png"}
This is my output:
{{$issue->user_data}}
What I need to retrieve is only the first_name, middle_name, and last_name. How am I supposed to achieve that? Thank you in ADVANCE!!!!!
As per the above code shown by you it will only insert data into the database.For retrieving data you can make use of Query Builder as i have written below and also you can check the docs
$users = DB::table('name of table')->select('first_name', 'middle_name', 'last_name')->get();
I will recommend using Resources. It really very helpful laravel feature. Check it out. It is a reusable class. You call anywhere and anytime.
php artisan make:resource UserResource
Go to your the newly created class App/Http/Resources/UserResource.php and drfine the column you want to have in your response.
public function toArray($request) {
return [
"first_name" => $this->first_name,
"middle_name" => $this->middle_name,
"last_name" => $this->last_name
]
}
Now is your controller you can use the UserResource like folow:
public function index()
{
return UserResource::collection(User::all());
}
Or after inserting data you can return the newly added data(f_name, l_name...)
$user = new User;
$user->first_name= $request->first_name;
$user->middle_name= $request->middle_name;
$user->last_name= $request->last_name;
$user->save();
$user_data= new UserResource($user);
return $user_data;

mysql_real_escape_string(): Access denied in DB insert

I am trying to use a legacy MediaWiki extension on PHP 5.6 and later versions, and it fails when it comes to DB inserts.
And yes, this is not a duplicate, as the code is different.
The full error was:
Warning: mysql_real_escape_string(): Access denied for user
''#'localhost' (using password: NO)
I tried changing to mysqli_real_escape_string but then I had:
mysqli_real_escape_string() expects exactly 2 parameters, 1 given on
line 235
Here is the function:
function Lookup_addLookup ($url, $name, $group)
{
$dbw = wfGetDB(DB_MASTER);
$groupOrder = Lookup_getGroupOrder($group);
$dbw->query ("INSERT INTO ".Lookup_prefix()."lookups (lu_name, lu_url, lu_group, lu_order, lu_group_order) VALUES ('".mysql_real_escape_string($name)."', '".mysql_real_escape_string($url)."', '".mysql_real_escape_string($group)."', 1, $groupOrder)");
Lookup_reOrderGroups();
return true;
}
And another one further down:
function Lookup_moveGroupUp($group)
{
$dbw = wfGetDB(DB_MASTER);
$dbw->query ("UPDATE ".Lookup_prefix()."lookups SET lu_group_order = 0 WHERE lu_group = '".mysqli_real_escape_string($group)."'");
Lookup_reOrderGroups();
return true;
}
mysqli_real_escape_string() needs the database link as the first parameter, which is why it isn't working.
However, MediaWiki wants us to avoid direct queries, so it has the $dbw->insert() method instead, one of several wrapper functions.
Use something like this:
function Lookup_addLookup ($url, $name, $group)
{
$dbw = wfGetDB(DB_MASTER);
$groupOrder = Lookup_getGroupOrder($group);
$dbw->insert(
Lookup_prefix()."lookups",
array(
'lu_name' => $name,
'lu_url' => $url,
'lu_group' => $group,
'lu_order' => 1,
'lu_group_order' => $groupOrder
)
);
Lookup_reOrderGroups();
return true;
}
And in the second example, use $dbw->update():
function Lookup_moveGroupUp($group)
{
$dbw = wfGetDB(DB_MASTER);
$dbw->update(
Lookup_prefix()."lookups",
array(
"lu_group_order" => 0
),
array(
"lu_group" => $group
)
);
Lookup_reOrderGroups();
return true;
}
For more information and other SQL wrappers, read about the different wrapper functions and their documentation.

Laravel: Store error messages in database

Any one know how to send error messages to database in laravel which generate from app/exceptions/handler.php ?
I need to send what error massages generated in report() method to database.
If you are interested doing this manually, you can do something as following.
Step 1 -
Create a model to store errors that has a DB structure as following.
class Error extends Model
{
protected $fillable = ['user_id' , 'code' , 'file' , 'line' , 'message' , 'trace' ];
}
Step 2
Locate the App/Exceptions/Handler.php file, include Auth, and the Error model you created. and replace the report function with the following code.
public function report(Exception $exception) {
// Checks if a user has logged in to the system, so the error will be recorded with the user id
$userId = 0;
if (Auth::user()) {
$userId = Auth::user()->id;
}
$data = array(
'user_id' => $userId,
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
);
Error::create($data);
parent::report($exception);
}
(I am demonstrating this using laravel 5.6)
Because Laravel uses Monolog for handling logging it seems that writing Monolog Handler would be the cleanest way.
I was able to find something that exists already, please have a look at monolog-mysql package. I did not use it, so I don't know whether it works and if it works well, but it's definitely good starting point.