CodeIgniter-Access from model to controller - php

I have the following code in Model:
<?php
class Route_Model extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function getRoute($date = array())
{
try {
$data = array(
'route' => array(
'id' => 1,
'name' => 'budapest-athens',
'price' => 150,
'id' => 2,
'name' => 'rome-madrid',
'pret' => 250,
'id' => 3,
'name' => 'belgrade-bucharest',
'price' => 180,
'id' => 4
)
);
return $data;
} catch (Exception $e) {
return $e->getMessage();
}
}
}?>
And I want to access array elements in my controller.
How can I access each field separately?
Something like $price = $this->data['price']?
Thank you!

You are returning an array with two levels, if you want to get the price from the array $data, simply do this in your controller:
$data = $this->route_model->getRoute($date);
$price = $data['route']['price'];
Please, note that your array is not well formed because you have repeated keys and this may cause problems

This array will never work since you're overwriting keys, I think you would want the following array:
$data = [
'route' => [
[
'id' => 1,
'name' => 'budapest-athens',
'price' => 150
], [
'id' => 2,
'name' => 'rome-madrid',
'price' => 250
], [
'id' => 3,
'name' => 'belgrade-bucharest',
'price' => 180
]
]
];
Next to that, your try / catch seems unnecessary here, there is no real try. It's a hard-coded array, so unless this will actually do some interactions there is no need for the try / catch.
Anyway, to receive this data in your controller you should do:
$this->load->model('Route_model');
$route = $this->Route_model->getRoute();
var_dump($route);
exit;
Now you will have this array. Another wonder, are you actually trying to grab all the routes in this array, or is there something you want to do with the $date parameter? Since right now it doesn't looks like it's used unless you stripped some code away.

Related

Laravel - phpunit array assertion

I am trying to write a phpunit test so I can test that I'm getting the correct assertion, this test i currently have which is passing and works as intended.
/**
#test
*/
$filmInfo = $this->postRequest();
$this->assertFilm($this->requestData, $filmInfo['id']);
$film = Film::findOrFail($filmInfo['id']);
$this->assertEquals('LOTR', $filmInfo['name']);
$this->assertEquals('Film about a ring', $filmInfo['description']);
$this->assertEquals('Fantasy', $filmInfo['genre']['main']);
$this->assertEquals('Adventure', $filmInfo['genre']['sub']);
}
This is the request data array it is referring to:
private function requestData(): array
{
return [
'name' => 'LOTR',
'description' => 'Film about a ring',
'main' => 'Fantasy',
'sub' => 'Adventure',
];
}
This test works fine and it passing but I want to test it within one assertion like so:
$this->assertEquals([
'name' => 'LOTR',
'description' => 'Film about a ring',
'genre' => [
'main' => 'Fantasy',
'sub' => 'Adventure'
]
,
], $filmInfo);
But I keep getting an error that the 2 arrays I'm asserting are not matching, do you guys have an idea on what could be causing this?
Just like what the PHPUnit said, your array aren't matching. Your array should match with all values in $filmInfo array.
From your code, we can guessing that you aren't comparing the id. Maybe you can try this code:
$this->assertEquals(array_merge($filmInfo, [
'name' => 'LOTR',
'description' => 'Film about a ring',
'genre' => [
'main' => 'Fantasy',
'sub' => 'Adventure'
],
]), $filmInfo);

How to access json in an array?

I am trying to access json in a nested array but it is not working yet.
I am developing in laravel an have this
public function rave(Request $request){
$vars = [
'id' => Request::get('id'),
'txref' => Request::get('txRef'),
'flwref' => Request::get('flwRef'),
'created_at' => Request::get('createdAt'),
'amount' => Request::get('amount'),
'status' => Request::get('status'),
'name' => Request::get('fullName'),
'email' => Request::get('customer'),
];
I am able to get and display all in view but name and email which comes back in the response as
"customer":{
"id":15672,
"phone":"7667866 ",
"fullName":"john doe ",
"customertoken":null,
"email":"kely#gmail.com",
"createdAt":"2019-06-09T08:28:56.000Z",
"updatedAt":"2019-06-09T08:28:56.000Z",
"deletedAt":null,
"AccountId":33519
},
How do I access the data under customer because this
'name' => Request::get('fullName'),
'email' => Request::get('customer'),
doesn't seem to work like the others
You use Request $request as function parameter,
and check output of
echo $request->input('customer.fullName');
echo $request->input('customer.email');

Using elasticsearch, how to create an index for a document that contains an array, and append to that array in the future

In my example code I am using the php client library, but it should be understood by anyone familiar with elasticsearch.
I'm using elasticsearch to create an index where each document contains an array of nGram indexed authors. Initially, the document will have a single author, but as time progresses, more authors will be appended to the array. Ideally, a search could be executed by an author's name, and if any of the authors in the array get matched, the document will be found.
I have been trying to use the documentation here for appending to the array and here for using the array type - but I have not had success getting this working.
First, I want to create an index for documents, with a title, array of authors, and an array of comments.
$client = new Client();
$params = [
'index' => 'document',
'body' => [
'settings' => [
// Simple settings for now, single shard
'number_of_shards' => 1,
'number_of_replicas' => 0,
'analysis' => [
'filter' => [
'shingle' => [
'type' => 'shingle'
]
],
'analyzer' => [
'my_ngram_analyzer' => [
'tokenizer' => 'my_ngram_tokenizer',
'filter' => 'lowercase',
]
],
// Allow searching for partial names with nGram
'tokenizer' => [
'my_ngram_tokenizer' => [
'type' => 'nGram',
'min_gram' => 1,
'max_gram' => 15,
'token_chars' => ['letter', 'digit']
]
]
]
],
'mappings' => [
'_default_' => [
'properties' => [
'document_id' => [
'type' => 'string',
'index' => 'not_analyzed',
],
// The name, email, or other info related to the person
'title' => [
'type' => 'string',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
'authors' => [
'type' => 'list',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
'comments' => [
'type' => 'list',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
]
],
]
]
];
// Create index `person` with ngram indexing
$client->indices()->create($params);
Off the get go, I can't even create the index due to this error:
{"error":"MapperParsingException[mapping [_default_]]; nested: MapperParsingException[No handler for type [list] declared on field [authors]]; ","status":400}
HAD this gone successfully though, I would plan to create an index, starting with empty arrays for authors and title, something like this:
$client = new Client();
$params = array();
$params['body'] = array('document_id' => 'id_here', 'title' => 'my_title', 'authors' => [], 'comments' => []);
$params['index'] = 'document';
$params['type'] = 'example_type';
$params['id'] = 'id_here';
$ret = $client->index($params);
return $ret;
This seems like it should work if I had the desired index to add this structure of information to, but what concerns me would be appending something to the array using update. For example,
$client = new Client();
$params = array();
//$params['body'] = array('person_id' => $person_id, 'emails' => [$email]);
$params['index'] = 'document';
$params['type'] = 'example_type';
$params['id'] = 'id_here';
$params['script'] = 'NO IDEA WHAT THIS SCRIPT SHOULD BE TO APPEND TO THE ARRAY';
$ret = $client->update($params);
return $ret;
}
I am not sure how I would go about actually appending a thing to the array and making sure it's indexed.
Finally, another thing that confuses me is how I could search based on any author in the array. Ideally I could do something like this:
But I'm not 100% whether it will work. Maybe there is something fundemental about elasticsearch that I am not understanding. I am completely new to so any resources that will get me to a point where these little details don't hang me up would be appreciated.
Also, any direct advice on how to use elasticsearch to solve these problems would be appreciated.
Sorry for the big wall of text, to recap, I am looking for advice on how to
Create an index that supports nGram analysis on all elements of an array
Updating that index to append to the array
Searching for the now-updated index.
Thanks for any help
EDIT: thanks to #astax, I am now able to create the index and append to the value as a string. HOWEVER, there are two problems with this:
the array is stored as a string value, so a script like
$params['script'] = 'ctx._source.authors += [\'hello\']';
actually appends a STRING with [] rather than an array containing a value.
the value inputted does not appear to be ngram analyzed, so a search like this:
$client = new Client();
$searchParams['index'] = 'document';
$searchParams['type'] = 'example_type';
$searchParams['body']['query']['match']['_all'] = 'hello';
$queryResponse = $client->search($searchParams);
print_r($queryResponse); // SUCCESS
will find the new value but a search like this:
$client = new Client();
$searchParams['index'] = 'document';
$searchParams['type'] = 'example_type';
$searchParams['body']['query']['match']['_all'] = 'hel';
$queryResponse = $client->search($searchParams);
print_r($queryResponse); // NO RESULTS
does not
There is no type "list" in elasticsearch. But you can use "string" field type and store array of values.
....
'comments' => [
'type' => 'string',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
....
And index a document this way:
....
$params['body'] = array(
'document_id' => 'id_here',
'title' => 'my_title',
'authors' => [],
'comments' => ['comment1', 'comment2']);
....
As for the script for apending an element to array, this answer may help you - Elasticsearch upserting and appending to array
However, do you really need to update the document? It might be easier to just reindex it as this is exactly what Elasticsearch does internally. It reads the "_source" property, does the required modification and reindexes it. BTW, this means that "_source" must be enabled and all properties of the document should be included into it.
You also may consider storing comments and authors (as I understand these are authors of comments, not the document authors) as child document in ES and using "has_child" filter.
I can't really give you specific solution, but strongly recommend installing Marvel plugin for ElasticSearch and use its "sense" tool to check how your overall process works step by step.
So check if your tokenizer is properly configured by running tests as described at http://www.elastic.co/guide/en/elasticsearch/reference/1.4/indices-analyze.html.
Then check if your update script is doing what you expect by retrieving the document by running GET /document/example_type/some_existing_id
The authors and comments should be arrays, but not strings.
Finally perform the search:
GET /document/_search
{
'query' : {
'match': { '_all': 'hel' }
}
}
If you're building the query yourself rather than getting it from the user, you may use query_string with placeholders:
GET /document/_search
{
'query' : {
'query_string': {
'fields': '_all',
'query': 'hel*'
}
}
}

Laravel merging Eloquent result and array

I'm trying to merge eloquent result and array because I need to add all possible filters that user can use for this model. If anyone have any other idea how to make it I really would be very thankful. Here is an example code:
<?php
class School extends Eloquent {
protected $table = 'schools';
public function listSchoolsEndUser()
{
$schools_data = new School;
$schools_data = $schools_data->paginate(12);
$filters = array(
'filters' => array(
'name' => 'Neshtoto'
)
);
$schools_data = (object) array_merge(get_object_vars($schools_data), $filters);
echo '<pre>';
print_r( $schools_data );
exit;
return $schools_data;
}
And the result is very interesting:
stdClass Object
(
[filters] => Array
(
[name] => Neshtoto
)
)
If you just want to send both, filters and the school_data back in a JSON response you can do it this way:
return Response::json(array(
'filters' => array(
'name' => 'Neshtoto'
),
'data' => $school_data->toArray()
));
Or if you want to use array_merge:
$school_data = array_merge($filters, array('data' => $school_data->toArray()));
return $school_data;
Edit
If you are just injecting the data into a view I see no reason at all to merge the data, just pass two variables!
return View::make('view', array('schools' => $school_data, 'filters' => $filters));
(Here $filters would obviously only be array('name' => 'Neshtoto') and not the full thing including 'filters' => ...)

Converting undefined indexes to null in PHP

I'm not sure if the title of this question is necessarily the accurate description of what I need to do, but I'll go ahead and ask my question and see what everyone thinks...
Basically, I am receiving data from a source that I have no control over, and I need to transpose it into a suitable format for inserting into my database using CakePHP. So, here's how I'm doing it:
public function submitApp($data) {
$array = array(
'Student' => array(
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'address' => $data['address'],
'dob' => $data['dob'],
'gender' => $data['gender']
),
'Application' => array(
'course_id' => $data['course_id'],
'question1' => $data['question1'],
'question2' => $data['question2'],
'question3' => $data['question3'],
'question4' => $data['question4'],
),
'ApplicationQualification' => $data['Qualifications']
);
// Logic to save $array goes here
}
The problem is that sometimes not all of the keys in $data will be submitted to my app but I still want my app to work with what it gets.
I know that I can wrap each key in a conditional like this:
if (!isset($data['name'])) { $data['name'] = null; }
...and then building the array, but this seems like a pretty clumsy way of doing it. Is there a more efficient way to do this?
You could use a simple ternary statement
'name' => array_key_exists('name', $data) ? $data['name'] : null
Alternatively, you can set up a default array and then merge the given values in
$defaults = [
'name' => null,
'email' => null,
// etc
];
$data = array_merge($defaults, $data);

Categories