CakePHP find on a json encoded field - json

I am creating a store in CakePHP and have added a text field which stores a json array of all the categories the store falls into.
How do I do a cake find on all stores that fall into the "gardening" category?
json encoded field:
["gardening","books-and-toys"]
I am thinking some sort of in_array() find but not quite sure.
Many Thanks

You can use the PHP method json_decode and return the JSON object as an associative array by setting the second option of that function to true. You can then call array_search for example to return the corresponding key.
<?php
$categories = json_decode($json, true);
$categoryKey = array_search('gardening', $categories);
$categoryName = $categories[$categoryKey];
?>
You can then use $categoryName in a CakePHP find() call.

Related

store string into array

In MySQL, I have a field name postcodes and the type is LONGTEXT. It stores several postcodes being separated by comma. How would I retrieve that and store it as an array for other use ?
you can use the PHP method explode().
one think you can't do, is to do a where x = x on it in the database.
In the model, you can set the mutator methods:
public function getPostcodesAttribute($value) {
return explode(',',$value);
}
public function setPostcodesAttribute($value) {
$this->attributes['postcodes'] = implode(',',$value);
}
Lets say that you have the result stored in a string like this:
$s = "6000,5447"; //$s = $array->postcodes;
you can get the each value on an index in an array using this:
$values= explode(",", $s);
echo $values[0]; // 6000
Or even better.. you can store it as json, and retrieve it as json in array format.
Store it as a JSON field in MySQL, Laravel encode and decode them when you retrieve and save them respectively
in your migration
$table->json('field_name');
then in the model
protected $json = ['field_name'];
then whenever you access the field, laravel will convert it to an array for you, you don't have to call any explicit methods.
Doc - https://laravel.com/docs/5.7/eloquent-mutators#attribute-casting
// the final array all the post codes are collected.
$postCodes = [];
foreach (Model::pluck('postcodes') as $stringCodes)
foreach (explode(',', $stringCodes) as $postCode) $postCodes[] = $postCode;

How to convert params to array

I receive the following URL
/articles?difficulty=1,2
Inside the framework I want to receive an array from that - [1,2]. Is there any method in the framework to convert params like that into an array automatically? Can the framework do that? I can do like that explode(',', $params['difficulty']) - but I'm wondering whether this can be handled by the framework.
I don't want to pass params like that:
/articles?difficulty[]=1&difficulty[]=2
There is no helper in framework Request component for converting such values, it can be easily achieved with native PHP explode function. Use:
$array = explode(',', $string);
as you suggested.
But the wrapper of explode exists - \yii\helpers\StringHelper::explode(), it has additional options for trimming and skipping empty elements, you can use it too. But most of the times using regular explode should be enough.
try this
Use the Request class.
http://www.yiiframework.com/doc-2.0/yii-web-request.html
print_r(Yii::$app->request->get()); returns all get variables in an array. It's like doing print_r($_GET); in straight php.
If you want a specific $_GET variable you access it as follows:
Yii::$app->request->get('difficulty');
In your case it would be:
$success = Yii::$app->request->get('success');
$token = Yii::$app->request->get('token');
Then after that explode it with comma, so it will get converted into array.
$array = explode(',', $success );

Kohana 2.3.4 : MySQL query to JSON

I am managing a project in Kohana 2.3.4 where I need to create an API for my android backend. What I am doing is send a query on my model which returns $result.
$query = "select product.deal_id,product.deal_key,p..."
$result = $this->db->query($query);
I am not sure if $result is an Object or and array This consists of 4 rows and 8 columns. I need to change $result to json format. I am currently doing this by echoing.
echo json_encode($result);
This return an empty json {}.
I am able to use the same query on my view by iterating through the $result
foreach ($result as $h){
echo $h->main_key;
}
Am I doing this right or is it that my $result on this connection has no rows?
I figured out I had use Kohana debug to know if my result was an object or an array. After calling the following
echo Kohana::debug($result);
I figured out that it was an object hence the empty result when converted to a json object. I had also tried getting an associative array with mysql_fetch_assoc which actually expected a mysql query object. This did
not work as the object was created by my ORM object. I then solved this by calling
$result = $this->db->query($query)->as_array();
This returned an array and solved my problem.

Facebook JSON output

I have this code
$graph_url = "https://graph.facebook.com/100000070848126/statuses?access_token=".$params['access_token'];
$status = json_decode(file_get_contents($graph_url),true);
echo $status->data->message;
and I'm having a problem on how to output data in the array $status. I just don't know how to call the items for this feed
The second parameter of json_decode() is whether to create an associative array or not.
json_decode ( string $json [, bool $assoc = false])
You specified that you do want an array, so the way you would access the values would be like this -
echo $status['data']['message'];
If you leave out the true parameter of the json_decode() function, you'll be able to access the values in a more object oriented way like the syntax in your question.

Magento: make PDO results into a Varien Object

I have a stored procedure that I call upon using 'core_read' and query method. The results are then gathered using fetchAll(PDO::FETCH_ASSOC).
The data comes out perfectly. I can do a foreach on the array, and access data by array keys ($row['name']).
I would like to convert the associative array into a Varien_Object, so I could access the data using $row->getName() notation... Keeping it in Magento style... How would I perform such a conversion, if possible?
Pass your array to the constructor of Varien_Object
$object = new Varien_Object($array);
See the code for the constructor in lib/varien/object
I think you just can use:
foreach($rows as $row) {
$object = new Varien_Object();
$object->setData($row);
}
Thank you for your suggestions, and I think it would have worked if I had one line coming back from the stored procedure. Here's what I ended up doing:
foreach($rows as $row) {
$orders[] = new Varien_Object($row);
}