Get Entity's ID in Datastore PHP API - php

Is there a way to get the entity's ID/Key using PHP Datastore API?
I've already authenticated everything and I'm able to get the values, but no the ID
Here's my code:
<?php
$title = "Builder | Makeroid Account";
include "../assets/includes/head.php";
if (!$USER->is_logged_in()) {
$USER->redirect('/');
}
require_once __DIR__.'/../assets/libs/datastore/vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Datastore\Query\Query;
$datastore = new DatastoreClient([
'projectId' => 'makeroid-builder'
]);
$query = $datastore->query();
$query->kind('UserData');
$query->filter('emaillower', '=', $U_DATA['email']);
$users = $datastore->runQuery($query);
$params = ["email", "emailFrequency", "emaillower", "isAdmin", "link", "name", "sessionid", "settings", "templatePath", "tosAccepted", "type", "upgradedGCS"];
?>
<div class="content">
<div class="container-fluid">
<div class="row">
<?php foreach ($users as $user) { ?>
<div class="col-md-12">
<div class="card">
<div class="card-header card-header-icon" data-background-color="rose">
<i class="material-icons">assignment</i>
</div>
<div class="card-content">
<h4 class="card-title">User Properties</h4>
<div class="table-responsive">
<table class="table">
<thead class="text-primary">
<th>Key</th>
<th>Value</th>
</thead>
<tbody>
<?php print_r($user); foreach ($params as $param) { ?>
<tr>
<td><?=$param?></td>
<td><?=$user?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
<?php include "../assets/includes/footer.php"; ?>
I would like to get this:
I'm able to get all values that are in the rows, such us my email, but I hadn't found a way to get that ID
I've tried by using $user['id'], $user['key'] or $user['__key__'] but none of them worked

You'll need to pull the key from the entity (https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/entity?method=key) then pull the id from the key (https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.56.0/datastore/key) with something like $key->pathEndIdentifier().
So instead of trying to pull the key out of object as a dictionary value, try pulling it out as a method. $user->key()->pathEndIdentifier() .

Related

Get the the average of the data in laravel

I have two eloquent tables, vendor and work, in the work table there is a total score column and I want to get the average of the total score based on the vendor ID (foreign Key) and save it to the vendor table. May I know how to achieve this?
Here is my vendor table blade file
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Tabel Penilaian Penyedia</h3>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive">
<table id="tabelpenyedia" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">No.</th>
<th>Nama Penyedia</th>
<th>Nilai</th>
<th style="width: 120px">Aksi</th>
</tr>
</thead>
<tbody>
#php $no = 1; #endphp
#foreach ($penyedia as $penyedias)
<tr>
<td>{{$no++}}</td>
<td>{{$penyedias->nama}}</td>
<td></td>
<td>
Beri Penilaian
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>
Here is the AdminController
public function update_nilaipekerjaan(Request $request, $id){
$pekerjaan = Pekerjaan::find($id);
//$pekerjaan->update($request->all());
//$total = Pekerjaan::select(DB::raw('avg(nilai_1 + nilai_2 + nilai_3 + nilai_4) as nilai_total'))->get();
$pekerjaan->nilai_1=$request->nilai_1;
$pekerjaan->nilai_2=$request->nilai_2;
$pekerjaan->nilai_3=$request->nilai_3;
$pekerjaan->nilai_4=$request->nilai_4;
$pekerjaan->nilai_total=$request->nilai_1+$request->nilai_2+$request->nilai_3+$request->nilai_4;
$pekerjaan->update();
return redirect()->back()->with('message','Nilai Pekerjaan Berhasil diupdate!');
}
public function tabelnilai_penyedia(){
$penyedia = Penyedia::all();
$pekerjaan = Pekerjaan::all();
return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan'));
} //I WANT TO SHOW THE AVERAGE OF THE TOTAL SCORE HERE.
Database https://imgur.com/a/N6NlcwJ
Here is what I want to achieve https://imgur.com/a/fk4Yq92
what I have tried
updating the AdminController
public function tabelnilai_penyedia($id){
$penyedia = Penyedia::all();
$pekerjaan = Pekerjaan::all();
$average = Pekerjaan::where('penyedia_id', $id)->avg('nilai_total');
Penyedia::where('id', $id)->update(['nilai'=>$average]);
return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan'));
}
But it got me an error,
Too few arguments to function App\Http\Controllers\AdminController::tabelnilai_penyedia(), 0 passed in C:\Users\ASUS TUF\webpenyedia\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
You can use the MySQL AVG function to get the average of a column, which in Eloquent is neatly wrapped in an aggregate function
Assuming your models are called "Work" and "Vendor"
// Get average of 'nilai' based on Vendor ID
$average = Work::where('penyedia_id', $vendorId) // Assuming this is the right column
->avg('nilai_total');
// Update Vendor directly in database (No model events)
Vendor::where('id', $vendorId)->update(['nilai' => $average]);

Undefined variable $p and foreach() argument must be of type array|object, null given in codeigniter 3

I am trying to pass an array that gets initialised when it gets queried from the database.
The array of products that I try to pass and display in the view just throws an error and i cannot figure out why.
I am using codeigniter 3.
ProductsModel.php
function get_all_products()
{
$this->db->select("produceCode,description,category,supplier, quantityInStock, bulkSalePrice,photo");
$this->db->from('products');
$query = $this->db->get();
return $query->result();
}
ProductsController.php
public function listProducts()
{
$data['p']=$this->ProductsModel->get_all_products(/*2, $this->uri->segment(3)*/);
$this->load->view('products',$data);
}
products.php
<?php foreach( $p as $row) { ?> // line of code thats causing the error
<div class="col-md-3">
<div class="card p-3">
<div class="text-center"> <!--<img src="../assets/images/products/thumbs/--><?php /*$row->photo;*/?>" width="200"> </div>
<div class="product-details"> <span class="font-weight-bold d-block">$ 7.00</span> <span><?php echo 'hi'/*$row->description;*/?></span>
<div class="buttons d-flex flex-row">
<div class="cart"><i class="fa fa-shopping-cart"></i></div> <button class="btn btn-success cart-button btn-block"><span class="dot">1</span>Add to cart </button>
</div>
</div>
</div>
</div>
<?php } ?>
any suggestions would be great
You are returning a variable named $data in your controller, but in your view you are looping through $p.
Try this:
foreach ($data['p'] as $row)

Search for Products in the User View - Cakephp 3

Friends, I am displaying a user's profile along with the products registered by him. My relationships are correct. I can list all products as normal, along with user information. But my product search field in the user's view doesn't work.
This is my code detail_user.ctp
//user information only above. (Address, Telephone, etc.)
.
.
.
<h2 class="bd-title mt-2" id="content">Products Gallery</h2>
<div class="card mt-2 mb-2">
<div class="card-header">
<?= $this->Form->create('', ['type' => 'get']); ?>
<div class="input-group">
<?= $this->Form->control('keyword', array('class' => 'form-control','name'=> 'search','label' => false,'required' => true,));?>
<div class="input-group-append">
<?= $this->Form->button(__('Search')); ?>
</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-hover">
<tbody>
<?php foreach ($user->products as $product): ?>
<td>
<p class="title mb-0"><?= h($product->name) ?> </p>
<var class="price text-muted">R$ <?= number_format($product->price);?></var>
</td>
<td> <br></td>
<td width="130">
<?= $this->Html->link(__('Open'), ['controller' => 'Produtos','action' => 'details_product','prefix' => false, $product->slug], array('class' => 'btn btn-primary')) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody></table>
</div> <!-- table-responsive .end// -->
</article>
And my method is like this:
public function detailUser($slug)
{
$keyword = $this->request->getQuery('keyword');
$query = $this->Users
->find()
->matching('Products', function(\Cake\ORM\Query $q) {
return $q->where(['name LIKE' => ('%'. $this->request->getQuery('keyword') . '%')]);
});
$usuario = $this->Users
->findBySlug($slug)
->contain(['Products', 'Groups'])
->firstOrFail();
$query = $this->Users->find('all');
$this->set('user', $user);
$this->set('query', $this->paginate());
}
I have difficulties to solve this method. At the moment, when I type the product name, the url changes, but the product is not searched.
I consulted the generated SQL and everything is correct, but it does not filter the word entered in the search field and also does not give an error.
I appreciate any comments!

is there any solution to face the Error: Call to a member function myfunction() on array in?

I am facing some errors with my code, actually I've read another same problem, but I cannot catch the point to solve this error, I'm using controller to fetch almost all table called including ppdb, <-- this table show errors Fatal Error Call to a member function getPdb() on array in line 64
this my code
<tbody>
<?php
$no = 1;
if(is_array($data->getPdb()) || is_object($data->getPdb())){ //This line 64
foreach($data->getPdb() as $data){?>
<tr>
<td><?=$no++?></td>
<td><?=$data['gelombang']?></td>
<td><?=$data['tanggalbuka']?></td>
<td><?=$data['tanggaltutup']?></td>
<td><?=$data['tahunajaran']?></td>
<td><?php if($data['status']==1){echo 'Aktif';}else{echo 'Tidak Aktif';}?></td>
<td>
<i class="fa fa-<?php echo (($data['status']==2)?'check':'check');?>"></I>&nbsp<?php echo (($data['status'] == 2)?'AKtifkan':'Non Aktifkan');?>
</td>
</tr>
<?php } } ?>
</tbody>
in my controller the code written like this:
$getta = $this->pdo->prepare("SELECT * FROM ppdb");
$getta->execute();
$c = $getta->rowcount();
if($c > 0){
foreach($getta as $data){
$hasil[] = $data;
}
return $hasil;
}
}
And the error shown like this
Fatal error: Uncaught Error: Call to a member function getPdb() on array in D:\xampp\htdocs\nh\inc\view\admin\inc\ppdb.php:64 Stack trace: #0 D:\xampp\htdocs\nh\inc\view\admin\index.php(31): include() #1 {main} thrown in D:\xampp\htdocs\nh\inc\view\admin\inc\ppdb.php on line 64
Above this table I use another function to save the form and fetch the option:
<form class="form animated bounceIn" method="POST">
<div class="form-group">
<?php
$data->createPpdb();
?>
</div>
<div class="card">
<div class="card-header bg-dark">
Setting Gelombang PPPDB
</div>
<div class="card-body">
<div class="form-group">
<input type="text" name="gelombang" title="Nama Gelombang" class="form-control" placeholder="contoh: Gelombang I">
</div>
<div class="form-group">
<input type="text" name="tanggalbuka" title="Tanggal Buka PPDB" class="form-control" placeholder="Pilih Tanggal" id="tbuka">
</div>
<div class="form-group">
<input type="text" name="tanggaltutup" title="Tanggal Tutup PPDB" class="form-control" placeholder="Pilih Tanggal" id='ttutup'>
</div>
<div class="form-group">
<select class="form-control" name="ta">
<option value="" disbaled>-Pilih Tahun Ajaran-</option>
<?php
if(is_array($data->getTa()) || is_object($data->getTa())){
foreach($data->getTa() as $data){?>
<option value="<?=$data['tahunajaran']?>"><?=$data['tahunajaran']?></option>
<?php
}
}
?>
</select>
</div>
</div>
<div class="card-footer">
<div class="form-group">
<input type="submit" name="simpanppdb" class="btn btn-sm btn-success" value="Simpan">
</div>
</div>
</div>
</form>
I'd read with similar problem from stacoverflow.com but I cannot get the point.
Thank you for someone who save my day.
The problem is that you're reusing the variable $data in the line:
foreach($data->getPdb() as $data){
So $data starts out being an object, but after the loop it contains the last row of the results, which is an array. The next time you try to call $data->getPdb() you'll get an error.
Use different variables, like this:
foreach ($data->getPdb() as $item) {
Also, you shouldn't call $data->getPdb() repeatedly. Each time you do is_array($data->getPdb()) and is_object($data->getPdb()) it performs the full query and collects all the results. Do it once and save the result in a variable.
$pdb = $data->getPdb();
if (is_array($pdb) || is_object($pdb)) {
foreach ($pdb as $item) {
...
}
}
Thank you for your answer all, I've solved this with my own way and it took from Mr. #Braham comment, I try to combine the variable and re-initiate the class.
this the solving problem
<table class="table table-hover" id="ppdb">
<thead>
<tr>
<th>No</th>
<th>Gelombang</th>
<th>Tanggal Buka</th>
<th>Tanggal Tutup</th>
<th>Tahun Ajaran</th>
<th>Status</th>
<th>Opsi</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$item = new NurulHuda(); // This re-initiate Class
if(is_array($item->getPpdb()) || is_object($item->getPpdb())){
foreach($item->getPpdb() as $val){?>
<tr>
<td><?=$no++?></td>
<td><?=$val['gelombang']?></td>
<td><?=$val['tanggalbuka']?></td>
<td><?=$val['tanggaltutup']?></td>
<td><?=$val['tahunajaran']?></td>
<td><?php if($val['status']==1){echo 'Aktif';}else{echo 'Tidak Aktif';}?></td>
<td>
<i class="fa fa-<?php echo (($val['status']==2)?'check':'check');?>"></I>&nbsp<?php echo (($val['status'] == 2)?'AKtifkan':'Non Aktifkan');?>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
Thank you Mr. #Braham for lighting me...

Yii2 rendering in a specific div

I'm using Yii2-advanced-template. I've 2 divisions on my form as follows - When I'll click on any of the radio button in 2nd div, the details of respective entry should be loaded in 1st div. That is, the form in 1st div will remain as is, but loads some values for update operation. But I'm getting it as - which is not what I expect. It is loading the whole page layout in that div.
I already used 'renderPartial()' in my controller. But it solved only the half of the problem, that is, the navigation bar & other layout is hidden after using renderPartial().
My form is as follows-
<div class="col-sm-12">
<div class="col-sm-6 fillitemform">
...
Enter Item Details form
...
</div>
<div id="item_details" class="col-sm-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Item details</h3>
</div>
<div class="panel-body">
<div class="table-responsive" >
<table class="table">
<thead>
<tr><th></th>
<th>PO No.</th>
<th>SKU</th>
<th>Order Type</th>
<th>Expected Qty</th>
<th>Received Qty</th>
<th>Weight</th>
</tr>
</thead>
<tbody>
<?php
for($itr = 0, $ro = $modelRO, $rod = $modelROD; $itr < count($modelROD); ++$itr){
echo '<tr onclick="placeitemdtls('.$rod[$itr]['id'].')">';
echo '<td><input type="radio" name="item"></td>';
echo '<td>'.$ro[0]['ro_po_no'].'</td>';
echo '<td>'.$rod[$itr]['rod_sku'].'</td>';
echo '<td>'.$ro[0]['ro_order_type_id'].'</td>';
echo '<td>'.$rod[$itr]['rod_expected_qty'].'</td>';
echo '<td>'.$rod[$itr]['rod_received_qty'].'</td>';
echo '<td>'.$rod[$itr]['rod_weight'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
My JS/jQuery function-
function placeitemdtls(id){
$.post("index.php?r=receiving-order-details/update&id="+id, function(response) {
jQuery(".fillitemform").html(response);
});
}
My Controller function-
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelRO = ReceivingOrders::find()->where(['id' => $id])->all();
$modelROD = ReceivingOrderDetails::find()->where(['receiving_order_id' => $id])->all();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['create', 'id' => $id]);
} else {
return $this->renderPartial('update', [
'model' => $model, 'modelRO' => $modelRO, 'modelROD' => $modelROD,
]);
}
}
Please note that, I'm getting the correct form with proper values, but having rendering problem as I showed in 2nd image. Please help me to solve it. So that my output should look like -

Categories