I have _Installation and Swipt_User classes in parse, the installation has UserR, which related to Swipt_User table as in the pic :
I have this query :
$query = new ParseQuery("_Installation");
$query->includeKey("Swipt_User");
$installations = $query->find(true);
included the object related to installation which is Swipt_User, but when i try to get the object in foreach, it didn't get it as below and returned as null
foreach ($installations as $key => $installation) {
$user = $installation->get('Swipt_User');
}
Any help ? .. please
You need to specify the name of the column, not the name of the related table (group), when using include.
Your code needs to be:
$query = new ParseQuery("_Installation");
$query->includeKey("userR");
$installations = $query->find(true);
Related
I have this raw query that extracts data from the database table in Laravel
$username = DB::table('hotspot_users')
->select('userName')
->where('assignedTo', '786')->get();
I get these values: [{"userName":"kim"}]
but I want to extract just the username "kim" itself, not an array. I have tried to do this without success:
$convert1=json_decode($username);
$newusername=$convert1->userName;
but i get an error: Trying to get property 'userName' of non-object
Any idea how I can solve this
Instead of what you're doing, you can just do this:
HotspotUser::where('assignedTo', 786)->pluck('userName');
Now you will have a collection of names; or if you only want the first one:
HotspotUser::where('assignedTo', 786)->first()->user_name;
(I'm not certain if the property will be user_name or userName; you should have simple column names to make things easier.)
You are getting the result of DB as php stdClass object, so you can iterate over it and get every variable that is queried. Something like below code may help you.
In case of using get()
$data = DB::table('hotspot_users')
->where('assignedTo', '786')->select('userName')->get();
foreach ($data as $datum) {
$result[] = $datum->userName;
}
In case of using first()
$data = DB::table('hotspot_users')
->where('assignedTo', '786')->select('userName')->first()->userName;
Hi As you mentioned that is an array [{"userName":"kim"}]
So you have two choices
First , you first insted of get
$username = DB::table('hotspot_users')
->select('userName')
->where('assignedTo', '786')->first();
$newusername=$convert1->userName;
Second , get the first element of array
$username = DB::table('hotspot_users')
->select('userName')
->where('assignedTo', '786')->get();
$username = reset($username);
$convert1=json_decode($username);
$newusername=$convert1['userName'];
I cannot get the findAll function to return more than the last row of the result query. I set R::debug( TRUE ) on the PHP script and it clearly says that there are 4 results in the returned data resultset. The query outputted works as expected, returning 4 rows, when I entered it directly into MySQL.
Here is my PHP code:
<?php
require 'include/rb.php';
include 'include/config.php';
R::debug( TRUE );
echo 'test4<br>';
$returnpeople = R::findAll('breadline');
echo '<br>';
foreach ($returnpeople as $key => $bean) {
echo $bean->tstamp.'<br>';
}
print_r($returnpeople );
?>
breadline is a MYSQL table with 2 fields:
tstamp and val
The system I have to deploy my code on runs PHP 5.3.3 and as such I've done the patch.
I've seen other people describe this issue as well, but they used parameters. I'm still getting it even after removing all parameters and calling R::findAll('breadline');.
I have not been able to reproduce this error, even when using parameters, on my test server which I have set to run PHP 5.3.3.
On Redbean website, there are some requirements :
Existing schemas
RedBeanPHP has been designed to build your database on-the-fly, as you go. Afterwards, you can manually change the schema to suit your needs (change column types, add additional indexes). Remember that the purpose of RedBeanPHP is to have an easy, configuration-less ORM. This can be achieved only by respecting certain conventions.
I assume your table must have a id field because in the method convertToBeans, the id is used :
public function convertToBeans( $type, $rows )
{
$collection = array();
$this->stash[$this->nesting] = array();
foreach ( $rows as $row ) {
$id = $row['id'];
$this->stash[$this->nesting][$id] = $row;
$collection[$id] = $this->load( $type, $id );
}
$this->stash[$this->nesting] = NULL;
return $collection;
}
$query = "SELECT * FROM Name";
works perfectly as expected but:
$query = "SELECT * FROM Name WHERE name = 'david'";
doesn't work as expected. The DataStore is created as follows:
$obj_name = new Entity();
$obj_name->name = strtolower($name);
$obj_name->age = $age;
$result = $obj_name_store->upsert($obj_name);
Any suggestions for how to extract a specific item using GQL?
Thank you.
It looks like you are using my php-gds library from here: https://github.com/tomwalder/php-gds
If so, then the problem is likely that you have not explicity requested the "name" property be indexed by Datastore.
When defining your Schema, you need to pass the optional second parameter "TRUE" in order for queries to work against those fields.
See here for a code example.
https://github.com/tomwalder/php-gds#defining-your-model
I use create command of Yii as
$sql = "select name from users where id = 2";
$EmployeeName=Yii::app()->db->createCommand($sql)->queryAll();
now i have to use foreachloop to get specific value like
foreach($EmployeeName as $value)
$name = $value['name'];
Can i bypass foreach loop like
$EmployeeName -> name; //To get value of specific field
Question is why i use foreach loop when i know i have single index array?
when i am using print_r($EmployeeName) its showing me sql command in object instead of data so i am confused how to debug object array
Assuming id is your primary key, you should use queryScalar instead. This will return a single value and not an array.
$name=Yii::app()->db->createCommand($sql)->queryScalar();
You can use this syntax in Yii:
$user = Users::model()->findByAttributes(array('id' => 2));
$name = $user->name;
echo $name;
Single line solution
The simplest approach is find by primary key (PK).
Suppose id is PK in the Users model (Users model should be set thru gii model generator):
$name = Users::model()->findByPk(2)->name;
I want to get all column names of a row in cassandra , how can I do it in phpcassa?
If phpcassa does not support it, does any other language, libs can do it?
In my case, column names are short, but rows are long(around 1000+),data are big(around 100K)
You have a good question. Try something like that:
$pool = new ConnectionPool('feed', array('127.0.0.1'));
$raw = $pool->get();
$rows = $raw->client->execute_cql_query("SELECT * FROM posts", cassandra_Compression::NONE);
var_dump($rows);
Maybe it will help...
Do you mean to get the names directly and only with phpCassa? I don't know any way to do it directly but I used to do that by getting all the row and then executing a foreach loop over the array I have from the column family, like this:
1.- A small function to use everywhere (build your own if you need ;) ):
function f_get_data_as_array($p_pool, $p_cf, $p_key, $p_col_count = 100, $p_column_names = NULL, $p_range_start = '', $p_range_end = '', $p_inverted_sort = false)
{
try{
$lv_slice = new ColumnSlice($p_range_start, $p_range_end, $p_col_count, p_inverted_sort);
$lv_cf = new ColumnFamily($p_pool, $p_cf);
$lv_cf->insert_format = ColumnFamily::ARRAY_FORMAT;
$lv_cf->return_format = ColumnFamily::ARRAY_FORMAT;
$lv_result = $lv_cf->get($p_key, $lv_slice, $p_column_names);
}catch(Exception $lv_e)
{
return false;
}
return $lv_result;
2.- I call it using the first four parameters, setting the pool, the column family name, the key I need and the number of columns I want to get (set the number as you need).
3.- A foreach loop over the returned array to get each column name. Or, if you know the structure you will get from your column family, you just need to use the right indexes, probably: $lv_result[0][0], $lv_result[0][1], and so...
Hope it helps. And sorry for my English!